Skip to content Skip to sidebar Skip to footer

Nan To Num Python

I have multiple array that for those I calculate a linear regression, but sometimes it gives me 0/0 values which gives me a 'NaN'. I know that to convert an array where there are n

Solution 1:

numpy.nan_to_num works fine on scalars.

>>>import numpy as np>>>np.nan_to_num(float('inf'))
1.7976931348623157e+308
>>>np.nan_to_num(float('nan'))
0.0
>>>np.nan_to_num(float('-inf'))
-1.7976931348623157e+308

Solution 2:

You can just check if your variable of choice is NaN with math.isnan. If it is, change it to the number of your choice. Like this:

>>>import math>>>x = float("nan")>>>x
nan
>>>if math.isnan(x):...    x = 123# just an example...>>>x
123

Post a Comment for "Nan To Num Python"