简体   繁体   English

多特征线性回归的实现

[英]Implementation of multiple feature linear regression

I have a train_data which holds information about Stores and their sales.我有一个 train_data 保存有关商店及其销售的信息。 Which looks like this看起来像这样在此处输入图片说明

I want to build a multiple feature linear regression to predict the 'Sales' on a test_data, by using 'DayofWeek', 'Customers', 'Promo'.我想通过使用“DayofWeek”、“Customers”、“Promo”来构建多特征线性回归来预测 test_data 上的“销售额”。

How do I build a Multiple Linear Regression Model for this, preferably by using SKlearn.我如何为此构建多元线性回归模型,最好使用 SKlearn。

edit: here's the link to the dataset I am using, if anyone is interested : https://www.kaggle.com/c/rossmann-store-sales编辑:这是我正在使用的数据集的链接,如果有人感兴趣: 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'.为此,我收到一条错误消息,提示“未定义线性回归”。

You aren't actually importing the LinearRegression class.您实际上并未导入LinearRegression类。 If you want to import everything in the linear_model module (which is generally frowned upon) you could do:如果您想导入linear_model模块中的所有内容(通常linear_model ),您可以执行以下操作:

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

You've imported linear_model, which is the module that contains the LinearRegression() class.您已经导入了 linear_model,它是包含 LinearRegression() 类的模块。 To call the LinearRegression class use this:要调用 LinearRegression 类,请使用以下命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM