提问人:wdl4076 提问时间:5/11/2021 最后编辑:bad_coderwdl4076 更新时间:5/12/2021 访问量:265
使用 sklearn 在 Python 中出现多元线性回归错误 [已关闭]
Error in Multiple Linear Regression in Python with sklearn [closed]
问:
我正在尝试对数据集执行多元线性回归。我已经准备好了数据集,train_test_split完成,当我尝试将模型拟合到线性回归器时,出现以下错误:
我还附上了下面的代码。请看一下并帮助我解决错误。
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
dataset = pd.read_csv('50_Startups.csv');
dataset.head()
x = dataset.iloc[:,:-1]
y = dataset.iloc[:,:4]
states = pd.get_dummies(x['State'], drop_first=True)
states.head()
x = x.drop('State', axis=1)
x.head()
x = pd.concat([x, states], axis=1)
from sklearn.model_selection import train_test_split
x_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_train, y_train)
答:
0赞
Adarsh Wase
5/11/2021
#1
您收到错误是因为您选择了错误的 Y 值(目标值)。 这将起作用 -
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
dataset = pd.read_csv('50_Startups.csv');
dataset.head()
x = dataset.iloc[:,:-1]
y = dataset['Profit']
x = pd.get_dummies(dataset, prefix=['State'])
from sklearn.model_selection import train_test_split
x_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_train, y_train)
评论
1赞
wdl4076
5/11/2021
这也行得通,但是,我自己已经解决了这个错误。它未正确附加新编码的状态列。无论如何,谢谢。
下一个:.Net 6 中的新遗物配置
评论
drop_first