线性回归模型预测函数不起作用[重复项]

问题描述 投票:0回答:1
我有此代码。在我尝试使用predict(x-value)获得y-value答案之前,它基本上一直有效。

下面是代码。

import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression df = pd.read_csv('linear_data.csv') x = df.iloc[:,:-1].values y = df.iloc[:,1:].values x_train, x_test, y_train, y_test= train_test_split(x,y,test_size=1/3,random_state=0) reg = LinearRegression() reg.fit(x_train, y_train) y_predict = reg.predict(x_test) y_predict_res = reg.predict(11) --> #This is the error! 11 is the number of years to predict the salary print(y_predict_res)

我得到的错误是:

[ValueError:预期的2D数组,改为标量数组:array = 11。如果您的数据有一个数组,请使用array.reshape(-1,1)重塑数据。单个要素或array.reshape(1,-1)(如果包含单个样本)。

错误消息对我没有多大帮助,因为我不明白为什么需要重塑它。

python scikit-learn linear-regression data-science valueerror
1个回答
1
投票
请注意here,期望的参数X是

类似数组或稀疏矩阵,形状(n_samples,n_features)] >>,这意味着它不能是单个数字。数字/值必须是数组的一部分。

© www.soinside.com 2019 - 2024. All rights reserved.