user3601754 user3601754 - 3 months ago 10x
Python Question

Python - Scipy linear regression with nan values

I would like to obtain the slopes of the linear regression of my data, but the Y contains some nan values...thus it perturbs linregress function...
For example :

from scipy import stats
import numpy as np

X = np.array([0,1,2,3,4,5])
Y = np.array([np.NaN,4, 5, 10, 2, 5])

But, I obtain : (nan, nan, nan, nan, nan)
Thus i try to mask invalid values as you can see:

import as ma

But it s the same...I dont see what i have to do...


Try the following:


upd: as Warren noticed, Y will be updated, so the nans are gone. You can feed Y[np.logical_not(np.isnan(Y))] and X=X[np.logical_not(np.isnan(Y))] directly into linear regression. Or see Warren's answer with np.isfinite