You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MSE is negative when returned by cross_val_score #2439
The Mean Square Error returned by sklearn.cross_validation.cross_val_score is always a negative. While being a designed decision so that the output of this function can be used for maximization given some hyperparameters, it's extremely confusing when using cross_val_score directly. At least I asked myself how a the mean of a square can possibly be negative and thought that cross_val_score was not working correctly or did not use the supplied metric. Only after digging in the sklearn source code I realized that the sign was flipped.
This behavior is mentioned in make_scorer in scorer.py, however it's not mentioned in cross_val_score and I think it should be, because otherwise it makes people think that cross_val_score is not working correctly.
Reacted by Sandeep Raju Prabhakar, Eugene Gapon, Tuan Nguyen, 侯奇, Dmytro Lituiev, Cass Petrus, Joseph Nelson, XI, slaqrichi, shgidi and 45 more
Reacted by Konstantinos Nikoloutsos, Eduard Maura i Puig, Shuchen Du, Ashutosh Deoghare, gamorwang, David Mora, Drew, Sasha and julclu
greater_is_better : boolean, default=True
Whether score_func is a score function (default), meaning high is good,
or a loss function, meaning low is good. In the latter case, the scorer
object will sign-flip the outcome of the score_func.
BTW I don't agree that it's a documentation issue. It's cross_val_score should return the value with the sign that matches the scoring name. Ideally the GridSearchCV(*params).fit(X, y).best_score_ should be consistent too. Otherwise the API is very confusing.
I also agree that a change to return the actual MSE without the sign switched would be the way better option.
The scorer object could just store the greater_is_better flag and whenever the scorer is used the sign could be flipped in case it's needed, e.g. in GridSearchCV.
I agree that we have a usability issue here, but I don't fully agree with @ogrisel's solution that we should
return the value with the sign that matches the scoring name
because that's an unreliable hack in the long run. What if someone defines a custom scorer with a name such as mse? What if they do follow the naming pattern but wrap the scorer in a decorator that changes the name?
The scorer object could just store the greater_is_better flag and whenever the scorer is used the sign could be flipped in case it's needed, e.g. in GridSearchCV.
This is what scorers originally did, during development between the 0.13 and 0.14 releases and it made their definition a lot harder. It also made the code hard to follow because the greater_is_better attribute seemed to disappear in the scorer code, only to reappear in the middle of the grid search code. A special Scorer class was needed to do something that ideally, a simple function would do.
I believe that if we want to optimize scores, then they should be maximized. For the sake of user-friendlyness, I think we might introduce a parameter score_is_loss ∈ ["auto", True, False] that only changes the display of scores and can use a heuristic based on the built-in names.
That was a hurried response because I had to get off the train. What I meant by "display" is really the return value from cross_val_score. I think scorers should be simple and uniform and the algorithms should always maximize.
This does introduce an asymmetry between built-in and custom scorers.
I like the score_is_loss solution, or something to that effect.. the sign change to match the scoring name seems hard to maintain could cause problems as @larsmans mentioned
@tdomhan@jaquesgrobler@larsmans Do you know if this applies to r2 as well? I am noticing that the r2 scores returned by GridSearchCV are also mostly negative for ElasticNet, Lasso and Ridge.
r2 is a score function (greater is better), so that should be positive if your model is any good -- but it's one of the few performance metrics that can actually be negative, meaning worse than 0.
What is the consensus on this issue? In my opinion, cross_val_score is an evaluation tool, not a model selection one. It should thus return the original values.
I can fix it in my PR #2759, since the changes I made make it really easy to fix. The trick is to not flip the sign upfront but, instead, to access the greater_is_better attribute on the scorer when doing grid search.
What is the consensus on this issue? In my opinion, cross_val_score is
an evaluation tool, not a model selection one. It should thus return
the original values.
Special case are varying behaviors are a source of problems in software.
I simply think that we should rename "mse" to "negated_mse" in the list
of acceptable scoring strings.
What if someone defines a custom scorer with a name such as mse? What if they do follow the naming pattern but wrap the scorer in a decorator that changes the name?
I don't think that @ogrisel was suggesting to use name matching, just to be consistent with the original metric. Correct me if I'm wrong @ogrisel.
IIRC we discussed this in the sprint (last summer?!) and decided to go with neg_mse (or was it neg-mse) and deprecate all scorers / strings where we have a negative sign now.
Is this still the consensus? We should do that before 0.18 then.
Ping @GaelVaroquaux@agramfort@jnothman@ogrisel@raghavrv
Yes. I totally agree! This also happened to Brier_score_loss, it works perfectly fine using Brier_score_loss, but it gets confusing when it comes from the GridSearchCV, the negative Brier_score_loss returns. At least, it would be better output something like, because Brier_score_loss is a loss (the lower the better), the scoring function here flip the sign to make it negative.
For Regression usecase:
I am getting different results when I use
(1) "cross_val_score" with scoring='neg_mean_squared_error'
and
(2) For the same inputs when when I use "GridSearchCV" and check the 'best_score_'
For Regression models which one is better ?
"cross_val_score" with scoring='neg_mean_squared_error'
(OR)
@pritishban
You're asking a usage question. The issue tracker is mainly for bugs and new features. For usage questions, it is recommended to try Stack Overflow or the Mailing List.
Activity
jaquesgrobler commentedon Sep 12, 2013
You're referring to
in http://scikit-learn.org/stable/modules/generated/sklearn.metrics.make_scorer.html
? (just for reference's sake)
I agree that that it can be more clear in cross_val_score docs
Thanks for reporting
ogrisel commentedon Sep 12, 2013
Indeed we overlooked that issue when doing the Scorer refactoring. The following is very counter-intuitive:
/cc @larsmans
ogrisel commentedon Sep 12, 2013
BTW I don't agree that it's a documentation issue. It's
cross_val_scoreshould return the value with the sign that matches the scoring name. Ideally theGridSearchCV(*params).fit(X, y).best_score_should be consistent too. Otherwise the API is very confusing.tdomhan commentedon Sep 12, 2013
I also agree that a change to return the actual MSE without the sign switched would be the way better option.
The scorer object could just store the
greater_is_betterflag and whenever the scorer is used the sign could be flipped in case it's needed, e.g. inGridSearchCV.larsmans commentedon Sep 13, 2013
I agree that we have a usability issue here, but I don't fully agree with @ogrisel's solution that we should
because that's an unreliable hack in the long run. What if someone defines a custom scorer with a name such as
mse? What if they do follow the naming pattern but wrap the scorer in a decorator that changes the name?This is what scorers originally did, during development between the 0.13 and 0.14 releases and it made their definition a lot harder. It also made the code hard to follow because the
greater_is_betterattribute seemed to disappear in the scorer code, only to reappear in the middle of the grid search code. A specialScorerclass was needed to do something that ideally, a simple function would do.I believe that if we want to optimize scores, then they should be maximized. For the sake of user-friendlyness, I think we might introduce a parameter
score_is_loss∈["auto", True, False]that only changes the display of scores and can use a heuristic based on the built-in names.larsmans commentedon Sep 13, 2013
That was a hurried response because I had to get off the train. What I meant by "display" is really the return value from
cross_val_score. I think scorers should be simple and uniform and the algorithms should always maximize.This does introduce an asymmetry between built-in and custom scorers.
Ping @GaelVaroquaux.
jaquesgrobler commentedon Sep 13, 2013
I like the score_is_loss solution, or something to that effect.. the sign change to match the scoring name seems hard to maintain could cause problems as @larsmans mentioned
tdomhan commentedon Sep 28, 2013
what's the conclusion, which solution should we go for? :)
amelio-vazquez-reina commentedon Oct 23, 2013
@tdomhan @jaquesgrobler @larsmans Do you know if this applies to
r2as well? I am noticing that ther2scores returned byGridSearchCVare also mostly negative forElasticNet,LassoandRidge.larsmans commentedon Oct 23, 2013
R² can be either positive or negative, and negative simply means your model is performing very poorly.
jnothman commentedon Jan 17, 2014
IIRC, @GaelVaroquaux was a proponent of returning a negative number when
greater_is_better=False.larsmans commentedon Jan 17, 2014
r2is a score function (greater is better), so that should be positive if your model is any good -- but it's one of the few performance metrics that can actually be negative, meaning worse than 0.mblondel commentedon Feb 4, 2014
What is the consensus on this issue? In my opinion,
cross_val_scoreis an evaluation tool, not a model selection one. It should thus return the original values.I can fix it in my PR #2759, since the changes I made make it really easy to fix. The trick is to not flip the sign upfront but, instead, to access the
greater_is_betterattribute on the scorer when doing grid search.GaelVaroquaux commentedon Feb 4, 2014
Special case are varying behaviors are a source of problems in software.
I simply think that we should rename "mse" to "negated_mse" in the list
of acceptable scoring strings.
mblondel commentedon Feb 4, 2014
I don't think that @ogrisel was suggesting to use name matching, just to be consistent with the original metric. Correct me if I'm wrong @ogrisel.
34 remaining items
amueller commentedon Aug 2, 2016
IIRC we discussed this in the sprint (last summer?!) and decided to go with
neg_mse(or was itneg-mse) and deprecate all scorers / strings where we have a negative sign now.Is this still the consensus? We should do that before 0.18 then.
Ping @GaelVaroquaux @agramfort @jnothman @ogrisel @raghavrv
agramfort commentedon Aug 2, 2016
raghavrv commentedon Aug 2, 2016
It was
neg_mseogrisel commentedon Aug 27, 2016
We also need:
neg_log_lossneg_mean_absolute_errorneg_median_absolute_errorshreyassks commentedon Oct 29, 2018
model = Sequential()
keras.layers.Flatten()
model.add(Dense(11, input_dim=3, kernel_initializer = keras.initializers.he_normal(seed = 2),
kernel_regularizer = regularizers.l2(2)))
keras.layers.LeakyReLU(alpha = 0.1)
model.add(Dense(8, kernel_initializer = keras.initializers.he_normal(seed = 2)))
keras.layers.LeakyReLU(alpha = 0.1)
model.add(Dense(4, kernel_initializer = keras.initializers.he_normal(seed = 2)))
keras.layers.LeakyReLU(alpha = 0.1)
model.add(Dense(1, kernel_initializer = keras.initializers.he_normal(seed = 2)))
keras.layers.LeakyReLU(alpha = 0.2)
adag = RMSprop(lr = 0.0002)
model.compile(loss=losses.mean_squared_error,
optimizer=adag
)
history = model.fit(X_train, Y_train, epochs=2000,
batch_size=20, shuffle = True)
How to cross validate the above code? I want leave one out cross validation method to be used in this.
jolespin commentedon May 14, 2019
@shreyassks this isn't the correct place for your question but I would check this out: https://keras.io/scikit-learn-api . Wrap your network in a
scikit-learnestimator then use w/model_selection.cross_val_scoreTomMeowMeow commentedon Jun 3, 2019
Yes. I totally agree! This also happened to Brier_score_loss, it works perfectly fine using Brier_score_loss, but it gets confusing when it comes from the GridSearchCV, the negative Brier_score_loss returns. At least, it would be better output something like, because Brier_score_loss is a loss (the lower the better), the scoring function here flip the sign to make it negative.
pritishban commentedon Dec 17, 2019
For Regression usecase:
model_score= cross_val_score(model, df_input, df_target, scoring='neg_mean_squared_error', cv=3)
I am getting the values as:
SVR:
[-6.20938025 -1.397376 -1.94519 ]
-3.183982080147279
Linear Regression:
[-5.94898085 -9.30931808 -1.15760676]
-5.4719685646934275
Lasso:
[ -7.22363814 -10.47734135 -2.20807684]
-6.6363521107522345
Ridge:
[-5.95990385 -4.17946756 -1.36885809]
-3.8360764993832004
So which one is best ?
SVR ?
pritishban commentedon Dec 17, 2019
For Regression usecase:
I am getting different results when I use
(1) "cross_val_score" with scoring='neg_mean_squared_error'
and
(2) For the same inputs when when I use "GridSearchCV" and check the 'best_score_'
For Regression models which one is better ?
(OR)
amueller commentedon Dec 17, 2019
@pritishban
You're asking a usage question. The issue tracker is mainly for bugs and new features. For usage questions, it is recommended to try Stack Overflow or the Mailing List.