他人原创自学非转载
1、下载libsvm 3.22,更新日期为2016年12月 https://www.csie.ntu.edu.tw/~cjlin/libsvm/
Download LIBSVM
The current release (Version 3.22, December 2016) of LIBSVM can be obtained by downloading the zip file or tar.gz file. 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\\R2016b\\toolbox\\matlab\\libsvm-3.22和D:\\Program Files\\MATLAB\\R2015b\\toolbox\\matlab\\libsvm-3.22\\matlab都在路径下。 3、编译 确保安装有Visual Studio 2015版,matlab当前工作目录切换到D:\\Program Files\\MATLAB\\R2015b\\toolbox\\matlab\\libsvm-3.22下,在matlab命令行窗口下输入: >>mex -setup
> mex -setup
MEX 配置为使用 ‘Microsoft Visual C++ 2015 Professional (C)’ 以进行 C 语言编译。
警告: MATLAB C 和 Fortran API 已更改,现可支持
包含 2^32-1 个以上元素的 MATLAB 变量。不久以后,
您需要更新代码以利用
新的 API。您可以在以下网址找到相关详细信息:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html。
要选择不同的语言,请从以下选项中选择一种命令:
mex -setup C++
mex -setup FORTRAN
> mex -setup C++
MEX 配置为使用 ‘Microsoft Visual C++ 2015 Professional’ 以进行 C++ 语言编译。
警告: MATLAB C 和 Fortran API 已更改,现可支持
包含 2^32-1 个以上元素的 MATLAB 变量。不久以后,
您需要更新代码以利用
新的 API。您可以在以下网址找到相关详细信息:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html。
>
4、编译文件 matlab当前工作目录切换到D:\\Program Files\\MATLAB\\R2015b\\toolbox\\matlab\\libsvm-3.22\\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.
四 使用
关于LIBSVM在Matlab中的使用,可以参看软件包中matlab目录下的README文件,这里对里面内容做一个翻译和一些细节的讲解。
1. 训练
libsvm函数用于对训练集的数据进行训练,得到训练好的模型。 model = libsvmtrain(training_label_vector, training_instance_matrix [, ‘libsvm_options’]); 这个函数有三个参数,其中
- -training_label_vector:训练样本的类标,如果有m个样本,就是m x 1的矩阵(类型必须为double)。这里可以是二分类和多分类,类标是(-1,1)、(1,2,3)或者其他任意用来表示不同的类别的数字,要转成double类型。
- -training_instance_matrix:训练样本的特征,如果有m个样本,每个样本特征是n维,则为m x n的矩阵(类型必须为double)。
- -libsvm_options:训练的参数,在第3点详细介绍。
2. 预测
libpredict函数用于对测试集的数据进行测试,还能对未知样本进行预测。 [predicted_label, accuracy, decision_values/prob_estimates] = libsvmpredict(testing_label_vector, testing_instance_matrix, model [, ‘libsvm_options’]); 这个函数包括四个参数,其中
- -testing_label_vector:测试样本的类标,如果有m个样本,就是m x 1的矩阵(类型必须为double)。如果类标未知,可以初始化为任意m x 1的double数组。
- -testing_instance_matrix:测试样本的特征,如果有m个样本,每个样本特征是n维,则为m x n的矩阵(类型必须为double)。
- -model:使用libsvmtrain返回的模型
- -libsvm_options:预测的参数,与训练的参数形式一样。
3. 训练的参数
LIBSVM训练时可以选择的参数很多,包括:
- -s svm类型:SVM设置类型(默认0) 0 — C-SVC; 1 –v-SVC; 2 – 一类SVM; 3 — e-SVR; 4 — v-SVR
- -t 核函数类型:核函数设置类型(默认2) 0 – 线性核函数:u’v 1 – 多项式核函数:(r*u’v + coef0)^degree 2 – RBF(径向基)核函数:exp(-r|u-v|^2) 3 – sigmoid核函数:tanh(r*u’v + coef0)
- -d degree:核函数中的degree设置(针对多项式核函数)(默认3)
- -g r(gamma):核函数中的gamma函数设置(针对多项式/rbf/sigmoid核函数)(默认1/k,k为总类别数)
- -r coef0:核函数中的coef0设置(针对多项式/sigmoid核函数)((默认0)
- -c cost:设置C-SVC,e -SVR和v-SVR的参数(损失函数)(默认1)
- -n nu:设置v-SVC,一类SVM和v- SVR的参数(默认0.5)
- -p p:设置e -SVR 中损失函数p的值(默认0.1)
- -m cachesize:设置cache内存大小,以MB为单位(默认40)
- -e eps:设置允许的终止判据(默认0.001)
- -h shrinking:是否使用启发式,0或1(默认1)
- -wi weight:设置第几类的参数C为weight*C (C-SVC中的C) (默认1)
- -v n: n-fold交互检验模式,n为fold的个数,必须大于等于2
以上这些参数设置可以按照SVM的类型和核函数所支持的参数进行任意组合,如果设置的参数在函数或SVM类型中没有也不会产生影响,程序不会接受该参数;如果应有的参数设置不正确,参数将采用默认值。
4. 训练返回的内容
libsvmtrain函数返回训练好的SVM分类器模型,可以用来对未知的样本进行预测。这个模型是一个结构体,包含以下成员:
- -Parameters: 一个5 x 1的矩阵,从上到下依次表示: -s SVM类型(默认0); -t 核函数类型(默认2) -d 核函数中的degree设置(针对多项式核函数)(默认3); -g 核函数中的r(gamma)函数设置(针对多项式/rbf/sigmoid核函数) (默认类别数目的倒数); -r 核函数中的coef0设置(针对多项式/sigmoid核函数)((默认0)
- -nr_class: 表示数据集中有多少类别,比如二分类时这个值即为2。
- -totalSV: 表示支持向量的总数。
- -rho: 决策函数wx+b中的常数项的相反数(-b)。
- -Label: 表示数据集中类别的标签,比如二分类常见的1和-1。
- -ProbA: 使用-b参数时用于概率估计的数值,否则为空。
- -ProbB: 使用-b参数时用于概率估计的数值,否则为空。
- -nSV: 表示每类样本的支持向量的数目,和Label的类别标签对应。如Label=[1; -1],nSV=[63; 67],则标签为1的样本有63个支持向量,标签为-1的有67个。
- -sv_coef: 表示每个支持向量在决策函数中的系数。
- -SVs: 表示所有的支持向量,如果特征是n维的,支持向量一共有m个,则为m x n的稀疏矩阵。
另外,如果在训练中使用了-v参数进行交叉验证时,返回的不是一个模型,而是交叉验证的分类的正确率或者回归的均方根误差。
5. 预测返回的内容
libsvmtrain函数有三个返回值,不需要的值在Matlab可以用~进行代替。
- -predicted_label:第一个返回值,表示样本的预测类标号。
- -accuracy:第二个返回值,一个3 x 1的数组,表示分类的正确率、回归的均方根误差、回归的平方相关系数。
- -decision_values/prob_estimates:第三个返回值,一个矩阵包含决策的值或者概率估计。对于n个预测样本、k类的问题,如果指定“-b 1”参数,则n x k的矩阵,每一行表示这个样本分别属于每一个类别的概率;如果没有指定“-b 1”参数,则为n x k*(k-1)/2的矩阵,每一行表示k(k-1)/2个二分类SVM的预测结果。
6. 读取或保存
libsvmread函数可以读取以LIBSVM格式存储的数据文件。 [label_vector, instance_matrix] = libsvmread(‘data.txt’); 这个函数输入的是文件的名字,输出为样本的类标和对应的特征。 libsvmwrite函数可以把Matlab的矩阵存储称为LIBSVM格式的文件。 libsvmwrite(‘data.txt’, label_vector, instance_matrix] 这个函数有三个输入,分别为保存的文件名、样本的类标和对应的特征(必须为double类型的稀疏矩阵)。