获得形状不对齐错误sklearn。

问题描述 投票:1回答:1
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
Dataset = pd.read_csv('Salary_Data.csv')
Salary , YearsExperience = Dataset['Salary'] ,Dataset['YearsExperience'] 
X_train, X_test, y_train, y_test = train_test_split(YearsExperience , 
Salary, test_size=0.33, random_state=42)
Regressor = LinearRegression()
Regressor.fit(X_train.values.reshape(1,-1),y_train.values.reshape(1,-1))    
y_pred = Regressor.predict(X_test.values.reshape(1,-1))

所以我写了这段代码做了线性回归。但是我在错误说明的.predict行上收到错误

ValueError: shapes (1,10) and (20,20) not aligned: 10 (dim 1) != 20 (dim 0)

但是当我将test_size保持为0.5时,不会发生错误。你能解释一下为什么会这样吗?我该怎么办 ?

python machine-learning scikit-learn linear-regression sklearn-pandas
1个回答
1
投票

如果你不重塑你的数据sklearn给你一个提示:

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

由于您的数据只有一个功能,您必须将其重新整形为(-1,1)而不是(1,-1)

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