Python/Numpy 中包含 NAN 的数组的线性回归 [英] Linear regression of arrays containing NANs in Python/Numpy

查看:29
本文介绍了Python/Numpy 中包含 NAN 的数组的线性回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组,比如 varx 和变量.两者都在不同位置包含 NAN 值.但是,我想对两者进行线性回归以显示两个数组的相关程度.到目前为止,这非常有帮助:http://glowingpython.blogspot.de/2012/03/linear-regression-with-numpy.html

I have two arrays, say varx and vary. Both contain NAN values at various positions. However, I would like to do a linear regression on both to show how much the two arrays correlate. This was very helpful so far: http://glowingpython.blogspot.de/2012/03/linear-regression-with-numpy.html

但是,使用这个:

slope, intercept, r_value, p_value, std_err = stats.linregress(varx, vary)

为每个输出变量产生 nans.仅将两个数组中的有效值作为线性回归的输入的最方便方法是什么?我听说过屏蔽数组,但不确定它是如何工作的.

results in nans for every output variable. What is the most convenient way to take only valid values from both arrays as input to the linear regression? I heard about masking arrays, but am not sure how it works exactly.

推荐答案

您可以使用掩码移除 NaN:

You can remove NaNs using a mask:

mask = ~np.isnan(varx) & ~np.isnan(vary)
slope, intercept, r_value, p_value, std_err = stats.linregress(varx[mask], vary[mask])

这篇关于Python/Numpy 中包含 NAN 的数组的线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆