Skip to content

inconsistent decision_function and predict_proba shapes #1023

Closed

Description

@amueller

The shapes of decision_function and predict_proba are inconsistent across classifiers for the two-label case.
As far as I can see, all classifier except LDA and QDA have a (n_samples,) shape decision_function and all classifiers
except SGDClassifier have (n_samples, 2) shape predict_proba. So I guess we should make all classifiers have that shape.

Btw, this means atm LDA and QDA don't work with multiclass and probably some other methods.

Activity

  1. pprett commented on Aug 14, 2012

    @pprett
    Member

    This should be fixed for the next release. Just to understand correctly: You propose that decision_function should have the shape (n_samples, n_classes)?

    PS: there is already a ticket for the predict_proba SGD issue

  2. agramfort commented on Aug 14, 2012

    @agramfort
    Member

    +1 for decision_function (n_samples,) when only 2 classes

  3. mblondel commented on Aug 14, 2012

    @mblondel
    Member

    -1 for a special shape in the 2 class case
    On Aug 14, 2012 5:02 PM, "Alexandre Gramfort" [email protected]
    wrote:

    +1 for decision_function (n_samples,) when only 2 classes

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/1023#issuecomment-7718914.

  4. pprett commented on Aug 14, 2012

    @pprett
    Member

    @mblondel considering a simple linear classifier in the binary case: how
    would you specify the decision_function values for (n_samples, n_classes)?
    (copy, negate)

    2012/8/14 Mathieu Blondel [email protected]

    -1 for a special shape in the 2 class case
    On Aug 14, 2012 5:02 PM, "Alexandre Gramfort" [email protected]
    wrote:

    +1 for decision_function (n_samples,) when only 2 classes

    —
    Reply to this email directly or view it on GitHub<
    https://github.com/scikit-learn/scikit-learn/issues/1023#issuecomment-7718914>.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/1023#issuecomment-7719589.

    Peter Prettenhofer

  5. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor

    Sorry if I was unclear. My proposal was to use a decision function of shape (n_samples,) for the binary case, as that is how it is implemented for basically all classifiers.

    This has the downside of being a special case but the upside of not changing the api to much.
    It would be the only case when decision_function and predict_proba have different shapes btw.

    another thing: SVC returns different shapes because of the one vs one. We could "transform" that. Should we?
    Related: What is the shape of the OneVsOneClassifier decision function and predict proba?

  6. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor

    OT: If anyone has the time to review #1009, #1013 or #1024 that would be great. They are all very small.

  7. mblondel commented on Aug 14, 2012

    @mblondel
    Member

    @pprett Sorry I was on my telephone and didn't have time to write a clear message. So to be more explicit, I would always use (n_samples, n_vectors), where n_vectors is 1 in the 2-class case and n_classes in the multiclass case. In my experience, this makes the code simpler.

    @amueller: Neither OvR nor OvO propogate decision_function and predict_proba at the moment.

    BTW, a very related issue is #389 (predic_scores API). If we move to a predict_scores API, we can introduce the new consistent (n_samples, n_vectors) shape.

  8. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor

    @mblondel Thanks for bringing up #389 which I basically duplicated here ;)
    For predict_scores you wanted [n_classes, 2]shapes for binary prediction, right?

    As far as I understand, your predict_score proposal addresses two issues that I kind of also raised here

    • making shapes consistent
    • finding a method that returns some score value

    The predict_score basically moves the problem of finding which function is supported into the mixin, instead of the place where it is used (multiclass for example).

    Is the main argument for the new name not to break stuff when changing the shape?
    I think the shape change affects very little estimators and is not worth introducing a rename.

  9. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor

    Oh, it seems I distributed the existing #389 over two issues, #1019 and this. My bad :-/ Not sure if they should be solved together, though.

  10. mblondel commented on Aug 14, 2012

    @mblondel
    Member

    Indeed, [n_samples, 2] could also be nice to make the argmax consistent...

  11. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor

    Didn't you just say "I would always use (n_samples, n_vectors), where n_vectors is 1 in the 2-class case" ?
    I am a bit confused now.

  12. mblondel commented on Aug 14, 2012

    @mblondel
    Member

    Sorry I had forgotten that I wrote [n_samples, 2] in issue #389. If we decide to give the predict_scores API a shot, it may be worth moving to [n_samples, n_classes] after all. That's my final answer.

    @pprett For decision_function, I would negate the predictions. The rule of thumb is that one should recover predict by using argmax on the output of predict_scores.

    @amueller Yes, introducing a new method allows to not break API. My research code often use decision_function, I really don't want that to break. Besides, the user should be able to explicitly pass the score method, e.g., predict_scores(X, score="proba").

  13. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor
    1. I see the point of not trying to break the API. Then again, if we go for [n_samples, 1] for decision function, that wouldn't break the API except for LDA and QDA. Not sure may people use the decision function there.

    2. I don't see the point in having someone explicitly pass the score method. What is the difference to having two functions?

    I am still not sure if I understood the predict_score proposal. Would you want to deprecate both, predict_proba and decision_function, one, or none?

    1. If you deprecate none, we will have three functions that essentially do the same. If you deprecate both, you will break everybody's code within the next two versions.
  14. mblondel commented on Aug 14, 2012

    @mblondel
    Member
    1. Apparently predict_proba also has inconsistency problems, right?

    2. Pro: It uses fewer methods and it gives us the opportunity to fix the shape problems for both decision_function and predict_proba. Con: a bit longer to type.

    3. If we change the returned shape, I would deprecate both decision_function and predict_proba (breaking API is not acceptable however small the change). Yes this will break the API within two releases, but isn't it what we've been doing so far? The point is to give people time to adjust (c.f. the scale_C fiasco)...

    If you don't like passing the score argument explicitly but still want to change the returned shape, we'll need to come up with new method names for decision_function, predict_proba and predict_log_proba.

    BTW, I completely agree with you that predict_scores will completely alleviate the need to search for a scoring method in the multiclass module.

  15. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor
    1. Only for SGDClassifier with loss='log'

    2. I think we should break the api as little as possible, what ever that means. I'd prefer instantly breaking code using QDA and LDA decision_function and SGDClassifier's predict_proba in the two-class case than change what I consider a large part of the interface across the library.

    From my point of view this is the essence: break the shapes and keep the names or change the names and deprecate.

    Let's see what other think.

  16. mblondel commented on Aug 14, 2012

    @mblondel
    Member

    From my point of view this is the essence: break the shapes and keep the names or change the names and deprecate.

    Indeed, that's a good summary of the trade-off at stake (although we'll have to introduce predict_scores anyway if we want a unified "predict with confidence" method)

  17. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor

    I don't really get this point. Why is it unified if it has two options and these might raise errors?
    I don't see how this is more unified than having two methods. I do get the argument that if we want to do save deprecation, we would need two new names, which is silly.

  18. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor

    I think having a decision_function and predict_proba and a unified way to test whether an estimator supports them would also be a workable solution, without the need for having a predict_score method. It would break some code, this is a definite downside, but it is the only one I see at the moment.

    Sorry, I just noticed that the intro to my previous post might sound unfriendly. It was not supposed to, it was rather that I'd like you explain your reasoning a bit more.

  19. mblondel commented on Aug 15, 2012

    @mblondel
    Member

    Even if we decide to keep decision_function and predict_proba, it would still be useful to have a predict_scores method. Something like:

    def predict_scores(self, X):
        if hasattr(self, "decision_function"):
            return self.decision_function(X)
        else:
            return self.predict_proba(X)

    In some cases like the multiclass module we don't care what the scores actually mean, we just care that the bigger the better.

    But what would be the best solution in my opinion is a method predict_scores(X, score=None | "margin" | "proba" | "log_proba") where None (default) means "return whatever score method is available. The point would be to have consistent shape across all the scikit for all scoring methods.

    As you said, it would also be nice to be able to query what predict_scores methods are available.

  20. amueller commented on Aug 15, 2012

    @amueller
    MemberAuthor

    Ok, that kind of convinced me that this is a good idea. One problem I see is that the user doesn't know what predict_scores will return by default. So do we add that to the docstring? That should be fine, though.

    Not really sold on changing the core api, though.
    Wait what the other have to say, then mailing list?

  21. mblondel commented on Aug 15, 2012

    @mblondel
    Member

    We could break API (without deprecation) for LDA/QDA but SGDClassifier is a bit more problematic since it's one of the most popular estimators in scikit-learn.

    Let's wait for more comments in this thread. Many people are still on holidays.

  22. amueller commented on Aug 19, 2012

    @amueller
    MemberAuthor

    For thinking about the predict_score proposal, I thought it might be interesting to have a list of all estimators that implement both decision_function and predict_proba.
    Here goes:

      <class 'sklearn.linear_model.logistic.LogisticRegression'>
      <class 'sklearn.linear_model.sparse.logistic.LogisticRegression'>
      <class 'sklearn.svm.classes.NuSVC'>
      <class 'sklearn.svm.sparse.classes.NuSVC'>
      <class 'sklearn.linear_model.perceptron.Perceptron'> 
      <class 'sklearn.qda.QDA'>
      <class 'sklearn.linear_model.stochastic_gradient.SGDClassifier'> 
      <class 'sklearn.linear_model.sparse.stochastic_gradient.SGDClassifier'>
      <class 'sklearn.linear_model.stochastic_gradient.SGDClassifier'>
      <class 'sklearn.svm.classes.SVC'>
      <class 'sklearn.svm.sparse.classes.SVC'>
      <class 'sklearn.linear_model.tests.test_sgd.SparseSGDClassifier'>
      <class 'sklearn.svm.sparse.classes.SVC'>
      <class 'sklearn.lda.LDA'>
    

    so basically SVM, SGD, QDA and LDA.
    For SVM and SGD, the availablity of predict_proba depends on the parameters, I think. For SVM there is the Platt scaling, for SGD there is the logistic loss.
    QDA and LDA have natural probabilistic output, though they are basically the softmax of the decision function.

  23. amueller commented on Aug 27, 2012

    @amueller
    MemberAuthor

    Closing this as it is a duplicate of #1023.

  24. GaelVaroquaux commented on Aug 27, 2012

    @GaelVaroquaux
    Member

    On Tue, Aug 14, 2012 at 09:08:28AM -0700, Mathieu Blondel wrote:

    [4]@amueller Yes, introducing a new method allows to not break API. My
    research code often use decision_function, I really don't want that to
    break.

    +0, but with a depreciation path.

    One thing that I like with the current API is that it enables duck typing
    to see if the estimator can do 'predict_proba'.

    G

  25. GaelVaroquaux commented on Aug 27, 2012

    @GaelVaroquaux
    Member

    BTW, I completely agree with you that predict_scores will completely
    alleviate the need to search for a scoring method in the multiclass
    module.

    That's an interesting win.

  26. GaelVaroquaux commented on Aug 27, 2012

    @GaelVaroquaux
    Member

    On Tue, Aug 14, 2012 at 10:49:40AM -0700, Andreas Mueller wrote:

    1. I think we should break the api as little as possible, what ever that
      means.

    I agree too: at the level of a community, it is the source of a huge
    headacke.

    That said, I still consider the scikit-learn in pre-API-stability phase
    (we haven't reach 1.0). And I think that we must move in order to get the
    best API for 1.0 (which should hopefully happen in the next year), even
    if it requires some API breakage.

    From my point of view this is the essence: break the shapes and keep the
    names or change the names and deprecate.

    I think that I prefer the first option (in favor of breking API, for
    once).

    G

  27. GaelVaroquaux commented on Aug 27, 2012

    @GaelVaroquaux
    Member

    On Tue, Aug 14, 2012 at 11:30:32PM -0700, Mathieu Blondel wrote:

    Even if we decide to keep decision_function and predict_proba, it would
    still be useful to have a predict_scores method. Something like:

    def predict_scores(self, X):
    if hasattr(self, "decision_function"):
    return self.decision_function(X)
    else:
    return self.predict_proba(X)

    I find that there is a cost to adding methods to our API. Is the gain
    larger than the cost? For the multiclass module, can we code testing
    both?

    G

  28. vappiah commented on Sep 19, 2018

    @vappiah

    Hi All,
    I am using sklearn version 0.19.1 and i have created a SVM classifier which is OneVsRestClassifier. My y shape is (301,) but when i run classifier.decision_function(xtest), the yscore it returns has a shape of (301,4). Due to this when i try to generate a roc_curve out of it , the error it generates is
    fpr[i],tpr[i],_=roc_curve(ytest[:,i],y_score[:,i])
    IndexError:too many indices for array
    Please advise on what to do
    BTW i have 4 classes below is a snippet of the code

    classifier=OneVsRestClassifier(model) #model is SVM
    if hasattr(model,'decision_function'):
    y_score=classifier.fit(xtrain,ytrain).decision_function(xtest)
    else:
    classifier.fit(xtrain,ytrain).predict_proba(xtest)
    fpr=dict()
    tpr=dict()
    roc_auc=dict()
    lw=2
    for i in range(n_classes):
    fpr[i],tpr[i],_=roc_curve(ytest[:,i],y_score[:,i]) #error generated here
    roc_auc[i]=auc(fpr[i],tpr[i])

  29. jnothman commented on Sep 19, 2018

    @jnothman
    Member

    Please provide a complete verifiable example, including imports and toy data.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions