.predict_proba() for SVC produces incorrect results for binary classification
See original GitHub issueDescription
svm.predict_proba()
produces revered results for binary classification
- this seems to be specific to binary classification. For example, it works fine for 3 way classification, which is in the test: https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/svm/tests/test_svm.py#L307
- I think this is related to #394, but in this case we are using the same
svm
object so I thinkpredict
andpredict_proba
should agree - Acknowledgement: @amennen noticed this error 👍
Steps/Code to Reproduce
here’s the code on colab: https://colab.research.google.com/github/qihongl/random/blob/master/sklearn-svm-predict-proba-bug.ipynb
# train a svm
X = np.array([[-1, -1], [1, 1]])
y = np.array([0, 1])
svm = SVC(probability=True)
svm.fit(X, y)
# SVM makes reasonable prediction on the learned examples...
print(svm.predict(X))
# but it makes the reversed probability estimates...
print(svm.predict_proba(X))
Expected Results
[0 1]
[[0.66383953 0.33616047]
[0.33916469 0.66083531]]
# i.e. when the prediction is class 0, prob(class0) should be bigger than prob(class1)
Actual Results
[0 1]
[[0.33616047 0.66383953]
[0.66083531 0.33916469]]
# i.e. when the prediction is class 0, prob(class0) < prob(class1)
Versions
0.20.3
Issue Analytics
- State:
- Created 4 years ago
- Comments:22 (12 by maintainers)
Top Results From Across the Web
Scikit-learn predict_proba gives wrong answers - Stack Overflow
predict_probas is using the Platt scaling feature of libsvm to callibrate probabilities, see: How does sklearn.svm.svc's function ...
Read more >sklearn.svm.SVC — scikit-learn 1.2.0 documentation
The parameter is ignored for binary classification. ... 2]) >>> from sklearn.svm import SVC >>> clf = make_pipeline(StandardScaler(), ... predict_proba (X).
Read more >Can you interpret probabilistically the output of a Support ...
(E.g., in binary classification, a sample may be labeled by predict as belonging to a class that has probability <12 according to predict_proba...
Read more >A Gentle Introduction to Threshold-Moving for Imbalanced ...
First, let's fit a model and calculate a ROC Curve. We can use the make_classification() function to create a synthetic binary classification ......
Read more >Calibration using predict_proba vs class_weight
decision_function() method for your final classification. Finally, try not to over-optimize your classifier, because you can easily end up with a trivial const ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I came to the same conclusion in my analysis, and could also reproduce the same behavior with CalibratedClassifierCV when not using stratification. This is the evil of LOO in small noisy datasets.
So I agree with the closing this as a separate bug.
Reopening this as I ran into the same issue. According to my observations, the issue of .predict() and argmax of .predict_proba() not tallying only comes up when class_weights are enabled. Sharing modified code of @rth to recreate the issue :
produces
My best hypothesis at the moment is that .predict_proba() gives raw probabilities whereas .predict() actually takes into class_weights on the outputs of .predict_proba() to make the final inference.