简体   繁体   中英

r2 score turns out to be negative

I study support vector regression but I faced a problem: my r2 score becomes negative. Is that normal or is there any changeable part in my code to fix this?

import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt
from sklearn.svm import SVR
df = pd.read_csv('Position_Salaries.csv')
df.head()
X = df.iloc[:, 1:2].values
y = df.iloc[:, -1].values
from sklearn.preprocessing import StandardScaler
y = y.reshape(len(y),1)
x_scaler = StandardScaler()
y_scaler = StandardScaler()
X = x_scaler.fit_transform(X)
y = y_scaler.fit_transform(y)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state = 42)
regressor = SVR(kernel="rbf")
regressor.fit(x_train,y_train.ravel())
y_pred = y_scaler.inverse_transform(regressor.predict(x_scaler.transform(x_test)))
from sklearn.metrics import r2_score
r2_score(y_scaler.inverse_transform(y_test), y_pred)

My output is -0.5313206322807349

From the documentation of sklearn.metrics.r2_score .

Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a R^2 score of 0.0.

In this part, your X is in scaled version

X = x_scaler.fit_transform(X)

In this part, your x_test also in scaled version

x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state = 42)

When creating prediction, you shouldn't transform your input again since your x_test already in scaled version

y_pred = y_scaler.inverse_transform(regressor.predict(x_scaler.transform(x_test)))

Per documentation :

Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM