LIBSVM工具包的训练函数使用----Matlab版本
采用多项式核的SVM进行二分类
这里采用的仍然是上一篇文章的数据集"satimage"。
1. 数据预处理
1.1 归一化
这里采用matlab的full函数进行处理,将稀疏矩阵转化为矩阵,再进行归一化。
存在两种归一化的方法,一种是使用mapminmax将特征映射至[0,1]区间,一种是使用normalize(),下面使用normalize:
clear
clc
%% 导入数据
%根据网站,总训练数据拆分成了train和validation两部分
clear
clc
%% 导入数据
%根据网站,总训练数据拆分成了train和val两部分
[LabelOfAll,FeatureOfAll] = libsvmread('satimage.scale.txt'); %总数据(4435):tr:3104 / val:1331
[LabelOfVal,FeatureOfVal] = libsvmread('satimage.scale.val.txt'); % val数据
[LabelOfTrain,FeatureOfTrain] = libsvmread('satimage.scale.train.txt');% train数据
[LabelOfTest,FeatureOfTest] = libsvmread('satimage.scale.testing.txt');% test数据
%% 稀疏阵转满阵
FeatureOfAllMatrix = full(FeatureOfAll);
FeatureOfTrainMatrix = full(FeatureOfTrain);
FeatureOfValMatrix = full(FeatureOfVal);
FeatureOfTestMatrix=full(FeatureOfTest);
%% 合并测试集与训练集(任务要求) && 归一化处理
FeatureOfAllMatrix_Minmax = normalize([FeatureOfValMatrix',FeatureOfTrainMatrix',FeatureOfTestMatrix']);
FeatureOfAllMatrix_Minmax = FeatureOfAllMatrix_Minmax';
FeatureOfValAndTrainMatrix_Minmax = FeatureOfAllMatrix_Minmax(1:4435,:);
FeatureOfTestMatrix_Minmax = FeatureOfAllMatrix_Minmax(4435:end,:);
LabelOfValAndTrain=[LabelOfVal;LabelOfTrain];
值得考究的是,部分文献认为使用normalize进行的归一化效果会好于单纯地将数据映射到[0,1]区间(即使用mapminmax函数)。就这个数据集来看,作者检验发现normalize归一化的效果确实是好于mapminmax归一化的(86.9899% && 71.1795%),当然有不同效果的朋友欢迎私信作者,一起讨论。以下文章内容仍以normalize函数进行归一化。
从矩阵来看,这个数据集也并不是特别稀疏:

1.2解读训练函数svmtrain
因为matlab自带svm函数,实在是难找到教学网站的训练参数,因此我们可以尝试性地使用它,便能得到参数信息。
例如:输入
svmtrain('How Dare You! SVM')
我们便能得到如下反馈:
Usage: model = svmtrain(training_label_vector, training_instance_matrix, 'libsvm_options');
libsvm_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_instance_matrix)
-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)
我们粗略翻译一下:
语法: model = svmtrain(training_label_vector, training_instance_matrix, 'libsvm_options');
libsvm_options(选择参数):
-s SVM类型 : 设置SVM类型 (默认 0)
0 -- C-SVC (多类别分类器)
1 -- nu-SVC (多类别分类器)
2 -- one-class SVM
3 -- epsilon-SVR (用于回归)
4 -- nu-SVR (用于回归)
-t 核函数类型 : 设置核函数类型 (默认 2)
0 -- 线性核函数: u'*v
1 -- 多项式核函数: (gamma*u'*v + coef0)^degree
2 -- RBF径向基函数: exp(-gamma*|u-v|^2)
3 -- sigmoid核函数: tanh(gamma*u'*v + coef0)
4 -- 用户自定义核函数
-d degree : 设置多项式核函数的 degree 值 (默认 3)
-g gamma : 设置核函数的 gamma 值 (默认 1/num_features)
-r coef0 : 设置核函数 coef0 值(默认 0)
-c cost : 设置部分分类器(C-SVC, epsilon-SVR, nu-SVR)的 C 值 (默认 1)
-n nu : 设置部分分类器(nu-SVC, one-class SVM, nu-SVR)的 nu 值 (默认 0.5)
-p epsilon : 设置分类器epsilon-SVR的 epsilon 值 (默认 0.1)
-m cachesize : 设置使用的高速缓存大小(单位:MB) (默认 100)
-e epsilon : 设置结束阈值 ε (默认 0.001)
-h shrinking : 是否使用启发式, 0 或 1 (默认 1)
-b probability_estimates : 对一个SVC或SVR模型使用概率估计, 0 或 1 (默认 0)
-wi weight : 设置分类器C-SVC对某类别权重,例如第i类权重为 weight*c (默认 1)
-v n: n折交叉检验(n>=2)
-q : 没有输出的模型
2.训练模型
2.1 解读输出参数
我们输入如下指令训练模型:
model=svmtrain(LabelOfTestAndTrain,FeatureOfTestAndTrainMatrix_Minmax);
可以得到如下的反馈
optimization finished,
#iter = 154 //iteration,迭代次数
nu = 0.103471 //核函数类型参数
obj = -133.931857, //二次规划求得的最小值
rho = 0.945455 //判决函数决定偏置
nSV = 197, //支持向量数目
nBSV = 186 //边界上的支持向量数目
2.2 训练模型
我们使用多项式核,degree设为3,c设为1,同时进行十折检验。因为尝试发现返回的SVM信息实在是太多了,所以作者还开了不返回详细信息指令(-q)。具体指令如下:
Accuracy=svmtrain(LabelOfValAndTrain,FeatureOfValAndTrainMatrix_Minmax,'-t 1 -d 3 -c 1 -v 10 -q')
返回结果:
Cross Validation Accuracy = 86.9899%
mapminmax归一化返回结果:
Cross Validation Accuracy = 71.1795%
可以发现此时返回的结果是交叉验证的准确性(Cross Validation Accuracy)。如果改变参数,返回的会是一个用于预测的结构体。

浙公网安备 33010602011771号