AttributeError: LinearRegression 对象没有属性“coef_" [英] AttributeError: LinearRegression object has no attribute 'coef_'

查看:55
本文介绍了AttributeError: LinearRegression 对象没有属性“coef_"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 bigdataexaminer 教程,我一直在尝试通过线性回归拟合这些数据.到目前为止,一切都运行良好.我从 sklearn 导入了 LinearRegression,并很好地打印了系数的数量.这是我尝试从控制台获取系数之前的代码.

将 numpy 导入为 np将熊猫导入为 pd导入 scipy.stats 作为统计信息导入 matplotlib.pyplot 作为 plt导入sklearn从 sklearn.datasets 导入 load_boston从 sklearn.linear_model 导入 LinearRegression波士顿 = load_boston()bos = pd.DataFrame(boston.data)bos.columns = boston.feature_namesbos['PRICE'] = boston.targetX = bos.drop('价格', 轴 = 1)lm = 线性回归()

完成所有这些设置后,我运行了以下命令,它返回了正确的输出:

In [68]: print('系数个数:', len(lm.coef_)系数数:13

但是,现在如果我再次尝试打印同一行,或使用lm.coef_",它会告诉我 coef_ 不是 LinearRegression 的属性,就在我刚刚成功使用它之后,我没有在我再次尝试之前触摸任何代码.

In [70]: print('系数个数:', len(lm.coef_))回溯(最近一次调用最后一次):文件<ipython-input-70-5ad192630df3>",第 1 行,在 <module> 中打印('系数的数量:',len(lm.coef_))AttributeError: 'LinearRegression' 对象没有属性 'coef_'

解决方案

coef_ 属性是在调用 fit() 方法时创建的.在此之前,它将是未定义的:

<预><代码>>>>将 numpy 导入为 np>>>将熊猫导入为 pd>>>从 sklearn.datasets 导入 load_boston>>>从 sklearn.linear_model 导入 LinearRegression>>>波士顿 = load_boston()>>>lm = 线性回归()>>>lm.coef_---------------------------------------------------------------------------AttributeError 回溯(最近一次调用最后一次)<ipython-input-22-975676802622>在 <module>()78 lm = 线性回归()---->9 lm.coef_AttributeError: 'LinearRegression' 对象没有属性 'coef_'

如果我们调用fit(),将定义系数:

<预><代码>>>>lm.fit(波士顿数据,波士顿目标)>>>lm.coef_数组([-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().

I've been attempting to fit this data by a Linear Regression, following a tutorial on bigdataexaminer. Everything was working fine up until this point. I imported LinearRegression from sklearn, and printed the number of coefficients just fine. This was the code before I attempted to grab the coefficients from the console.

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()

After I had all this set up I ran the following command, and it returned the proper output:

In [68]: print('Number of coefficients:', len(lm.coef_)

Number of coefficients: 13

However, now if I ever try to print this same line again, or use 'lm.coef_', it tells me coef_ isn't an attribute of LinearRegression, right after I JUST used it successfully, and I didn't touch any of the code before I tried it again.

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_'

解决方案

The coef_ attribute is created when the fit() method is called. Before that, it will be undefined:

>>> 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_'

If we call fit(), the coefficients will be defined:

>>> 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])

My guess is that somehow you forgot to call fit() when you ran the problematic line.

这篇关于AttributeError: LinearRegression 对象没有属性“coef_"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆