转载请注明出处!!!http://blog.csdn.net/zhonghuan1992
LIBSVM使用
LIBSVM是一个简单,容易使用和有效的SVM库(关于SVM的介绍,可以去看一下SVM的三层境界),LIBSVM可以在http://www.csie.ntu.edu.tw/~cjlin/libsvm下载使用。最新的版本是2014愚人节更新的3.18。
想快速使用LIBSVM的话,可以在tool目录下使用easy.py,它会帮你自动做全部事情,数据的比例放缩和参数的选择。使用:
Usage: easy.py training_file [testing_file]
对于参数选择的问题,可以看tool下的readme。
SVM-toy图形接口的使用
SVM-toy是一个简单的图形工具,可以用它来可视化SVM是如何分割数据的。它在window目录下,已经被编译。在windows系统下直接双击就可运行。它的界面如下:
可以看到这个图形界面,它比较简单,在上面可以画点,不同类别的点颜色不一样,change按钮可以用来改变类别。Load可以用来导入数据,数据形式请看libsvm下的readme。Clear用来清空界面。点击Run,可以获得SVM模型。示例请看下面:
我画了3类的点,所以SVM分了3类。这里最多也是支持3类。
C++的使用
SVM-scale:
顾名思义,用来进行数据放缩。进行缩放的原因和使用神经网络时的考虑是一样的,由于RBF网络中采用样本数据的欧式距离来计算。主要优点就是避免数值范围较大的属性控制数值范围较小的属性。另一个优点就是避免计算时的numerical difficulties. 因为核值通常依赖特征向量的内积(innerproduct),而较大的属性值可能导致numerical问题。因此推荐把每个属性缩放到[-1, 1]或者[0, 1]之间,而且前一个范围要比后一个好,即对列向量进行规范化,其详细解释和计算公式见http://www.faqs.org/faqs/ai-faq/neural-nets/part2/
用法: svm-scale [options] data_filename
options:
-l lower : x scaling lower limit(default -1)
-u upper : x scaling upper limit(default +1)
-y y_lower y_upper : y scaling limits(default: no y scaling)
-s save_filename : save scalingparameters to save_filename
-r restore_filename : restore scalingparameters from restore_filename
SVM-train
SVM-train就是用来训练用,具体的一些选择可以看下面的用法,SVM的原理不了解,可以看SVM的三层境界。
用法: svm-train [options] training_set_file [model_file]
options:
-s svm_type : set type of SVM (default 0)
0-- C-SVC (multi-classclassification)
1-- nu-SVC (multi-classclassification)
2-- one-class SVM
3-- epsilon-SVR (regression)
4-- nu-SVR (regression)
-t kernel_type : set type of kernelfunction (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 lossfunction of epsilon-SVR (default 0.1)
-m cachesize : set cache memory size in MB(default 100)
-e epsilon : set tolerance of terminationcriterion (default 0.001)
-h shrinking : whether to use the shrinkingheuristics, 0 or 1 (default 1)
-b probability_estimates : whether to traina SVC or SVR model for probability estimates, 0 or 1 (default 0)
-wi weight : set the parameter C of class ito weight*C, for C-SVC (default 1)
-v n: n-fold cross validation mode
-q : quiet mode (no outputs)
SVM-predict:
SVM-predict,用来通过上面train得到的模型,来预测结果。用法如下:
用法: svm-predict [options] test_file model_file output_file
options:
-b probability_estimates: whether topredict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 issupported
model_file is the model file generated bysvm-train.
test_file is the test data you want topredict.
svm-predict will produce output in theoutput_file.
实例使用,使用的数据是libsvm下的heart-scale。
MatLab下的使用
在Matlab中的配置只需要按照matlab目录下的readme中所说的进行操作就好了.在matlab中添加path,如果编译有问题的话.可以直接把windows目录的编译好的libsvmread.mexw64,libsvmwrite.mexw64,svmpredict.mexw64直接拿来用,因为我的visual studio的版本是2013,matlab版本也是,matlab不识别2013的visual studio,所以直接把window下的文件拿来用了,这里无关紧要。下面介绍下函数的用法。
matlab> model = svmtrain(training_label_vector,training_instance_matrix [, 'libsvm_options']);
-training_label_vector:
An m by 1 vector of training labels (type must be double).
-training_instance_matrix:
An m by n matrix of m training instances with n features.
It can be dense or sparse (type must be double).
-libsvm_options:
A string of training options in the same format as that of LIBSVM.
matlab> [predicted_label, accuracy,decision_values/prob_estimates] = svmpredict
(testing_label_vector, testing_instance_matrix, model[, 'libsvm_options']);
matlab> [predicted_label] =svmpredict(testing_label_vector, testing_instance_matrix, model [,'libsvm_options']);
-testing_label_vector:
An m by 1 vector of prediction labels. If labels of test
data are unknown, simply use any random values. (type must be double)
-testing_instance_matrix:
An m by n matrix of m testing instances with n features.
It can be dense or sparse. (type must be double)
-model:
The output of svmtrain.
-libsvm_options:
A string of testing options in the same format as that of LIBSVM.
[label_vector, instance_matrix] =libsvmread('data.txt');
libsvmwrite('data.txt', label_vector, instance_matrix]
样例使用如下: