我一直在try 通过线性回归来拟合这些数据,遵循关于bigdataexaminer的教程.到目前为止,一切都很顺利.我从sklearn导入了线性回归,并很好地打印了系数的数量.这是我试图从控制台获取系数之前的代码.
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
然而,现在如果我再次try 打印同一行,或者使用"lm".coef_u'告诉我coef_u不是LinearRegression的属性,就在我刚刚成功使用它之后,在我再次try 之前,我没有接触任何代码.
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_'