AttributeError: LinearRegression object has no attribute 'coef_'
AttributeError: LinearRegression object has no attribute 'coef_'
我一直在尝试按照 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_
属性。在此之前,它将是未定义的:
>>> import numpy as np
>>> import pandas as pd
>>> from sklearn.datasets import load_boston
>>> from sklearn.linear_model import LinearRegression
>>> boston = load_boston()
>>> lm = LinearRegression()
>>> lm.coef_
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-22-975676802622> in <module>()
7
8 lm = LinearRegression()
----> 9 lm.coef_
AttributeError: 'LinearRegression' object has no attribute 'coef_'
如果我们调用fit()
,系数将定义为:
>>> lm.fit(boston.data, boston.target)
>>> lm.coef_
array([ -1.07170557e-01, 4.63952195e-02, 2.08602395e-02,
2.68856140e+00, -1.77957587e+01, 3.80475246e+00,
7.51061703e-04, -1.47575880e+00, 3.05655038e-01,
-1.23293463e-02, -9.53463555e-01, 9.39251272e-03,
-5.25466633e-01])
我的猜测是,当您 运行 有问题的行时,您以某种方式忘记了调用 fit()
。
我一直在尝试按照 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_
属性。在此之前,它将是未定义的:
>>> import numpy as np
>>> import pandas as pd
>>> from sklearn.datasets import load_boston
>>> from sklearn.linear_model import LinearRegression
>>> boston = load_boston()
>>> lm = LinearRegression()
>>> lm.coef_
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-22-975676802622> in <module>()
7
8 lm = LinearRegression()
----> 9 lm.coef_
AttributeError: 'LinearRegression' object has no attribute 'coef_'
如果我们调用fit()
,系数将定义为:
>>> lm.fit(boston.data, boston.target)
>>> lm.coef_
array([ -1.07170557e-01, 4.63952195e-02, 2.08602395e-02,
2.68856140e+00, -1.77957587e+01, 3.80475246e+00,
7.51061703e-04, -1.47575880e+00, 3.05655038e-01,
-1.23293463e-02, -9.53463555e-01, 9.39251272e-03,
-5.25466633e-01])
我的猜测是,当您 运行 有问题的行时,您以某种方式忘记了调用 fit()
。