In mathematical sense you need to set:
SVC(kernel="linear", **kwargs) # by default it uses RBF kernel
and
LinearSVC(loss="hinge", **kwargs) # by default it uses squared hinge loss
Another element, which cannot be easily fixed is increasing intercept_scaling
in LinearSVC
, as in this implementation bias is regularized (which is not true in SVC nor should be true in SVM – thus this is not SVM) – consequently they will never be exactly equal (unless bias=0 for your problem), as they assume two different models
- SVC :
1/2||w||^2 + C SUM xi_i
- LinearSVC:
1/2||[w b]||^2 + C SUM xi_i
Personally I consider LinearSVC one of the mistakes of sklearn developers – this class is simply not a linear SVM.
After increasing intercept scaling (to 10.0
)
However, if you scale it up too much – it will also fail, as now tolerance and number of iterations are crucial.
To sum up: LinearSVC is not linear SVM, do not use it if do not have to.