ValueError:x和y的大小必须相同

问题描述 投票:0回答:1

我有一个数据集,我正在尝试使用sklearn计算线性回归。我正在使用的数据集已经制作完毕,因此不会有任何问题。我已经使用train_test_split来将我的数据分为训练和测试组。当我尝试使用matplotlib来创建我的ttest和预测组之间的散点图时,出现下一个错误:

ValueError:x和y的大小必须相同

这是我的代码:

y=data['Yearly Amount Spent']
x=data[['Avg. Session Length','Time on App','Time on Website','Length of Membership','Yearly Amount Spent']]
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=101)

#training the model

from sklearn.linear_model import LinearRegression
lm=LinearRegression()
lm.fit(x_train,y_train)
lm.coef_

predictions=lm.predict(X_test)

#here the problem starts:

plt.scatter(y_test,predictions)

为什么会出现此错误?我在这里看到过以前的帖子,建议f或者是使用x.shape和y.shape的,但是我不确定这样做的目的是什么。

谢谢

python matplotlib scikit-learn linear-regression
1个回答
-1
投票

x和y必须具有相同数量的示例(x.shapey.shape中的第一个数字)。对于每个x_i,应该有一个标签y_i。您的训练测试拆分功能可能存在问题。如注释中所述,问题也可能与分散的阵列有关。希望这可以帮助 !

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