AttributeError: LinearRegression object has no attribute 'coef_'

67,128

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.

Share:
67,128

Related videos on Youtube

Destroxia
Author by

Destroxia

I am a student at Penn State University enrolled in a dual-major of physics and mathematics. I am here to learn more about coding in python, in order to run tests to validate the theoretical with experimental data. I also like to code for fun, and find data analysis very intriguing, and love attempting things like scraping data from websites, and creating programs that make things I like doing easier. "What I cannot create, I do not understand" - Richard Feynman

Updated on July 09, 2022

Comments

  • Destroxia
    Destroxia almost 2 years

    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_'
    
    • ayhan
      ayhan almost 8 years
      Where do you call the fit method? With only the part you shared, len(lm.coef_) cannot print 13.
    • Destroxia
      Destroxia almost 8 years
      I never called a fit method, but I can promise you, the first time I ran that line print('Number of coefficients:', len(lm.coef_)) it definitely returned 13. I'm not sure if its a python 3 issue or whatnot, but it did print that the first time.
    • user1157751
      user1157751 almost 8 years
      @Destroxia If you did not fit the function, how is there a coefficient???
    • user1157751
      user1157751 almost 8 years
      @Destroxia Essentially you are trying to solve m in y=mx+c, and the m is your coefficient.
    • ayhan
      ayhan almost 8 years
      What's there in between 68 and 70? I guess something like runfile(...)?
    • Destroxia
      Destroxia almost 8 years
      Yes, I was just recompiling the code.
  • Destroxia
    Destroxia almost 8 years
    Thank you, this seemed to fix the problem, although I'm not sure how it worked the first time without the fit.