1、下载libsvm 3.21,更新日期为2015年12月14日
https://www.csie.ntu.edu.tw/~cjlin/libsvm/
Download LIBSVM
The current release (Version 3.21, December 2015) of LIBSVM can be obtained by downloading the zip fileor tar.gzfile. You can also check this github directory. Please e-mail us if you have problems to download the file.
The package includes the source code of the library in C++ and Java, and a simple program for scaling training data. A README file with detailed explanation is provided. For MS Windows users, there is a sub-directory in the zip file containing binary executable files. Precompiled Java class archive is also included.
Please read the COPYRIGHT notice before using LIBSVM.
2、添加路径
将工具包放到任何地方均可(建议放在D:\Program Files\MATLAB\R2015b\toolbox\matlab下),将工具包添加到Matlab的搜索路径,Set Path->add with subfolders->save。
备注:保证D:\Program Files\MATLAB\R2015b\toolbox\matlab\libsvm-3.21和D:\Program Files\MATLAB\R2015b\toolbox\matlab\libsvm-3.21\matlab都在路径下。
3、编译
确保安装有Visual Studio 2013版,matlab当前工作目录切换到D:\Program Files\MATLAB\R2015b\toolbox\matlab\libsvm-3.21下,在matlab命令行窗口下输入:
>>mex -setup
4、编译文件
matlab当前工作目录切换到D:\Program Files\MATLAB\R2015b\toolbox\matlab\libsvm-3.21\matlab目录下,在matlab命令行窗口下输入命令,若显示MEX completed successfully.表示安装成功:
>>make
5、测试
在libsvm-3.21可以看到hear_scale 文件,在命令行输入下面几行代码
clear;
[label_vector, instance_matrix] =libsvmread(‘heart_scale‘);
model = svmtrain(label_vector, instance_matrix);
[predicted_label, accuracy, prob_estimates] = svmpredict(label_vector, instance_matrix, model, ‘b‘);
备注参考:
options: -s svm_type : set type of SVM (default 0) 0 -- C-SVC 1 -- nu-SVC 2 -- one-class SVM 3 -- epsilon-SVR 4 -- nu-SVR -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) -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) The k in the -g option means the number of attributes in the input data.