Skip to content
Snippets Groups Projects
Commit 564495ab authored by Alexandre Gramfort's avatar Alexandre Gramfort
Browse files

adding docstring to LinearSVC and test to make sure the coef_ and intercept_...

adding docstring to LinearSVC and test to make sure the coef_ and intercept_ returned by SVC and LinearSVC are the same.

git-svn-id: https://scikit-learn.svn.sourceforge.net/svnroot/scikit-learn/trunk@704 22fbfee3-77ab-4535-9bad-27d1bd3bc7d8
parent 03c7d926
No related branches found
No related tags found
No related merge requests found
......@@ -261,23 +261,47 @@ class LinearSVC(object):
"""
Linear Support Vector Classification.
Similar to SVC with parameter kernel='linear', but uses internally
liblinear rather than libsvm, so it has more flexibility in the
choice of penalties and loss functions and should be faster for
huge datasets.
Parameters
----------
X : array-like, shape = [nsamples, nfeatures]
Training vector, where nsamples in the number of samples and
nfeatures is the number of features.
Y : array, shape = [nsamples]
Target vector relative to X
Also accepts parameter penalty, that can have values 'l1' or 'l2'
loss : string, 'l1' or 'l2' (default 'l2')
Specifies the loss function. With 'l1' it is the standard SVM
loss (a.k.a. hinge Loss) while with 'l2' it is the squared loss.
(a.k.a. squared hinge Loss)
Similar to SVC with parameter kernel='linear', but uses internally
liblinear rather than libsvm, so it has more flexibility in the
choice of penalties and loss functions and should be faster for
huge datasets.
penalty : string, 'l1' or 'l2' (default 'l2')
Specifies the norm used in the penalization. The 'l2'
penalty is the standard used in SVC. The 'l1' leads to coef_
vectors that are sparse.
dual : bool, (default True)
Specifies the norm used in the penalization. The 'l2'
penalty is the standard used in SVC. The 'l1' leads to coef_
vectors that are sparse.
TODO: wrap Cramer & Singer
References
----------
LIBLINEAR -- A Library for Large Linear Classification
http://www.csie.ntu.edu.tw/~cjlin/liblinear/
"""
_solver_type = {'l2l2_1': 1, 'l2l2_0' : 2, 'l2l1_1' : 3, 'l1l2_0' : 5}
def __init__(self, penalty='l2', loss='l2', dual=False, eps=1e-4, C=1.0):
def __init__(self, penalty='l2', loss='l2', dual=True, eps=1e-4, C=1.0):
s = penalty + loss + '_' + str(int(dual))
print s
try: self.solver_type = self._solver_type[s]
except KeyError:
raise ValueError('Not supported set of arguments')
......
......@@ -124,7 +124,7 @@ def test_LinearSVC():
assert_array_equal(clf.predict(T), true_result)
# the same with l1 penalty
clf = svm.LinearSVC(penalty='l1')
clf = svm.LinearSVC(penalty='l1', dual=False)
clf.fit(X, Y)
assert_array_equal(clf.predict(T), true_result)
......@@ -138,14 +138,17 @@ def test_LinearSVC():
clf.fit(X, Y)
assert_array_equal(clf.predict(T), true_result)
def test_coef_SVC_vs_LinearSVC():
def test_coef_and_intercept_SVC_vs_LinearSVC():
"""
Test that SVC and LinearSVC return the same coef_ and intercept_
"""
svc = svm.SVC(kernel='linear', C=1)
svc.fit(X, Y)
print svc.coef_
linsvc = svm.LinearSVC(C=1)
linsvc = svm.LinearSVC(C=1, penalty='l2', loss='l1', dual=True)
linsvc.fit(X, Y)
print linsvc.coef_
assert_array_equal(linsvc.coef_.shape, svc.coef_.shape)
assert_array_almost_equal(linsvc.coef_, svc.coef_, 5)
assert_array_almost_equal(linsvc.intercept_, svc.intercept_, 6)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment