-1

I am implementing simple linear regression and multiple linear regression using pandas and sklearn

My code is as follows

import pandas as pd
import numpy as np
import scipy.stats
from sklearn import linear_model
from sklearn.metrics import r2_score
df = pd.read_csv("Auto.csv", na_values='?').dropna()

lr = linear_model.LinearRegression()
y = df['mpg']
x = df['displacement']
X = x.values.reshape(-1,1)
sklearn_model = lr.fit(X,y)

This works fine, but for multiple linear regression, for some reason it doesn't work WITH the () at the end of sklearn's linear regression, when I use it with the brackets I get the following error:

TypeError: 'LinearRegression' object is not callable

My multiple linear regression code is as follows:

lr = linear_model.LinearRegression

feature_1 = np.array(df[['displacement']])
feature_2 = np.array(df[['weight']])
feature_1 = feature_1.reshape(len(feature_1),1)
feature_2 = feature_2.reshape(len(feature_2),1)

X = np.hstack([feature_1,feature_2])

sklearn_mlr = lr(X,df['mpg'])

I want to know what I'm doing wrong. Additionally, I'm not able to print the various attributes in the linear regression method if I don't use the () at the end. e.g.

print(sklearn_mlr.coef_)

Gives me the error:

AttributeError: 'LinearRegression' object has no attribute 'coef_'
Grr
  • 15,553
  • 7
  • 65
  • 85
fashioncoder
  • 79
  • 2
  • 8

3 Answers3

2

Given this snippet:

lr = linear_model.LinearRegression

feature_1 = np.array(df[['displacement']])
feature_2 = np.array(df[['weight']])
feature_1 = feature_1.reshape(len(feature_1),1)
feature_2 = feature_2.reshape(len(feature_2),1)

X = np.hstack([feature_1,feature_2])

sklearn_mlr = lr(X,df['mpg'])

Your issue is that you have not initialized an instance of the LinearRegression class. You need to initialize it like you did in the first example. Then you can use the fit method like so:

lr = linear_model.LinearRegression()

feature_1 = np.array(df[['displacement']])
feature_2 = np.array(df[['weight']])
feature_1 = feature_1.reshape(len(feature_1),1)
feature_2 = feature_2.reshape(len(feature_2),1)

X = np.hstack([feature_1,feature_2])

sklearn_mlr = lr.fit(X,df['mpg'])

Once an instance has been fit it will have the attributes listed in the documentation (e.g. .coef_). As it was you were trying to access .coef of the LogisticRegression class itself.

Grr
  • 15,553
  • 7
  • 65
  • 85
0

lr is a class in your example.

You need to initialize it, and then call .fit(X,df['mpg']) from the instance.

David Dale
  • 10,958
  • 44
  • 73
0

Why not import it as follows:

from sklearn.linear_model import LinearRegression

In my opinion it is much cleaner than what you did. You can then use it like that:

lr = LinearRegression()
Andreas Rau
  • 338
  • 2
  • 11