r2 分数结果为负

r2 score turns out to be negative

我研究支持向量回归,但我遇到了一个问题:我的 r2 分数变为负数。这是正常的还是我的代码中有任何可更改的部分来解决这个问题?

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)

我的输出是-0.5313206322807349

来自 sklearn.metrics.r2_scoredocumentation

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.

根据 documentation:

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

这部分,你的X是缩小版的

X = x_scaler.fit_transform(X)

在这部分,你的x_test也是缩放版

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

创建预测时,您不应再次转换输入,因为您的 x_test 已经是缩放版本

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