Girish Mallya
Girish Mallya

Reputation: 65

Implementation of multiple feature linear regression

I have a train_data which holds information about Stores and their sales. Which looks like this enter image description here

I want to build a multiple feature linear regression to predict the 'Sales' on a test_data, by using 'DayofWeek', 'Customers', 'Promo'.

How do I build a Multiple Linear Regression Model for this, preferably by using SKlearn.

edit: here's the link to the dataset I am using, if anyone is interested : https://www.kaggle.com/c/rossmann-store-sales

This is what i've tried so far.

import pandas as pd

from sklearn import linear_model

x=train_data[['Promo','Customers','DayOfWeek']]

y=train_data['Sales']

lm=LinearRegression()


lm.fit(x,y)

For which i am getting an error saying 'LinearRegression not defined'.

Upvotes: 1

Views: 1397

Answers (2)

Grr
Grr

Reputation: 16079

You aren't actually importing the LinearRegression class. If you want to import everything in the linear_model module (which is generally frowned upon) you could do:

from sklearn.linear_model import *
lr = LinearRegression()
...

A better practice is to import the module itself and give it an alias. Like so:

import sklearn.linear_model as lm
lr = lm.LinearRegression()
...

Finally you could import just the class you want:

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
...

Upvotes: 1

amanbirs
amanbirs

Reputation: 1108

You've imported linear_model, which is the module that contains the LinearRegression() class. To call the LinearRegression class use this:

lm = linear_model.LinearRegression()
lm.fit(x,y)

Upvotes: 0

Related Questions