Skip to content

[MRG + 1] Flip sign of SVM's dual_coef_ in a binary case #4326

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 4 commits into from
Mar 3, 2015

Conversation

artsobolev
Copy link
Contributor

This PR aims to fix #4262

For the details, please refer to the issue discussion.

Makes it consistent with other sign adjustments, adds
a test to reproduce decision_function in a kernel case
assert_array_equal(clf.predict([[-.1, -.1]]), [1])
clf.dual_coef_ = np.array([[.0, 1.]])
clf._dual_coef_ = np.array([[.0, 1.]])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this. Since this PR fixes sign of dual_coef_ in the same way intercept_ is fixed (which is done by introducing another property _intercept_), then it'd be a bit non-intuitive to set new dual coefficients, since a user would need to change _dual_coef_ instead of dual_coef_ (Should I document it somewhere?).

Though, this usecase seems very odd and rare to me in the first place, I can't imagine a case when I'd need to change dual coefficients.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could forbid overwriting dual_coef_ as we did with coef_. I'm not sure that is necessary, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not entirely sure this test tests something very useful ;)

@amueller
Copy link
Member

amueller commented Mar 2, 2015

This looks good to me. Please document the change in whatsnew, and also feel free to change the documentation mentioning -rho.

@amueller amueller added this to the 0.16 milestone Mar 2, 2015
@amueller amueller changed the title [MRG] Flip sign of SVM's dual_coef_ in a binary case [MRG + 1] Flip sign of SVM's dual_coef_ in a binary case Mar 3, 2015
clf.fit(X, Y)
norm = lambda x: np.apply_along_axis(np.linalg.norm, 1, x)
rbfs = [np.exp(-clf.gamma * norm(clf.support_vectors_ - X[i])**2)
for i in range(len(X))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you use:

from sklearn.metrics.pairwise import rbf_kernel

rbfs = rbf_kernel(X, clf.support_vectors_, gamma=clf.gamma)

instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know about this function, edited.

@@ -181,8 +181,11 @@ def fit(self, X, y, sample_weight=None):
# In binary case, we need to flip the sign of coef, intercept and
# decision function. Use self._intercept_ internally.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the comment to tell "Use self.intercept and self.dual_coef internally".

@ogrisel
Copy link
Member

ogrisel commented Mar 3, 2015

LGTM, thanks for the fix @Barmaley-exe. +1 once my last minor comment is addressed.

ogrisel added a commit that referenced this pull request Mar 3, 2015
[MRG + 1] Flip sign of SVM's dual_coef_ in a binary case
@ogrisel ogrisel merged commit f7efb1a into scikit-learn:master Mar 3, 2015
@ogrisel
Copy link
Member

ogrisel commented Mar 3, 2015

Merged, thanks!

@artsobolev artsobolev deleted the svm-dual-coef-fix branch March 3, 2015 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SVM decision function consistency
3 participants