You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scipy调用stats.linregress报错无intercept_stderr属性如何解决?

错误原因

这个错误由Scipy版本不匹配导致:intercept_stderr 是Scipy 1.7.0及以上版本才为LinregressResult对象新增的属性,你本地环境安装的Scipy版本低于1.7.0,因此调用该属性会触发AttributeError。

修复方案

方案1:升级Scipy版本(推荐)

直接升级到符合要求的Scipy版本即可,执行对应包管理命令:

# pip 安装
pip install --upgrade scipy

# conda 安装
conda update scipy

升级完成后原有代码无需修改即可正常运行。

方案2:手动计算截距标准误(不升级版本适配)

如果不方便升级Scipy,可以基于linregress已返回的参数手动计算截距的标准误,示例代码如下:

from scipy import stats
import numpy as np

# 以第一组回归为例
x = retornos['ineg']
y = retornos['Modelo']
linregress = stats.linregress(x, y)

n = len(x)
x_mean = np.mean(x)
# 计算x的总平方和
ss_x = np.sum((x - x_mean)**2)
# 计算残差
y_pred = linregress.intercept + linregress.slope * x
resid = y - y_pred
# 残差均方误
mse = np.sum(resid**2) / (n - 2)
# 手动计算截距标准误
intercept_stderr = np.sqrt(mse * (1/n + x_mean**2 / ss_x))

# 计算t值
T = linregress.intercept / intercept_stderr

其余两组回归复用上述计算逻辑,即可得到对应的T值。

内容的提问来源于stack exchange,提问作者Juan Torcuatto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 05:54:04