【发布时间】:2016-12-03 10:00:12
【问题描述】:
按照关于 bigdataexaminer 的教程,我一直在尝试通过线性回归拟合这些数据。到目前为止,一切都运行良好。我从 sklearn 导入了 LinearRegression,并很好地打印了系数的数量。这是我尝试从控制台获取系数之前的代码。
import numpy as np
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt
import sklearn
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
boston = load_boston()
bos = pd.DataFrame(boston.data)
bos.columns = boston.feature_names
bos['PRICE'] = boston.target
X = bos.drop('PRICE', axis = 1)
lm = LinearRegression()
完成所有这些设置后,我运行了以下命令,它返回了正确的输出:
In [68]: print('Number of coefficients:', len(lm.coef_)
Number of coefficients: 13
但是,现在如果我再次尝试打印同一行,或者使用“lm.coef_”,它会告诉我 coef_ 不是 LinearRegression 的属性,就在我成功使用它之后,我没有在我再次尝试之前触摸任何代码。
In [70]: print('Number of coefficients:', len(lm.coef_))
Traceback (most recent call last):
File "<ipython-input-70-5ad192630df3>", line 1, in <module>
print('Number of coefficients:', len(lm.coef_))
AttributeError: 'LinearRegression' object has no attribute 'coef_'
【问题讨论】:
-
你在哪里调用 fit 方法?仅使用您共享的部分,len(lm.coef_) 无法打印 13。
-
我从来没有调用过 fit 方法,但我可以向你保证,当我第一次运行
print('Number of coefficients:', len(lm.coef_))时,它肯定返回了 13。我不确定它是 python 3 问题还是其他问题,但它确实是第一次打印出来。 -
@Destroxia 如果没有拟合函数,怎么有系数???
-
@Destroxia 本质上,您是在尝试在 y=mx+c 中求解 m,而 m 是您的系数。
-
68 和 70 之间有什么?我猜像
runfile(...)?
标签: python python-3.x scikit-learn linear-regression attributeerror