python - 属性错误: 'OLSResults' object has no attribute 'norm_resid'

标签 python numpy statsmodels

当我运行此程序时,出现以下错误:

AttributeError: 'OLSResults' object has no attribute 'norm_resid'

我有最新版本的 OLS,因此属性 norm_resid 应该在那里。

有什么想法吗?

from scipy import stats
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from sklearn import datasets, linear_model
from statsmodels.formula.api import ols
"""
Data Management
"""
data = pd.read_csv("TestExer1-sales-round1.csv")
X_train = data["Advertising"]
Y_train  = data["Sales"]


# use of linregregress
model = ols("Y_train ~ X_train", data).fit()
print(model.summary()) 

plt.plot(X_train,Y_train , 'ro')
plt.plot(X_train, model.fittedvalues, 'b')
plt.legend(['Sales', 'Advertising'])
plt.ylim(0, 70)
plt.xlim(5, 18)
plt.hist(model.norm_resid())
plt.ylabel('Count')
plt.xlabel('Normalized residuals')

plt.xlabel('Temperature')
plt.ylabel('Gas')
plt.title('Before Insulation')

最佳答案

我遇到了同样的问题,但以下方法有效:

plt.hist(model.resid_pearson)

因此您的解决方案应如下所示:

from scipy import stats
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from sklearn import datasets, linear_model
from statsmodels.formula.api import ols
"""
Data Management
"""
data = pd.read_csv("TestExer1-sales-round1.csv")
X_train = data["Advertising"]
Y_train  = data["Sales"]


# use of linregregress
model = ols("Y_train ~ X_train", data).fit()
print(model.summary()) 

plt.plot(X_train,Y_train , 'ro')
plt.plot(X_train, model.fittedvalues, 'b')
plt.legend(['Sales', 'Advertising'])
plt.ylim(0, 70)
plt.xlim(5, 18)
plt.hist(model.resid_pearson)
plt.ylabel('Count')
plt.xlabel('Normalized residuals')

plt.xlabel('Temperature')
plt.ylabel('Gas')
plt.title('Before Insulation')

当使用 statsmodel 版本 0.8.0 或更高版本时。

注意: PIL 逊残差仅将每个残差值除以残差的标准误差。而归一化还将每个残差除以所有残差的总和。更多信息请参见here

来自docs .

关于python - 属性错误: 'OLSResults' object has no attribute 'norm_resid' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37327509/

相关文章:

python - 查找一个序列大于另一个序列的索引

python - 如何仅使用 numpy(而不是 sklearn LabelEncoder)创建标签编码器?

python - 如何将列中的 k-means 预测聚类添加到 Python 中的数据框

python - 将 pyCurl XML 服务器响应放入变量 (Python)

python - 模块在python3中没有属性错误

numpy 矩阵跟踪行为

python - Statsmodels.api 中的协方差类型是什么意思?

python - Python中的方差膨胀因子

python - statsmodels 无法使用诸如登录异构类型行之类的函数来预测公式

python - 从 ePassport 读取 EF.COM 在 READ BINARY 处失败,显示 rAPDU 6988(安全消息数据对象不正确)