user3841581
user3841581

Reputation: 2747

Linear regression with Y values containing NAN using scipy

I have two one dimension arrays and I would like to do some linear regression. I used:

slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)

but the slope and intercept are always NAN, NAN. I read a little bit and I found out that if either x or y has some NAN, that is the results expected. I tried this solution but it doesnt work because, in my case, only the y contains some NANs; not x. So using that solution, I have the error: ValueError: all the input array dimensions except for the concatenation axis must match exactly.

How can i fix this issue?

Upvotes: 0

Views: 1960

Answers (1)

tmdavison
tmdavison

Reputation: 69223

mask the values in both x and y for which there is a NaN in y:

xm = np.ma.masked_array(x,mask=np.isnan(y)).compressed()
ym = np.ma.masked_array(y,mask=np.isnan(y)).compressed()

slope, intercept, r_value, p_value, std_err = stats.linregress(xm, ym)

Upvotes: 5

Related Questions