Skip to content

Our R^2 makes odd assumptions, doesn't work with LeaveOneOut #5097

Open

Description

@amueller
from sklearn.datasets import make_regression
from sklearn.cross_validation import cross_val_score, LeaveOneOut
from sklearn.linear_model import Ridge

X, y, coef_ = make_regression(random_state=42, noise=1, n_samples=200, coef=True)
cross_val_score(Ridge(), X, y, cv=LeaveOneOut(len(X)))

array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0.])

Maybe the single sample in the LOO is not interpreted correctly? :-/

Activity

  1. jnothman commented on Aug 8, 2015

    @jnothman
    Member

    r2_score is returning 0 for any two arrays of shape (1,). According to http://scikit-learn.org/dev/modules/model_evaluation.html#r2-score, this case should rather involve a zero-denominator, but in any case determining the mean y from a single sample is blatantly problematic.

    I have wondered for a while if there are cases where the cross validation score should be micro-averaged, i.e. calculate score over output of cross_val_predict rather than averaging scores from each fold's fit.

  2. agramfort commented on Aug 8, 2015

    @agramfort
    Member
  3. amueller commented on Aug 8, 2015

    @amueller
    MemberAuthor

    I did not realize that is what iid is for. I rarely do regression so I didn't know about this behavior of the r2_score. It is quite counter-intuitive :-/

  4. jnothman commented on Aug 9, 2015

    @jnothman
    Member

    It's not what iid does in my memory, but it might have done so in
    prehistory. iid selects between unweighted or sample-size-weighted average
    over scores per fold.

    I haven't worked with r2 scores before; I might have thought that the
    y-mean could be better estimated from the training set.

    On 9 August 2015 at 01:32, Andreas Mueller [email protected] wrote:

    I did not realize that is what iid is for. I rarely do regression so I
    didn't know about this behavior of the r2_score. It is quite
    counter-intuitive :-/

    —
    Reply to this email directly or view it on GitHub
    #5097 (comment)
    .

  5. jnothman commented on Aug 9, 2015

    @jnothman
    Member

    And iid was introduced by Gaël in a9e73ad, it seems with current semantics.

  6. mblondel commented on Aug 9, 2015

    @mblondel
    Member

    there used to be a way to do that with a horrible parameter name called iid... @mblondel argued to remove it AFAIK.

    iid is still in GridSearchCV. I didn't even know there used to be iid in cross_val_score.

  7. amueller commented on Aug 9, 2015

    @amueller
    MemberAuthor

    I think r2_score should at least raise a warning with a single sample, if not a ValueError.

    And we should probably have a look at what R does here, so we could work around it.
    Getting the y-mean from the test data seems slightly odd indeed.

  8. jnothman commented on Aug 10, 2015

    @jnothman
    Member

    It's not going to be much better with 2 samples!

  9. jnothman commented on Aug 10, 2015

    @jnothman
    Member

    And getting the y-mean from anything other than the test data makes an
    interesting use-case for our scorer API!

  10. amueller commented on Aug 10, 2015

    @amueller
    MemberAuthor

    I agree with both your statements. Relabeling as API bug. We should ask a statistician ^^

  11. added this to the 0.19 milestone on Sep 29, 2016
  12. 1 remaining item

  13. changed the title [-]Bug in cross-val-score?[/-] [+]Our R^2 makes odd assumptions, doesn't work with LeaveOneOut[/+] on Oct 27, 2016
  14. hsiaoyi0504 commented on Apr 22, 2017

    @hsiaoyi0504

    Is it already fixed? I am curious about why it works for RFECV but doesn't work for cross_val_score (both conditions use leaveOneOut cross validation)

  15. modified the milestone: 0.19 on Jun 12, 2017
  16. jnothman commented on Jul 5, 2018

    @jnothman
    Member

    I seem to have come to agree with @amueller that we should raise an error with a single sample. See #11435

  17. jnothman commented on Jul 5, 2018

    @jnothman
    Member

    Should we support the case of metric(y, cross_val_predict(...)) but find a way to document its difference from the mean held-out performance? It requires some change of API (in cross_validate, cross_val_score and BaseSearchCV.cv_results_), because we will no longer be reporting score per-split and the mean across splits.

  18. adrinjalali commented on Dec 13, 2018

    @adrinjalali
    Member

    Would this API change proposal be a candidate for a SLEP? i.e. an option to score all the outputs together rather than average over folds. Or is it too minor of a change for that?

  19. amueller commented on Dec 13, 2018

    @amueller
    MemberAuthor

    Lol @jnothman agreeing with me for my birthday and I only saw it now!
    I think it would warrant a SLEP but honestly I don't know what the solution should be. And I hear more and more people argue against R^2 as a metric :-/

  20. adrinjalali commented on Dec 13, 2018

    @adrinjalali
    Member

    I think the API proposal is independent of whether we want to keep/recommend R^2 or not, the user can use the same API/solution for other scorers.

  21. amueller commented on Dec 13, 2018

    @amueller
    MemberAuthor

    It's much less pressing if we move away from R^2, though.

  22. jnothman commented on Dec 17, 2018

    @jnothman
    Member
  23. amueller commented on Dec 17, 2018

    @amueller
    MemberAuthor

    @jnothman I think we're on the same page.

  24. MLopez-Ibanez commented on Oct 6, 2021

    @MLopez-Ibanez
    Contributor

    By the way, the special case of LOOCV leads to some wrong assumptions: https://machinelearningmastery.com/loocv-for-evaluating-machine-learning-algorithms/

    When implementing LOOCV manually, the author uses accuracy_score() on the y_pred and y_true collected from all test folds.

    However, when using cross_val_score(), the accuracy scorer is applied to each test folds individually. The author (and surely many users) does not notice the difference because the result is the same for accuracy_score, however, it would not be the same for most scoring metrics specially those for regression. Perhaps this page should warn about this pitfall: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.LeaveOneOut.html

    What RidgeCV() is doing is yet another different thing, because it chooses the best parameters of the model based on the CV, which is not what cross_val_score() or GridSearchCVis used for. In that sense, it may be more similar to bootstrapping with OOB samples in RandomForest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions