Isa*_*aac 6 python libsvm scikit-learn liblinear
我正在尝试使用该LinearSVC对象进行以下简单分类scikit-learn.我尝试过同时使用0.10和0.14版本.使用代码:
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)
Run Code Online (Sandbox Code Playgroud)
我得到输出:
array([2, 2, 2, 2])
Run Code Online (Sandbox Code Playgroud)
但是,如果我用分隔符替换分类器
svc = SVC(kernel='linear')
Run Code Online (Sandbox Code Playgroud)
然后我得到了结果
array([ 1., 1., 2., 2.])
Run Code Online (Sandbox Code Playgroud)
哪个是对的.有谁知道为什么使用LinearSVC会破坏这个简单的问题?
Fre*_*Foo 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)
Run Code Online (Sandbox Code Playgroud)
(该警告涉及LibLinear常见问题解答,因为scikit-learn 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])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5729 次 |
| 最近记录: |