我一直在尝试通过线性回归来拟合这些数据,遵循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()
Run Code Online (Sandbox Code Playgroud)
完成所有这些设置后,我运行以下命令,并返回正确的输出:
In [68]: print('Number of coefficients:', len(lm.coef_)
Number of coefficients: 13
Run Code Online (Sandbox Code Playgroud)
但是,现在如果我再次尝试打印同一行,或者使用'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:', …Run Code Online (Sandbox Code Playgroud) python linear-regression attributeerror python-3.x scikit-learn