Here is my code:
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))
Error:
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)
I reshaped the two variables (y_pred_lassocv_reshape, y_test_reshape) so that they were both 2D arrays with the same number of columns and rows so I have no idea why I'm getting this error. Any ideas?
The score
method of LassoCV()
should be used as score(X, y, sample_weight=None)
where X
is basically x_test and y
would be y_test
. It makes the prediction and scoring:
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