1. 参考网站:
libsvm库下载:http://www.csie.ntu.edu.tw/~cjlin/libsvm/
视频:http://v.youku.com/v_showMini/id_XMjc2NTY3MzYw_ft_131.html(重点参考)
详解:http://www.matlabsky.com/thread-11925-1-1.html
2. 操作流程:
请注意:详细操作流程请参考上面的“详解”网站,这里只说大框架和详解里没有提到的问题。
A.设置path(注意是subfolders)
File->set path ->add with subfolders->加入libsvm-3.20文件夹的路径
B. 在matlab中编译
目的:将libsvm-3.20\matlab 中 libsvmwrite.c 等 C++文件编译成 libsvmread.mexw64等matlab文件,这样就可以在command window中被直接调用了。
注意:在最外面的Readme中有提到已经有编译好的文件,比如在libsvm-3.20\windows中也会看到libsvmread.mexw64,但这里不要被误导!还是需要你自己再编译一遍的!
C.加载数据集
有两个数据集,一个是C++的, 一个是matlab的。libsvm库中下载的是C++数据,
所以matlab加载我们下载的heart_scale是会报错的。需要转换成mat文件。可以用libsvmread而非load,然后保存为mat文件
1 2 3 4 5 6 7 8 9 10 |
>> load heart_scale >> model = svmtrain(heart_scale_label,heart_scale_inst); * optimization finished, #iter = 162 nu = 0.431029 obj = -100.877288, rho = 0.424462 nSV = 132, nBSV = 107 Total nSV = 132 >> [predict_label, accuracy,decision_values] = svmpredict(heart_scale_label, heart_scale_inst, model,'-b 0'); Accuracy = 86.6667% (234/270) (classification) |
注意:这里如果使用下列语句,无法得到结果。
1 |
[predict_label,accuracy] = svmpredict(heart_scale_label,heart_scale_inst,model); |
3.20版本的svmpredict作了修改,必须在[predict_label,accuracy] = svmpredict(heart_scale_label,heart_scale_inst,model);的基础上修改才能得到accuracy。
下面转载自:http://blog.csdn.net/abcjennifer/article/details/7370177
============MAC 版如何在matlab中使用libsvm==================
下面说下mac怎么用libsvm,这里的问题是mex -setup的问题,需要安装一个补丁。
1. mac中安装xcode
2. xcode的preference中装command line tools
3. 下载最新补丁http://www.mathworks.cn/support/solutions/en/data/1-FR6LXJ/
4. 在matlab命令行输入>>cd(matlabroot)
5. 在matlab命令行输入这个打补丁
>>!unzip -o ~/Downloads/optsPatch_MACOSX8.patch
6. 可以运行mex -setup了,选择将原来的gcc覆盖掉
7. 到libsvm/matlab进行make
就搞定啦!
具体参考:http://bbs.weiphone.com/read-htm-tid-5787323.html
4、深入阅读README文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
`svm-train' Usage ================= 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 : set type of kernel function (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 The k in the -g option means the number of attributes in the input data. option -v randomly splits the data into n parts and calculates cross validation accuracy/mean squared error on them. |
linear:线性核函数(linear kernel)
polynomial:多项式核函数(ploynomial kernel)
RBF:径向基核函数(radical basis function)
sigmoid: 神经元的非线性作用函数核函数(Sigmoid tanh)
precomputed :用户自定义核函数
究竟用哪一种核函数取决对数据处理的要求,不过建议一般都是使用RBF核函数。因为RBF核函数具有良好的性态,在实际问题中表现出了良好的性能。
1 2 3 4 5 6 7 8 9 10 11 |
`svm-predict' Usage =================== 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 model file generated by svm-train. test_file is the test data you want to predict. svm-predict will produce output in the output_file. |
这里的probablity estimate是什么意思。需要进一步了解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
Precomputed Kernels =================== Users may precompute kernel values and input them as training and testing files. Then libsvm does not need the original training/testing sets. Assume there are L training instances x1, ..., xL and. Let K(x, y) be the kernel value of two instances x and y. The input formats are: New training instance for xi: <label> 0:i 1:K(xi,x1) ... L:K(xi,xL) New testing instance for any x: <label> 0:? 1:K(x,x1) ... L:K(x,xL) That is, in the training file the first column must be the "ID" of xi. In testing, ? can be any value. All kernel values including ZEROs must be explicitly provided. Any permutation or random subsets of the training/testing files are also valid (see examples below). Note: the format is slightly different from the precomputed kernel package released in libsvmtools earlier. Examples: Assume the original training data has three four-feature instances and testing data has one instance: 15 1:1 2:1 3:1 4:1 45 2:3 4:3 25 3:1 15 1:1 3:1 If the linear kernel is used, we have the following new training/testing sets: 15 0:1 1:4 2:6 3:1 45 0:2 1:6 2:18 3:0 25 0:3 1:1 2:0 3:1 15 0:? 1:2 2:0 3:1 ? can be any value. Any subset of the above training file is also valid. For example, 25 0:3 1:1 2:0 3:1 45 0:2 1:6 2:18 3:0 implies that the kernel matrix is [K(2,2) K(2,3)] = [18 0] [K(3,2) K(3,3)] = [0 1] |