主要内容:在linux上下载使用LIBSVM(C语言和Python)
LIBSVM是台湾大学林智仁博士等人开发设计的一个操作简单、易于使用、快速有效的通用SVM软件包,可用于解决分类问题和回归问题,提供了线性、多项式、径向基和Sigmoid函数四种常用的核函数以供选择,此外还能自行定义核函数。
1、安装
软件包地址是:http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm.cgi?+http://www.csie.ntu.edu.tw/~cjlin/libsvm+zip
linux系统:下载完成后解压,进入libsvm文件夹输入‘make’命令编译即可使用。
2、基本功能使用方法
(1)数据格式:<label> <index1>:<value1> <index2>:<value2>…
可以通过tools/checkdata.py文件来检查数据的格式是否正确。
(2)基本函数
- 'svm-scale' Usgae: svm-scale [options] data_filename
options:
-l lower : 数据放缩的下界(default=-1)
-u upper: 数据放缩的上界(default=+1)
-y y_lower y_upper: y放缩的界限(默认不对y进行放缩)
-s save_filename: 存储放缩操作参数的文件
-r restore_filename: 读入放缩操作参数的文件
- 'svm-train' Usage: svm-train [options] training_set_file [model_file]
options:
-s svm_type: set type of SVM(default = 0)
0 -- C-SVC (multi-class classification)
1 -- nu-SVC (multi-class classification)
2 -- one-class SVM
3 -- epsilon-SVR (regression)
4 -- nu-SVR (regression)
-t kernel_type: 核函数的类型(default=2)
0 -- linear: u'*v
1 -- polynomial: (gamma*u'*v + coef0)^degree
2 -- radial basis function: exp(-gamma*|u-v|^2)
3 -- sigmoid: tanh(gamma*u'*v + coef0)
4 -- precomputed kernel (kernel values in training_set_file)
-d degree : set degree in kernel function (default 3)
-g gamma : set gamma in kernel function (default 1/num_features)
-r coef0 : set coef0 in kernel function (default 0)
-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
-m cachesize : set cache memory size in MB (default 100)
-e epsilon : set tolerance of termination criterion (default 0.001)
-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)
-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)
-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)
-v n: n-fold cross validation mode
-q : quiet mode (no outputs)
- 'svm-predict' Usage: svm-predict [options] test_file model_file output_file
options:
-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 is supported
model_file is the path of the model generated by svm-train.
test_file is the path of the test data.
output_file is the path to which the result outputs.
(3)使用实例
svm-scale -l -1 -u 1 -s range train > train.scale
svm-scale -r range test > test.scale
svm-train -s 0 -t 2 -c 5- g 0.5 train.scale
svm-predict -b 1 test_file model output_file
3、Python的使用接口
相关文件在libsvm目录下的python文件夹中,通过‘from svmutil import *’将函数加载进来。包括如下函数:
- svm_train() 用于训练SVM模型
model = svm_train(y, x [,training_options])
model = svm_train(prob, param)
prob: 创建一个svm问题,通过svm_problem(y, x)
param: 创建一组svm的参数,通过svm_parameter('training options')
- svm_predict()
p_labs, p_acc, p_vals = svm_predict(y, x, model [,'predicting_options'])
- svm_load_model()
- svm_save_model()
m = svm_load_model(model_file)
svm_save_model(model_file, model)
4、调参的心得
一般来说首先根据问题选定参数‘-s’,之后主要需要调整的就是核函数的类型‘-t’,选定核函数之后根据核函数的相关参数来调整'-d', '-g'和‘-r’.
以上只是列出了我自己一些平时常用的函数和参数,更多详细的使用方法和技巧请参见: