8000 [MRG + 1] raise AttributeError in SVC.coef_ for proper duck-typing by amueller · Pull Request #8009 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG + 1] raise AttributeError in SVC.coef_ for proper duck-typing #8009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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