Skip to content

Commit fcf9cb4

Browse files
committed
Merge pull request scikit-learn#4835 from tw991/feature2
[MRG] fix SVM can be tricked into running proba() scikit-learn#4791
2 parents 32b2f8e + 4c4bca1 commit fcf9cb4

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

sklearn/svm/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,9 @@ def predict(self, X):
571571
# probabilities are not available depending on a setting, introduce two
572572
# estimators.
573573
def _check_proba(self):
574-
if not self.probability:
575-
raise AttributeError("predict_proba is not available when"
576-
" probability=%r" % self.probability)
574+
if not self.probability or self.probA_.size == 0 or self.probB_.size == 0:
575+
raise AttributeError("predict_proba is not available when fitted with"
576+
" probability=False")
577577
if self._impl not in ('c_svc', 'nu_svc'):
578578
raise AttributeError("predict_proba only implemented for SVC"
579579
" and NuSVC")

sklearn/svm/tests/test_svm.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,3 +842,13 @@ def test_lsvc_intercept_scaling_zero():
842842
lsvc = svm.LinearSVC(fit_intercept=False)
843843
lsvc.fit(X, Y)
844844
assert_equal(lsvc.intercept_, 0.)
845+
846+
847+
def test_trick_proba():
848+
# Test that the right error message is thrown when self.probability is manually set to be True
849+
850+
G = svm.SVC()
851+
G.fit(iris.data, iris.target)
852+
G.probability = True
853+
msg = "predict_proba is not available when fitted with probability=False"
854+
assert_raise_message(AttributeError, msg, getattr, G, "predict_proba")

0 commit comments

Comments
 (0)