2013-12-17 27 views
4

我正在嘗試使用scikit-learn中的LinearSVC對象進行以下簡單分類。我試過使用版本0.10和0.14。使用代碼:LinearSVC爲什麼不能做這種簡單的分類?

from sklearn.svm import LinearSVC, SVC 
from numpy import * 

data = array([[ 1007., 1076.], 
       [ 1017., 1009.], 
       [ 2021., 2029.], 
       [ 2060., 2085.]]) 
groups = array([1, 1, 2, 2]) 

svc = LinearSVC() 
svc.fit(data, groups) 
svc.predict(data) 

我得到的輸出:

array([2, 2, 2, 2]) 

但是,如果我更換

svc = SVC(kernel='linear') 

分類,然後我得到的結果

array([ 1., 1., 2., 2.]) 

哪個是對的。有誰知道爲什麼使用LinearSVC會弄糟這個簡單的問題?

回答

13

基本LinearSVC的算法是在其輸入極值非常敏感:

>>> svc = LinearSVC(verbose=1) 
>>> svc.fit(data, groups) 
[LibLinear].................................................................................................... 
optimization finished, #iter = 1000 

WARNING: reaching max number of iterations 
Using -s 2 may be faster (also see FAQ) 

Objective value = -0.001256 
nSV = 4 
LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True, 
    intercept_scaling=1, loss='l2', multi_class='ovr', penalty='l2', 
    random_state=None, tol=0.0001, verbose=1) 

(警告指LibLinear常見問題,因爲scikit學習的LinearSVC是基於庫)

安裝前應進行標準化處理:

>>> from sklearn.preprocessing import scale 
>>> data = scale(data) 
>>> svc.fit(data, groups) 
[LibLinear]... 
optimization finished, #iter = 39 
Objective value = -0.240988 
nSV = 4 
LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True, 
    intercept_scaling=1, loss='l2', multi_class='ovr', penalty='l2', 
    random_state=None, tol=0.0001, verbose=1) 
>>> svc.predict(data) 
array([1, 1, 2, 2]) 
相關問題