16 Oct 2023

Negative MSE

from sklearn.svm import SVR

SVM_reg = SVR()
param_grid = [
        {'kernel': ['linear'], 'C': [10., 30., 100., 300., 1000., 3000.]},
        {'kernel': ['rbf'], 'C': [1.0, 3.0, 10., 30., 100., 300., 1000.0],
         'gamma': [0.01, 0.03, 0.1, 0.3, 1.0, 3.0]},
    ]

grid_search = GridSearchCV(SVM_reg, param_grid, cv=5,
                            scoring='neg_mean_squared_error',
                            verbose=2)
# grid_search.fit(housing_prepared, housing_labels)

negative_mse = grid_search.best_score_
rmse = np.sqrt(-negative_mse)
rmse

Why is the MSE negative in this case?

The scoring parameter expects a utility function, where higher values indicate better performance. However, mean squared error (MSE) is an error metric, where lower values indicate better performance. To convert the MSE into a utility function, the negative value is used.

By using negative MSE as the scoring metric, the grid search algorithm will try to maximize the negative MSE, which is equivalent to minimizing the MSE.