# [Solved] RuntimeWarning: invalid value encountered in divide

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error RuntimeWarning: invalid value encountered in divide in python. So Here I am Explain to you all the possible solutions here.

## How RuntimeWarning: invalid value encountered in divide Error Occurs?

Today I get the following error RuntimeWarning: invalid value encountered in divide in python.

## How To Solve RuntimeWarning: invalid value encountered in divide Error ?

1. How To Solve RuntimeWarning: invalid value encountered in divide Error ?

To Solve RuntimeWarning: invalid value encountered in divide Error I think your code is trying to “divide by zero” or “divide by NaN”. If you are aware of that and don't want it to bother you, then you can try:

2. RuntimeWarning: invalid value encountered in divide

To Solve RuntimeWarning: invalid value encountered in divide Error I think your code is trying to “divide by zero” or “divide by NaN”. If you are aware of that and don't want it to bother you, then you can try:

## Solution 1

I think your code is trying to “divide by zero” or “divide by NaN”. If you are aware of that and don’t want it to bother you, then you can try:

```import numpy as np
np.seterr(divide='ignore', invalid='ignore')```

## Solution 2

To prevent division by zero you could pre-initialize the output ‘out’ where the div0 error happens, eg `np.where` does not cut it since the complete line is evaluated regardless of condition.

example with pre-initialization:

```a = np.arange(10).reshape(2,5)
a[1,3] = 0
print(a)    #[[0 1 2 3 4], [5 6 7 0 9]]
a/a   # errors at 3/0
out = np.ones( (5) )  #preinit
np.divide(a,a, out=out, where=a!=0) #only divide nonzeros else 1```

## Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.