Skip to content

implement predict_scores proposal #389

Closed

Description

@mblondel

predict_scores is a method to unify predict methods which output a score such as decision_function, predict_proba, etc. It should return a [n_samples, n_classes] array containing scores (the bigger, the more likely to choose the corresponding class).

The method signature should be predict_scores(self, X, score="margin|proba|log_proba|...") or maybe predict_scores(self, X, score_func="margin|proba|log_proba|...").

TODO list (please comment):

  • Add a PredictorMixin which implements predict_scores (just calling the underlying decision_function, predict_proba, etc if present and raising a NotImplementedError if not)
  • Add PredictorMixin to objects which have a decision_function or a predict_proba method
  • Currently, decision_function returns an array of shape [n_samples, 1] in the binary case. Break API to return [n_samples, 2] or handle decision_function separately in PredictorMixin.predict_scores. I suggest we create a new method predict_margin which is basically the same as decision_function but with the array shape fixed and deprecate decision_function (keep it for two releases).
  • Override predict_scores in KMeans and other clustering modules to return the opposite of the distances to the clusters.
  • Implement predict_log_proba as the log of predict_proba in PredictorMixin (?)
  • Change multiclass module to internally use predict_scores (currently it looks for decision_function and predict_proba)
  • Add predict_scores to the multiclass estimators. OneVsOneClassifier and OutputCodeClassifier will need to override predict_scores

Probably a good candidate for the next sprint.

Activity

  1. larsmans commented on Oct 19, 2011

    @larsmans
    Member

    If it's going to raise NotImplementedError whenever a score is not available, then we might as well add predict_scores to ClassifierMixin.

    As for predict_log_proba, a default implementation as the log of predict_proba would be fine as long as the reverse is also true: predict_proba as the exp of predict_log_proba. Probabilistic classifiers (such as NB) commonly work in logspace. This might get tricky, though.

  2. mblondel commented on Oct 19, 2011

    @mblondel
    MemberAuthor

    If it's going to raise NotImplementedError whenever a score is not available, then we might as well add predict_scores to ClassifierMixin.

    Any thought, others? (@ogrisel, @GaelVaroquaux, ...)

    As for predict_log_proba, a default implementation as the log of predict_proba would be fine as long as the reverse is also true: predict_proba as the exp of predict_log_proba. Probabilistic classifiers (such as NB) commonly work in logspace. This might get tricky, though.

    How do you plan to do it? (Won't we end up with a recursive definition?)

  3. larsmans commented on Oct 19, 2011

    @larsmans
    Member

    I think we'd have to rename the methods in derived classes to _predict_proba and _predict_log_proba to infinite recursion.

  4. ogrisel commented on Oct 19, 2011

    @ogrisel
    Member

    I have not had the time to follow the details but I would rather avoid to add method to the mixins that raise NotImplementedError: the motivation is that we give incentives to use duck typing to discover the capabilities of an estimator and base methods that raise NotImplementedError feels adversary to duck typing-style introspection.

  5. mblondel commented on Oct 19, 2011

    @mblondel
    MemberAuthor

    I have not had the time to follow the details but I would rather avoid to add method to the mixins that raise NotImplementedError: the motivation is that we give incentives to use duck typing to discover the capabilities of an estimator and base methods that raise NotImplementedError feels adversary to duck typing-style introspection.

    What should we do in predict_scores when the underlying method (e.g. predict_proba) is not defined. Just let Python raise an AttributeError? BTW, the user can still do hasattr(clf, "predict_proba"), the question here is just, what to do inside predict_scores.

  6. agramfort commented on Oct 19, 2011

    @agramfort
    Member

    I had in mind just adding in ClassifierMixin

    this method:

    def predict_scores(self, X, score="proba"):
        method_name = "predict_" + score
        if not hasattr(self, method_name):
            raise NotImplementedError("%s is not implemented" % method_name)
        else:
            return getattr(self, method_name)(X)
    
  7. larsmans commented on Oct 19, 2011

    @larsmans
    Member

    +1 for @agramfort's proposal (with an exception to call decision_function for score=margin). This seems like the simplest option to me.

  8. mblondel commented on Oct 19, 2011

    @mblondel
    MemberAuthor

    +1 for @agramfort's proposal (with an exception to call decision_function for score=margin). This seems like the simplest option to me.

    When we tackle this issue, I would like to deprecate decision_function to fix the shape issue in the binary case, if that's ok with everyone. I'm thinking of calling the new method predict_margin instead.

  9. GaelVaroquaux commented on Oct 19, 2011

    @GaelVaroquaux
    Member

    When we tackle this issue, I would like to deprecate decision_function to fix the shape issue in the binary case, if that's ok with everyone. I'm thinking of calling the new method predict_margin instead.

    I'd really like us to stop changing APIs. We are massively used nowadays.
    There's been a printed book published referencing us, gazillion of blog
    posts, and who knows how much code in different labs using the scikit. We
    need to be very careful about changing API.

    This is not a vote completely against, as I haven't dedicated much mental
    bandwidth to this issue (I am crawling under work currently). But I'd
    like the reasons for any API change to be clearly thought through and
    explained on the mailing list.

  10. ogrisel commented on Oct 19, 2011

    @ogrisel
    Member

    Well, as discussed on the mailing list, the current API is completely inconsistent especially in terms of shapes. As long as we deprecate methods with warnings, preserve backward compatibility and document the replacement API this is fine.

  11. mblondel commented on Oct 19, 2011

    @mblondel
    MemberAuthor

    I'd really like us to stop changing APIs. We are massively used nowadays. There's been a printed book published referencing us, gazillion of blog posts, and who knows how much code in different labs using the scikit. We need to be very careful about changing API.

    If we fix the shape issue of decision_function without renaming it, this will break existing code (in the binary case). So I prefer to fix the shape issue in a new method predict_margin and deprecate decision_function. Also decision_function is probably used by advanced users only.

  12. amueller commented on Oct 7, 2012

    @amueller
    Member

    @mblondel would you be ok with closing this? We now have consistent shapes, so I personally don't really see the need for predict_score any more. wdyt?

  13. mblondel commented on Oct 7, 2012

    @mblondel
    MemberAuthor

    I wouldn't mind but the goal of predict_score was not only to have consistent shapes but also to have a unique method with a notion of score (the higher, the more likely to assign the class). This is useful for the multiclass module and will also be for calibration I guess.

  14. amueller commented on Oct 7, 2012

    @amueller
    Member

    You think a try-except is not a good solution?
    We could put the try-except in the mixin instead of the multi class.
    That would be very easy to do, but would add another function to the user-interface.
    Not sure this is beneficial.

  15. 2 remaining items

  16. raghavrv commented on Dec 6, 2014

    @raghavrv
    Member

    okay :)

  17. agramfort commented on Dec 6, 2014

    @agramfort
    Member

    There is a score_samples method in PCA

  18. mblondel commented on Dec 6, 2014

    @mblondel
    MemberAuthor

    @agramfort this issue is about getting continuous "confidence" scores in a
    consistent manner from regressors and classifiers. I am not saying adding a
    new method is the solution but I hope we can reach a concensus on a
    solution soon (c.f. recent discussion on the ML). score_samples is
    something different.

  19. iskandr commented on Dec 10, 2014

    @iskandr

    Would it be possible to equip every classifier with a default implementation of decision_function based on e.g. log probabilities?

  20. mblondel commented on Dec 11, 2014

    @mblondel
    MemberAuthor

    @iskandr That would indeed be possible (and also add decision_function as an alias to predict in regressors in order to be able to do binary classification and ranking). I personally see predict as what you get when you take the row-wise argmax of decision_function. Of course there are sometimes several ways of defining decision_function so that this is true. The only problem is that in some classifiers decision_function has inconsistent shape in the binary case: (n_samples) instead of (n_samples, 2).

    BTW if you're at NIPS I would be glad to have a chat about parakeet.

  21. iskandr commented on Dec 11, 2014

    @iskandr

    @mblondel I guess worries about either (1) breaking compatibility or (2) muddying the interface by adding a nearly identical method; is where this issue got stalled in 2012. Which of these two options seem preferable to you? I personally like the suggestion to deprecate decision_function and make a shape consistent predict_score or predict_margin.

    In either case, it seems like classifier with predict_proba should always have a default implementation of either decision_function and, if we add the method, predict_score.

    (I'm at NIPS! Are you going the poster session later today? I'll be there around 3 or 4)

  22. amueller commented on Dec 11, 2014

    @amueller
    Member

    I can introduce you two ;)
    For the two class case the decision function is (n_samples,) consistently in all models.
    So adding a default implementation of decision_function would remove the need for ducktyping in many cases. It will still require an if for the decision function in some cases, but I think that shouldn't really be that big an issue.

  23. mblondel commented on Dec 11, 2014

    @mblondel
    MemberAuthor

    I personally like the suggestion to deprecate decision_function and make a shape consistent predict_score or predict_margin.

    This is my favored solution too, since I want to fix the shape inconsistency issue but would rather not break compatibility.

  24. amueller commented on Dec 11, 2014

    @amueller
    Member

    I'm not sure which inconsistency you mean. The fact that predict_proba has a diffferent shape from decision function? Why is that a problem?

  25. iskandr commented on Dec 11, 2014

    @iskandr

    @amueller Are there any binary classifiers whose decision_function returns an array with shape (n_samples, 2)?

  26. mblondel commented on Dec 11, 2014

    @mblondel
    MemberAuthor

    I was talking of the inconsistency between the n_classes=2 and n_classes>2 cases. This requires branching in user code to handle the different shapes. Not the end of the world but that's inconvenient :)

  27. amueller commented on Aug 28, 2015

    @amueller
    Member

    I think we kind of decided on not changing that, right? @mblondel would be ok to close?

  28. mblondel commented on Aug 29, 2015

    @mblondel
    MemberAuthor

    Alright, closing :)

  29. valeman commented on Oct 25, 2023

    @valeman

    It is very unfortunate that this was not implemented, the fact is 'predict_proba' is both false and misleading people believing that scikit-learn produces 'probabilities'.

    In fact it produces miscalibrated class scores that not only costs companies money but is very dangerous in high risk applications including health, medicine, self driving cars, finance and many others.

    Misnomering the function as 'predict_proba' is not only incorrect, but causes harm to anyone who uses it and is irresponsible act from what is most used machine learning library. Not only the function is clearly misleading but the documentation does not even explain that class scores are miscalibrated that has been documents in countless academic literature for over 20 years.

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