Skip to content

Pipeline can not be used with multiclass (or the other way around) #1019

Closed

Description

@amueller

tldr: Is it good to check if a classifier supports decision_function using hasattr?

The plan use the following in the multi-class to get a decision function:

try:
    clf.predict_proba()
except (AttributeError, NotImplementedError):
   clf.decision_function()

Optionally, put the decision_function and predict_proba in place on the fly as in GridSearchCV.

Hey everybody.
I just noticed that pipeline can not be used with the multiclass module in some cases.
The multiclass module checks whether a classifier has a decision_function and tries to use that, otherwise it uses predict_proba. A Pipeline by default has the whole zoo of methods and tries to call them on the last estimator.
So if the last estimator has no decision_function, but a predict_proba (like the trees), this fails :-/

The main reason this annoys me is because I wanted to use the same decision_function / predict_proba pattern for auc cross-validation (see #1014), so this also fails with pipelines.

Two ways to fix this:

  1. set the methods of pipeline on the fly and only if the last estimator has them. It is done this way in GridSearchCV.

  2. Instead of using hasattr(clf, 'decision_function'), we could use a try ... except AttributeError block.

From a pretty-coding standpoint I'd prefer 2). But the AttributeError might be caused by something else and shadow some other bug. If there was an NoDecisionFunctionError, I'd go for that. But we don't want custom exceptions, do we?

I'm glad i didn't start counting the API annoyances that I found this weekend :-/

Activity

  1. amueller commented on Aug 13, 2012

    @amueller
    MemberAuthor

    Ok another idea: we define decision_function and predict_proba in the mixin, but raise a NotImplementedError there.
    Then we can just

    try:
        out = decision_function(X)
    except NotImplementedError:
       out = predict_proba(X)

    Opinions?

  2. pprett commented on Aug 14, 2012

    @pprett
    Member

    the try except looks good to me (a nice example of duck typing). Note that if NotImplementedError is the norm rather than the exception we might consider hasattr because it should be faster.

  3. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor

    As this is called once per call to "decision-function" or "predict_proba" the overhead is hopefully neglectible.

  4. mblondel commented on Aug 14, 2012

    @mblondel
    Member

    It's cleaner if an estimator raise AttributeError rather than NotImplementedError when a function is not available. So adding the functions on the fly in Pipeline sounds a good idea to me (it is also more consistent with GridSearchCV).

    If you decide to go for the second solution, I'd catch both AttributeError and NotImplementedError.

  5. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor

    @mblondel I feel adding functions on the fly is a bit hacky.
    Also note that for some estimators, errors are already raised, for example in SVC and SGDClassifier.
    There, predict_proba_ is only available when certain parameters are set. So we should change it there, too?

    Why do you think raising an AttributeError is cleaner than raising a NotImplementedError? I changed the errors in SGDClassifier to NotImplementedError at some point as I felt that was more appropriate. I'm not sure what they were before.

    I'm not totally against using hasattr, though.

  6. mblondel commented on Aug 14, 2012

    @mblondel
    Member

    But wasn't the original problem caused by the fact hasattr always returns True for a Pipeline object?

    If a functionality is missing, the corresponding method should ideally even not exist, which is why I think AttributeError is usually better. But for cases like SVC where predict_proba can only work if it was enabled in the constructor, I think it makes sense to raise NotImplementedError.

  7. amueller commented on Aug 14, 2012

    @amueller
    MemberAuthor

    @mblondel you are right, these are two different problems. I think it would be nice if we could handle them uniformly, though. I guess you are aware that the decision in multi-class only works because the decision_function is tested before the predict_proba. Since otherwise, the NotImplementedError would be raised.

    Luckily, there are no estimators that raise a NotImplementedError on decision_function, otherwise the whole thing wouldn't work.

  8. amueller commented on Aug 31, 2012

    @amueller
    MemberAuthor

    @mblondel so to handle the situation correctly using AttributeError and NotImplementedError,
    basically we can do

    try:
        clf.predict_proba()
    except (AttributeError, NotImplementedError):
       clf.decision_function()

    Does that look good to you?

  9. ogrisel commented on Sep 1, 2012

    @ogrisel
    Member

    LGTM

  10. mblondel commented on Oct 8, 2012

    @mblondel
    Member

    Fixed in #1215.

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

    EasyEnhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions