scikit-learn cross validation, negative values with mean squared error

PythonRegressionScikit LearnCross Validation

Python Problem Overview


When I use the following code with Data matrix X of size (952,144) and output vector y of size (952), mean_squared_error metric returns negative values, which is unexpected. Do you have any idea?

from sklearn.svm import SVR
from sklearn import cross_validation as CV

reg = SVR(C=1., epsilon=0.1, kernel='rbf')
scores = CV.cross_val_score(reg, X, y, cv=10, scoring='mean_squared_error')

all values in scores are then negative.

Python Solutions


Solution 1 - Python

Trying to close this out, so am providing the answer that David and larsmans have eloquently described in the comments section:

Yes, this is supposed to happen. The actual MSE is simply the positive version of the number you're getting.

The unified scoring API always maximizes the score, so scores which need to be minimized are negated in order for the unified scoring API to work correctly. The score that is returned is therefore negated when it is a score that should be minimized and left positive if it is a score that should be maximized.

This is also described in sklearn GridSearchCV with Pipeline.

Solution 2 - Python

You can fix it by changing scoring method to "neg_mean_squared_error" as you can see below:

from sklearn.svm import SVR
from sklearn import cross_validation as CV

reg = SVR(C=1., epsilon=0.1, kernel='rbf')
scores = CV.cross_val_score(reg, X, y, cv=10, scoring='neg_mean_squared_error')

Solution 3 - Python

To see what are available scoring keys use:

import sklearn
print(sklearn.metrics.SCORERS.keys())

You can either use 'r2' or 'neg_mean_squared_error'. There are lots of options based on your requirement.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionahmethungariView Question on Stackoverflow
Solution 1 - PythonAN6U5View Answer on Stackoverflow
Solution 2 - PythonOtacílio MaiaView Answer on Stackoverflow
Solution 3 - PythonMD RijwanView Answer on Stackoverflow