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
inconsistent decision_function and predict_proba shapes #1023
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.
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
-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.
@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)
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/1023#issuecomment-7719589.
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?
@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.
@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.
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").
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.
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?
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.
Apparently predict_proba also has inconsistency problems, right?
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.
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.
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.
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)
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.
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.
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.
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?
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.
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:
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.
On Tue, Aug 14, 2012 at 10:49:40AM -0700, Andreas Mueller wrote:
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).
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])
Activity
pprett commentedon Aug 14, 2012
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_probaSGD issueagramfort commentedon Aug 14, 2012
+1 for decision_function (n_samples,) when only 2 classes
mblondel commentedon Aug 14, 2012
-1 for a special shape in the 2 class case
On Aug 14, 2012 5:02 PM, "Alexandre Gramfort" [email protected]
wrote:
pprett commentedon Aug 14, 2012
@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]
Peter Prettenhofer
amueller commentedon Aug 14, 2012
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_functionandpredict_probahave 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?
amueller commentedon Aug 14, 2012
OT: If anyone has the time to review #1009, #1013 or #1024 that would be great. They are all very small.
mblondel commentedon Aug 14, 2012
@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_scoresAPI). If we move to apredict_scoresAPI, we can introduce the new consistent (n_samples, n_vectors) shape.amueller commentedon Aug 14, 2012
@mblondel Thanks for bringing up #389 which I basically duplicated here ;)
For
predict_scoresyou wanted[n_classes, 2]shapes for binary prediction, right?As far as I understand, your
predict_scoreproposal addresses two issues that I kind of also raised hereThe
predict_scorebasically 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.
amueller commentedon Aug 14, 2012
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.
mblondel commentedon Aug 14, 2012
Indeed,
[n_samples, 2]could also be nice to make theargmaxconsistent...amueller commentedon Aug 14, 2012
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.
mblondel commentedon Aug 14, 2012
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
predictby using argmax on the output ofpredict_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").amueller commentedon Aug 14, 2012
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.
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_probaanddecision_function, one, or none?mblondel commentedon Aug 14, 2012
Apparently predict_proba also has inconsistency problems, right?
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.
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.
amueller commentedon Aug 14, 2012
Only for SGDClassifier with loss='log'
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.
mblondel commentedon Aug 14, 2012
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)
amueller commentedon Aug 14, 2012
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.
amueller commentedon Aug 14, 2012
I think having a
decision_functionandpredict_probaand a unified way to test whether an estimator supports them would also be a workable solution, without the need for having apredict_scoremethod. 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.
mblondel commentedon Aug 15, 2012
Even if we decide to keep decision_function and predict_proba, it would still be useful to have a predict_scores method. Something like:
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.
amueller commentedon Aug 15, 2012
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_scoreswill 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?
mblondel commentedon Aug 15, 2012
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.
amueller commentedon Aug 19, 2012
For thinking about the
predict_scoreproposal, I thought it might be interesting to have a list of all estimators that implement bothdecision_functionandpredict_proba.Here goes:
so basically SVM, SGD, QDA and LDA.
For SVM and SGD, the availablity of
predict_probadepends 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.
amueller commentedon Aug 27, 2012
Closing this as it is a duplicate of #1023.
GaelVaroquaux commentedon Aug 27, 2012
On Tue, Aug 14, 2012 at 09:08:28AM -0700, Mathieu Blondel wrote:
+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
GaelVaroquaux commentedon Aug 27, 2012
That's an interesting win.
GaelVaroquaux commentedon Aug 27, 2012
On Tue, Aug 14, 2012 at 10:49:40AM -0700, Andreas Mueller wrote:
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.
I think that I prefer the first option (in favor of breking API, for
once).
G
GaelVaroquaux commentedon Aug 27, 2012
On Tue, Aug 14, 2012 at 11:30:32PM -0700, Mathieu Blondel wrote:
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
vappiah commentedon Sep 19, 2018
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])
jnothman commentedon Sep 19, 2018
Please provide a complete verifiable example, including imports and toy data.