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

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

问题描述

我有两个数组,比如说varx和variant.两者在不同位置都包含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天全站免登陆