使用 scipy 对包含 NAN 的 Y 值进行线性回归

Linear regression with Y values containing NAN using scipy

我有两个一维数组,我想做一些线性回归。 我用过:

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

但斜率和截距总是 NAN,NAN。我读了一点,发现如果 x 或 y 有一些 NAN,那就是预期的结果。我试过 solution 但它不起作用,因为在我的例子中,只有 y 包含一些 NAN;不是 x。所以使用那个解决方案,我有错误: ValueError: all the input array dimensions except for the concatenation axis must match exactly.

我该如何解决这个问题?

屏蔽 xy 中的值,y 中有 NaN:

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)