【发布时间】:2021-10-09 03:40:48
【问题描述】:
这是我的代码:
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),使它们都是具有相同列数和行数的二维数组,所以我不知道为什么会出现这个错误。有什么想法吗?
【问题讨论】:
标签: python linear-regression reshape sklearn-pandas lasso-regression