【问题标题】:Linear regression with Y values containing NAN using scipy使用 scipy 包含 NAN 的 Y 值的线性回归
【发布时间】:2015-11-05 16:15:43
【问题描述】:

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

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.

我该如何解决这个问题?

【问题讨论】:

  • 你能插入你的 nans 吗?
  • 你不能简单地排除 nan 值吗?我看不到它们为您的模型贡献了哪些信息。

标签: python arrays numpy scipy linear-regression


【解决方案1】:

屏蔽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)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 2020-06-10
  • 2017-10-02
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
相关资源
最近更新 更多