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.

Without wasting your time, Let’s start This Article to Solve This Error.

Table of Contents

## 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 ?

**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:

**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[0]/a[1] # errors at 3/0 out = np.ones( (5) ) #preinit np.divide(a[0],a[1], out=out, where=a[1]!=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.

**Also, Read**