我试图通过简单的回归来预测答案,但出现以下错误:
('形状(1,151)和(603,603)不对齐:151(dim 1)!= 603(dim 0)')
这是我的代码
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset`enter code here`
dataset = pd.read_csv('pure_cotton.csv')
X = dataset.iloc[:,7].values
y = dataset.iloc[:,10].values
# Splitting the dataset into the Training set and Test set
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)
#fitting simple_linear_reg to training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit([X_train], [y_train])
#predicting the test results
y_pred = regressor.predict([X_test])
答案 0 :(得分:0)
您应该做一些事情:
regressor.fit(np.expand_dims(X_train, 1), np.expand_dims(y_train, 1))
还有
regressor.predict(np.expand_dims(X_test, 1))
为避免尺寸问题,第一维代表样本数量,第二维代表要素数量
答案 1 :(得分:0)
您只需从.values
和X
中删除y
。您不必将它们转换为numpy数组。但是,如果使用numpy数组,则可能必须使用np.reshape()
或np.expand_dims()
来更改数组的尺寸。