0
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??

Tanjim Ahmed Khan
  • 650
  • 1
  • 9
  • 21
Emon
  • 63
  • 9
  • try `y_test=y_test.reshape(-1,1)` , `test_pred_linear_regression=test_pred_linear_regression.reshape(-1,1)` before running score, see if it helps. – Ehsan May 16 '20 at 09:39
  • I tried to reshape the value inside the score method before,still got the same error. After following your suggestion i get error that local variable is referenced before assignment – Emon May 16 '20 at 09:44
  • `that local variable is referenced before assignment` means you did not put the code in the right place. try it right before `LinearRegression_R2_test_score = model.score(y_test, test_pred_linear_regression)` – Ehsan May 16 '20 at 10:30
  • I did try as you mentioned but still getting the same error, As i mentioned in my first comment i tried,`LinearRegression_R2_test_score = lasso.score(y_test.reshape(-1,1),test_pred_linear_regression.reshape(-1,1)` which arises `ValueError: shapes (4,1) and (13,) not aligned: 1 (dim 1) != 13 (dim 0) ` – Emon May 16 '20 at 11:05
  • worth a shot. I am not aware of details of the methods. I would recommend reading their doc for input requirements. – Ehsan May 16 '20 at 11:06

1 Answers1

-1

This simply has to do with what each function expects as input. The score method of an estimator object in scikit-learn expects the test input (x_test_poly) and the corresponding true value (y_test) as input. The r2_score function expects the predicted values y_pred and the corresponding true values (y_test) as input.

r2_score

LinearRegression

Hope that helps!

EDIT: The correct code is

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(x_test_poly,y_test)
    r2 = r2_score(y_test,test_pred_linear_regression)
    return LinearRegression_R2_test_score, r2

linearreg, r2 = regression_score() # -4.311980555741178, -4.311980555741178
Niklas Höpner
  • 364
  • 2
  • 4
  • [difference-between-model-score-vs-r2-score](https://stackoverflow.com/questions/45529907/difference-between-model-score-vs-r2-score) . Both of them actually do the same thing,i do not understand the array reshaping part for the score method – Emon May 16 '20 at 09:48
  • You don't need to reshape anything! The score method does not take the predicted values as input. I added the correct code to the answer. – Niklas Höpner May 16 '20 at 17:31