ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 1)

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 1)

这是我的代码:

import numpy as np
from sklearn.linear_model import LassoCV
from sklearn.model_selection import train_test_split

df = pd.read_csv(dataframe, columns= "X1, X2, Y") 

x = np.asarray(df[['X1', 'X2']])
y = np.asarray(df['Y'])

x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=3, test_size=.2)

r = LassoCV().fit(x_train, y_train)
y_pred_lassocv = r.predict(x_test)

y_pred_lassocv_reshape = y_pred_lassocv.reshape(len(y_pred_lassocv), 1) #reshaping to 2D array because that's what this the .score() method below requires
y_test_reshape = y_test.reshape(len(y_test), 1) #reshaping to 2D array here as well so the two sets are the same size
print(y_pred_lassocv_reshape.shape)
>>> (100, 1) #results of finding the shape of y_pred_lassocv_reshape
print(y_test_reshape) 
>>> (100, 1) #results of finding the shape y_test_reshape

print("LassoCV Test Accuracy: %.3f" % r.score(y_pred_lassocv_reshape, y_test_reshape))

错误:

ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_29360/2794156765.py in <module>
     65 print(y_test_reshape.shape)
     66 
---> 67 print("LassoCV Test Accuracy: %.3f" % r.score(y_pred_lassocv_reshape, y_test_reshape))

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 1)

我重塑了两个变量(y_pred_lassocv_reshape、y_test_reshape),使它们都是具有相同列数和行数的二维数组,所以我不知道为什么会出现此错误.有什么想法吗?

LassoCV()score 方法应该用作 score(X, y, sample_weight=None) 其中 X 基本上是 x_test 而 y 将是 y_test。它做出预测和评分:

from sklearn.datasets import make_regression
X, y = make_regression( n_features=2, random_state=0,noise=50)
x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=3, test_size=.2)

r = LassoCV().fit(x_train, y_train)
r2score = r.score(x_test, y_test)

print("LassoCV Test Accuracy: %.3f" % r2score)
LassoCV Test Accuracy: 0.681