import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import Lasso, LinearRegression
from sklearn.metrics.regression import r2_score
np.random.seed(0)
n = 15
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
def regression_score():
polynomial_features= PolynomialFeatures(degree=12)
x_train_poly = polynomial_features.fit_transform(X_train.reshape(-1,1))
x_test_poly = polynomial_features.fit_transform(X_test.reshape(-1,1))
model = LinearRegression()
model.fit(x_train_poly, y_train)
test_pred_linear_regression = model.predict(x_test_poly)
LinearRegression_R2_test_score = model.score(y_test, test_pred_linear_regression)
regression_score()
Whenever I run the above code, I get the following error
ValueError: Expected 2D array, got 1D array instead:
array=[ 0.99517935 -0.16081 0.3187423 1.53763897].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
But if I try to find out the shape for y_test and test_pred_linear_regression both of them provide same shape (4,)
. But whenever I use r2_score instead of score method I get the desired result.
LinearRegression_R2_test_score = r2_score(y_test, test_pred_linear_regression)
Can somebody point out what i am missing here??