Skip to content

Commit

Permalink
FIX raise AttributeError in SVC.coef_ for proper duck-typing (scikit-…
Browse files Browse the repository at this point in the history
  • Loading branch information
amueller authored and jnothman committed Dec 9, 2016
1 parent 5dd9bfe commit 9811b3b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sklearn/svm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ def _validate_for_predict(self, X):
@property
def coef_(self):
if self.kernel != 'linear':
raise ValueError('coef_ is only available when using a '
'linear kernel')
raise AttributeError('coef_ is only available when using a '
'linear kernel')

coef = self._get_coef()

Expand Down
6 changes: 4 additions & 2 deletions sklearn/svm/tests/test_svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_libsvm_iris():
for k in ('linear', 'rbf'):
clf = svm.SVC(kernel=k).fit(iris.data, iris.target)
assert_greater(np.mean(clf.predict(iris.data) == iris.target), 0.9)
assert_true(hasattr(clf, "coef_") == (k == 'linear'))

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

Expand Down Expand Up @@ -257,7 +258,7 @@ def test_oneclass():
assert_array_almost_equal(clf.dual_coef_,
[[0.632, 0.233, 0.633, 0.234, 0.632, 0.633]],
decimal=3)
assert_raises(ValueError, lambda: clf.coef_)
assert_false(hasattr(clf, "coef_"))


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

# the same with l1 penalty
clf = svm.LinearSVC(penalty='l1', loss='squared_hinge', dual=False, random_state=0).fit(X, Y)
clf = svm.LinearSVC(penalty='l1', loss='squared_hinge', dual=False,
random_state=0).fit(X, Y)
assert_array_equal(clf.predict(T), true_result)

# l2 penalty with dual formulation
Expand Down

0 comments on commit 9811b3b

Please sign in to comment.