预备
正文
a 编译libsvm
uname@hname
:
~/libsvm
$ make
g++ -Wall -Wconversion -O3 -fPIC -c svm.cpp
g++ -Wall -Wconversion -O3 -fPIC svm-train.c svm.o -o svm-train -lm
g++ -Wall -Wconversion -O3 -fPIC svm-predict.c svm.o -o svm-predict -lm
g++ -Wall -Wconversion -O3 -fPIC svm-scale.c -o svm-scale
b svmguide1 (类别平衡数据)
代码wrap.py, 详见附录.
-
$ python3 wrap.py --kernel rbf --C 1
Accuracy = 66.925% (2677/4000) (classification)
-
$ python3 wrap.py --kernel rbf --C 1 --scale
Accuracy = 96.15% (3846/4000) (classification)
-
$ python3 wrap.py --kernel linear --C 1
Accuracy = 95.675% (3827/4000) (classification)
$ python3 wrap.py --kernel linear --C 1 --scale
Accuracy = 95.675% (3827/4000) (classification)
-
$ python3 wrap.py --kernel rbf --C 1000
Accuracy = 70.475% (2819/4000) (classification)
$ python3 wrap.py --kernel rbf --C 1000 --scale
Accuracy = 96.725% (3869/4000) (classification)
-
$ python3 easy.py ../../data/svmguide1.tr ../../data/svmguide1.te
Best c=2.0, g=2.0 CV rate=96.9893
Accuracy = 96.875% (3875/4000) (classification)
结论
- rbf核svm对数据规范化敏感, 线性核svm对数据规范化不敏感.
- 可以通过调整超参数(正则化参数)提高(rbf核)svm的性能.
- 调参(搜索最优参数)需要网格搜索(grid search), 代价是巨大的, 训练测试分类器的次数关于需要调的参数的个数是指数级增长的.
注意easy.py需要修改一行代码
52c52
< cmd = '{0} -svmtrain "{1}" -gnuplot "{2}" "{3}"' ...
---
> cmd = 'python3 {0} -svmtrain "{1}" -gnuplot "{2}" "{3}"' ...
否则需要执行$ chmod +777 /path/to/grid.py
.
c shuttle 类别不平衡数据
代码peek.py, 详见附录.
dataset |
label count |
note |
svmguide1 |
{1: 4000, 0: 3089} |
balance |
shuttle |
{1: 35033, 4: 6906, 5: 2525, 3: 133, 2: 37, 7: 11, 6: 6} |
imbalance |
代码wrap.py, 详见附录.
$ python3 wrap.py --dataset shuttle
Accuracy = 98.0017% (1128/1151) (classification)
$ python3 wrap.py --dataset shuttle --weight_class
Accuracy = 85.0565% (979/1151) (classification)
根据{1: 35033, 4: 6906, 5: 2525, 3: 133, 2: 37, 7: 11, 6: 6}, 输入一组参数
-w4 5.0728352157544165 (== 35033 / 6906)
-w5 13.874455445544555 (== 35033 / 2525)
-w3 263.406015037594 (== 35033 / 133)
-w2 946.8378378378378 (== 35033 / 37)
-w7 3184.818181818182 (== 35033 / 11)
-w6 5838.833333333333 (== 35033 / 6)
这一组参数十分夸张, 准确率大幅下降了. 但是如果我们仔细观察预测输出和真实标签, 我们就发现模型在较多的类上错误增加, 但是在较少的类上错误减少.
代码tmp.py, 详见附录.
argument |
error |
with weight_class |
{1: 167, 4: 5} |
without weight_class |
{1: 9, 4: 13, 3: 1} |
启发式的输入另一组参数
-w4 1.025
-w3 1.005
好吧, 只多正确分类一个
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)