首页 > 解决方案 > Scipy 线性回归不包括`intercept_stderr` 属性

问题描述

我正在尝试使用 scipy 对一组二维点执行线性回归。如文档here所述,适当的调用是

regression_results = scipy.stats.linregress(x_values, y_values)

文档指出该regression_results对象包含以下值:slope, intercept, rvalue, pvalue, stderr, intercept_stderr. 除了最后一个,所有这些都存在。

这些值都存在于字典中,但intercept_stderr根本不是。我终其一生都无法理解为什么。这是我尝试运行的简单代码:

from scipy import stats

# given two lists nmeq_x and nmeq_y... 

result = stats.linregress(nmeq_x, nmeq_y)
print(result.intercept, result.intercept_stderr)

我得到错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-7df9d260a3bb> in <module>
      1 result = stats.linregress(nmeq_x, nmeq_y)
----> 2 print(result.intercept, result.intercept_stderr)

AttributeError: 'LinregressResult' object has no attribute 'intercept_stderr'

标签: pythonscipy

解决方案


这是关于你的 scipy 版本;此功能是在 2020-12-31 发布的 1.6 中添加的。

请注意,文档是这样说的:

为了与旧版本的 SciPy 兼容,返回值的作用类似于长度为 5 的命名元组,包含字段 slope、intercept、rvalue、pvalue 和 stderr

那么,您必须拥有这些旧版本之一。该intercept_stderr字段是最近添加的。将您的 scipy 更新到 1.6。


推荐阅读