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
SGDClassifier never has the attribute "predict_proba" (even with log or modified_huber loss) #10113
SGDClassifier's predict_proba() is not compatible with MultiOutputClassifier's predict_proba() (even when it has the proper loss functions: log or modified_huber).
ifnothasattr(self.estimator, "predict_proba"):
raiseValueError("The base estimator should implement""predict_proba method")
Just for the overly simplified example below, LogisticRegression classifiers do have the attribute, and those work correctly.
Steps/Code to Reproduce
fromsklearn.linear_modelimportSGDClassifierasonlinefromsklearn.linear_modelimportLogisticRegressionaslog# use either one because they allow predict_proba() with SGDClassifier alone:clf_test=online(loss="log", penalty="l2")
#clf_test = online(loss="modified_huber", penalty="l2")# The problematic condition in MultiOutputClassifier's predict_proba():ifnothasattr(clf_test, "predict_proba"):
print("Don't allow predict_proba() when wrapped by MultiOutputClassifier.")
else:
print("Allow predict_proba() when wrapped by MultiOutputClassifier.")
# By contrast, the logistic regression classifier would work.clf_test=log()
ifnothasattr(clf_test, "predict_proba"):
print("Don't allow predict_proba() when wrapped by MultiOutputClassifier.")
else:
print("Allow predict_proba() when wrapped by MultiOutputClassifier.")
Expected Results
Allow predict_proba() when wrapped by MultiOutputClassifier.
Allow predict_proba() when wrapped by MultiOutputClassifier.
Actual Results
Don't allow predict_proba() when wrapped by MultiOutputClassifier.
Allow predict_proba() when wrapped by MultiOutputClassifier.
changed the title [-]SDGClassifier never has the attribute "predict_proba" (even with log or modified_huber loss)[/-][+]SGDClassifier never has the attribute "predict_proba" (even with log or modified_huber loss)[/+]on Nov 11, 2017
Alright, it has something to do with predict_proba being a property in SGDClassifier.
A simple workaround (which will work before or after there's a fix) is to make a simple subclass that implements predict_proba (not a property) by calling the super's predict_proba. In case it isn't obvious, the data is made-up here.
fromsklearn.linear_modelimportSGDClassifierasonlinefromsklearn.multioutputimportMultiOutputClassifierasmocclassSimple(online):
# be sure to add the rest of the parameters if you need themdef__init__(self, loss='log'):
super(Simple, self).__init__(loss=loss, penalty="l2")
defpredict_proba(self, X):
returnsuper(Simple, self).predict_proba(X)
# Subclass worksclf_test=moc(Simple())
clf_test.partial_fit([[0,1], [1,0]], [[0,1], [1,0]], [[0,1], [0,1]])
printclf_test.predict_proba([[0,1]])
# main class failsclf_test=moc(online(loss="log", penalty="l2"))
clf_test.partial_fit([[0,1], [1,0]], [[0,1], [1,0]], [[0,1], [0,1]])
printclf_test.predict_proba([[0,1]])
The problem is that MultiOutputClassifier is checking the before-fitting estimator for whether it supports predict_proba, not the after-fitting estimator.
SGDClassifier, after fitting, should support predict_proba with an appropriate loss:
@srajanpaliwal it seems your are not going to close this issue soon, but correct me if I'm wrong?
I'm taking over it, and will try to push something soon.
'Fix SGDClassifier never has the attribute "predict_proba" ' #10389 seems to fix #10113, but not yet reviewed and closed. @jnothman do you agree that #10389 fixed #10113 ?
Activity
[-]SDGClassifier never has the attribute "predict_proba" (even with log or modified_huber loss)[/-][+]SGDClassifier never has the attribute "predict_proba" (even with log or modified_huber loss)[/+]jhays006 commentedon Nov 11, 2017
Alright, it has something to do with predict_proba being a property in SGDClassifier.
A simple workaround (which will work before or after there's a fix) is to make a simple subclass that implements predict_proba (not a property) by calling the super's predict_proba. In case it isn't obvious, the data is made-up here.
jnothman commentedon Nov 11, 2017
predict_proba, for some estimators, is only available after fitting.
yes, MultiOutputClassifier is incorrectly checking whether
predict_probais available onself.estimatorrather thanself.estimators_[0]. Fix welcome.jnothman commentedon Nov 11, 2017
Perhaps MultiOutputClassifier should also use the same property mechanism to only make predict_proba available after fitting.
jhays006 commentedon Nov 11, 2017
But, predict_proba isn't available even after fitting.
jnothman commentedon Nov 11, 2017
The problem is that MultiOutputClassifier is checking the before-fitting estimator for whether it supports predict_proba, not the after-fitting estimator.
SGDClassifier, after fitting, should support predict_proba with an appropriate loss:
jhays006 commentedon Nov 11, 2017
Oh, it's checking too early. Alright, thanks for looking into it so fast.
jnothman commentedon Nov 11, 2017
Fix SGDClassifier never has the attribute "predict_proba" (even with …
Fix SGDClassifier never has the attribute "predict_proba" (even with …
FinLouarn commentedon Jul 16, 2018
@srajanpaliwal it seems your are not going to close this issue soon, but correct me if I'm wrong?
I'm taking over it, and will try to push something soon.
FinLouarn commentedon Jul 16, 2018
'Fix SGDClassifier never has the attribute "predict_proba" ' #10389 seems to fix #10113, but not yet reviewed and closed. @jnothman do you agree that #10389 fixed #10113 ?
jnothman commentedon Jul 16, 2018
@FinLouarn, yes, it's the right solution, but lacks a test.
jnothman commentedon Jul 16, 2018
Please do complete it.
rebekahkim commentedon Sep 29, 2018
I'll work on the test
jnothman commentedon Feb 25, 2019
I can't reproduce this issue at master actually. Running the code under "steps to reproduce" I get the expected results. Close?
rebekahkim commentedon Feb 27, 2019
@jnothman, I think @TomDLT also suggested the same thing but as @amueller pointed out, it would still fail for certain (obscure) cases.
Here's an edge case I came up with in #12222