close

[Solved] AttributeError: LinearRegression object has no attribute ‘coef_’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError: LinearRegression object has no attribute ‘coef_’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AttributeError: LinearRegression object has no attribute ‘coef_’ Error Occurs?

Today I get the following error AttributeError: LinearRegression object has no attribute ‘coef_’ in python.

How To Solve AttributeError: LinearRegression object has no attribute ‘coef_’ Error ?

  1. How To Solve AttributeError: LinearRegression object has no attribute 'coef_' Error ?

    To Solve AttributeError: LinearRegression object has no attribute 'coef_' Error The coef_ attribute is created when the fit() method is called. Before that, it will be undefined:

  2. AttributeError: LinearRegression object has no attribute 'coef_'

    To Solve AttributeError: LinearRegression object has no attribute 'coef_' Error The coef_ attribute is created when the fit() method is called. Before that, it will be undefined:

Solution 1

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read