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
Pipeline can not be used with multiclass (or the other way around) #1019
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:
set the methods of pipeline on the fly and only if the last estimator has them. It is done this way in GridSearchCV.
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 :-/
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.
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.
@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.
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.
@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.
Activity
amueller commentedon Aug 13, 2012
Ok another idea: we define
decision_functionandpredict_probain the mixin, but raise aNotImplementedErrorthere.Then we can just
Opinions?
pprett commentedon Aug 14, 2012
the try except looks good to me (a nice example of duck typing). Note that if
NotImplementedErroris the norm rather than the exception we might considerhasattrbecause it should be faster.amueller commentedon Aug 14, 2012
As this is called once per call to "decision-function" or "predict_proba" the overhead is hopefully neglectible.
mblondel commentedon Aug 14, 2012
It's cleaner if an estimator raise
AttributeErrorrather thanNotImplementedErrorwhen a function is not available. So adding the functions on the fly inPipelinesounds a good idea to me (it is also more consistent withGridSearchCV).If you decide to go for the second solution, I'd catch both
AttributeErrorandNotImplementedError.amueller commentedon Aug 14, 2012
@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
SVCandSGDClassifier.There,
predict_proba_is only available when certain parameters are set. So we should change it there, too?Why do you think raising an
AttributeErroris cleaner than raising aNotImplementedError? I changed the errors inSGDClassifiertoNotImplementedErrorat 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.mblondel commentedon Aug 14, 2012
But wasn't the original problem caused by the fact
hasattralways returnsTruefor a Pipeline object?If a functionality is missing, the corresponding method should ideally even not exist, which is why I think
AttributeErroris usually better. But for cases likeSVCwherepredict_probacan only work if it was enabled in the constructor, I think it makes sense to raiseNotImplementedError.amueller commentedon Aug 14, 2012
@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_functionis tested before thepredict_proba. Since otherwise, theNotImplementedErrorwould be raised.Luckily, there are no estimators that raise a
NotImplementedErrorondecision_function, otherwise the whole thing wouldn't work.amueller commentedon Aug 31, 2012
@mblondel so to handle the situation correctly using
AttributeErrorandNotImplementedError,basically we can do
Does that look good to you?
ogrisel commentedon Sep 1, 2012
LGTM
mblondel commentedon Oct 8, 2012
Fixed in #1215.