根据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()
方法时,将创建coef_
属性。在此之前,它将是未定义的:如果我们调用
fit()
,将定义系数:我的猜测是,当你运行有问题的线路时,不知怎么的,你忘记给
fit()
打电话了。相关问题 更多 >
编程相关推荐