python - 使用 .score() 方法时出错 : shapes (10719, 1) 和 (16,1) 未对齐 : 1 (dim 1) ! = 16 (dim 0)

标签 python machine-learning error-handling scikit-learn linear-regression

我正在尝试在拟合的线性回归器上使用 .score() 方法,但出现错误。

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.metrics import mean_squared_error

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, 
random_state = 104)
reg = LinearRegression()
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)
print("R^2: {}".format(reg.score(X_test, y_test)))
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print("Root Mean Squared Error: {}".format(rmse))
reg.score(y_test.reshape(-1,1), y_pred.reshape(-1,1))

ValueError: shapes (10719,1) and (16,1) not aligned: 1 (dim 1) != 16 (dim 0)

我应该提到我已经尝试 reshape y_pred 和 y_test 以使它们匹配,但它仍然不起作用。我不知道为什么错误说 (16,1);这些尺寸是干什么用的?我曾尝试搜索类似的问题,例如:Error using sklearn and linear regression: shapes (1,16) and (1,1) not aligned: 16 (dim 1) != 1 (dim 0)但我仍然很困惑。

编辑:这是形状的输出:
print(X_test.shape, y_test.shape, y_pred.shape)

(10719, 16) (10719, 1) (10719, 1)

最佳答案

来自 scikit docs , score(X, y, sample_weight=None) ,因此您不会将预测作为第一个参数发送给它。相反,您发送功能。

因此,最后一行应该是 print(reg.score(X_test, y_test))

关于python - 使用 .score() 方法时出错 : shapes (10719, 1) 和 (16,1) 未对齐 : 1 (dim 1) ! = 16 (dim 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51959370/

相关文章:

reactjs - React 错误边界无法与异步函数正常工作

angularjs - 在哪里处理AngularJS后端错误

wpf - 当输入错误时如何显示MessageBox,以及如何在WPF中使用MVVM还原TextBox的旧值

python - 我的登录方法未在我的register()中调用,并且在mainError(0)中出现我的错误(如果不应该这样做)

machine-learning - 使用机器学习识别名字

machine-learning - Weka 中级联分类器的错误方法

apache-spark - 发现没有 H2O 实例的执行器,杀死了云

python - Python 中的 RGB 极坐标图

python - 我们如何使用神经网络计算多类分类器的准确性

python - 在一个线程中接收套接字数据,在另一个线程中写入数据——python