8000 FIX raise AttributeError in SVC.coef_ for proper duck-typing (#8009) · scikit-learn/scikit-learn@9811b3b · GitHub
[go: up one dir, main page]

Skip to content

Commit 9811b3b

Browse files
amuellerjnothman
authored andcommitted
FIX raise AttributeError in SVC.coef_ for proper duck-typing (#8009)
1 parent 5dd9bfe commit 9811b3b

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

sklearn/svm/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,8 @@ def _validate_for_predict(self, X):
482482
@property
483483
def coef_(self):
484484
if self.kernel != 'linear':
485-
raise ValueError('coef_ is only available when using a '
486-
'linear kernel')
485+
raise AttributeError('coef_ is only available when using a '
486+
'linear kernel')
487487

488488
coef = self._get_coef( 8000 )
489489

sklearn/svm/tests/test_svm.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def test_libsvm_iris():
5757
for k in ('linear', 'rbf'):
5858
clf = svm.SVC(kernel=k).fit(iris.data, iris.target)
5959
assert_greater(np.mean(clf.predict(iris.data) == iris.target), 0.9)
60+
assert_true(hasattr(clf, "coef_") == (k == 'linear'))
6061

6162
assert_array_equal(clf.classes_, np.sort(clf.classes_))
6263

@@ -257,7 +258,7 @@ def test_oneclass():
257258
assert_array_almost_equal(clf.dual_coef_,
258259
[[0.632, 0.233, 0.633, 0.234, 0.632, 0.633]],
259260
decimal=3)
260-
assert_raises(ValueError, lambda: clf.coef_)
261+
assert_false(hasattr(clf, "coef_"))
261262

262263

263264
def test_oneclass_decision_function():
@@ -641,7 +642,8 @@ def test_linearsvc():
641642
assert_array_almost_equal(clf.intercept_, [0], decimal=3)
642643

643644
# the same with l1 penalty
644-
clf = svm.LinearSVC(penalty='l1', loss='squared_hinge', dual=False, random_state=0).fit(X, Y)
645+
clf = svm.LinearSVC(penalty='l1', loss='squared_hinge', dual=False,
646+
random_state=0).fit(X, Y)
645647
assert_array_equal(clf.predict(T), true_result)
646648

647649
# l2 penalty with dual formulation

0 commit comments

Comments
 (0)
0