libsvm实现分类与回归(附参数详细说明、程序、数据集)

libsvmtrain以及libsvmpredict的相关参数说明

(1)model= svmtrain(train_label, train_matrix, ['libsvm_options']);
% train_label表示训练集的标签。
% train_matrix表示训练集的属性矩阵。
% libsvm_options是需要设置的一系列参数。
% model:是训练得到的模型,是一个结构体(如果参数中用到-v,得到的就不是结构体,对于分类问题,得到的是交叉检验下的平均分类准确率;对于回归问题,得到的是均方误差)。
(2)[predicted_label, accuracy/mse,decision_values/prob_estimates]=svmpredict(test_label, test_matrix, model, ['libsvm_options']);
% test _label表示测试集的标签(可以没有)。
% test _matrix表示测试集的属性矩阵。
% model   是上面训练得到的模型。
% libsvm_options是需要设置的一系列参数。
% predicted_label表示预测得到的标签。
% accuracy/mse是一个3*1的列向量,其中第1个数字用于分类问题,表示分类准确率;后两个数字用于回归问题,第2个数字表示mse;第三个数字表示平方相关系数。
% decision_values/prob_estimates:第三个返回值,一个矩阵包含决策的值或者概率估计。
% 对于n个预测样本、k类的问题,如果指定“-b 1”参数,则n x k的矩阵,每一行表示这个样本分别属于每一个类别的概率;如果没有指定“-b 1”参数,则为n x k*(k-1)/2的矩阵,每一行表示k(k-1)/2个二分类SVM的预测结果。

%训练参数的选择
% -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)%回归问题
% %前3类为分类问题,后2类为回归问题
% -t kernel_type : set type of kernel function (default 2)%核函数设置类型(默认2)
% 0-- linear: u'*v%线性核函数
% 1-- polynomial: (gamma*u'*v + coef0)^degree%多项式核函数
% 2-- radial basis function: exp(-gamma*|u-v|^2)%RBF(径向基)核函数
% 3-- sigmoid: tanh(gamma*u'*v + coef0)%sigmoid核函数
% 4 -- precomputed kernel (kernel values in training_set_file)%预计算内核(training_set_文件中的内核值)
% -d degree : set degree in kernel function (default 3)%核函数中的degree设置(针对多项式核函数)
% -g gamma : set gamma in kernel function (default 1/num_features)%核函数中的gamma函数设置,针对多项式/rbf/sigmoid核函数(默认为1/看,k为总类别)
% -r coef0 : set coef0 in kernel function (default 0)%核函数中的coef0设置(针对多项式和sigmoid函数)
% -c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)%设置C-SVC,e -SVR和v-SVR的参数(损失函数)(默认1)
% -n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)%设置v-SVC,one-class SVM和v- SVR的参数(默认0.5)
% -p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)%设置e -SVR 中损失函数p的值(默认0.1)
% -m cachesize : set cache memory size in MB (default 100)%设置cache内存大小,以MB为单位(默认100)
% -e epsilon : set tolerance of termination criterion (default 0.001)%设置允许的终止判据(默认0.001)
% -h shrinking: whether to use the shrinking heuristics, 0 or 1 (default 1)%是否使用启发式,0或1(默认1)
% -b probability_estimates: whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)%是否训练SVC或SVR模型进行概率估计
% -wi weight: set the parameter C of class i to weight*C, for C-SVC (default 1)%设置第几类的参数C为weight*C (C-SVC中的C) (默认1)
% -v n: n-fold cross validation mode%n-fold交互检验模式,n为fold的个数,必须大于等于2【随机将数据拆分为n个部分并计算交叉验证精度/它们的均方误差】
% -q : quiet mode (no outputs) %停止模式(没有输出)

%model参数
%model.Parameters参数意义:
% -s svm类型:SVM设置类型(默认0)
% -t 核函数类型:核函数设置类型(默认2)
% -d degree:和函数中的degree设置(针对多项式核函数)(默认3)
% -g gama:核函数中的gamma函数设置(针对多项式/rbf/sigmoid核函数)(默认类别数目的倒数)
% -r coef0:核函数中的coef0设置(针对多项式/sigmoid核函数)(默认为0)
% model.Label:表示数据集中类别的标签
% model.nr_class:表示数据集中有多少标签 
% model.totalSV:代表总共的支持向量机的数目
% model.nSV:表示每类样本的支持向量的数目
% model.ProbA,model.ProbB:
% model.sv_coef:表示支持向量在决策函数中的系数
% model.SVs:表示支持向量
% model.rho:表示决策函数中的常数项的相反数(-b)
% model.sv_indices:表示支持向量在数据集中的位置

%分类命令行输出参数:
% iter:迭代次数
% nu: 与前面的操作参数-n nu 相同
% obj:为SVM问题转换为的二次规划求解得到的最小值
% rho:表示决策函数中的常数项的相反数(-b)
% nSV:标准支持向量个数,就是在分类的边界上,松弛变量等于0,拉格朗日系数 0=

svm回归的例程:训练函数为传递函数(MATLAB实现)

安装libsvm-3.23详细配置文档说明参考博客:https://www.cnblogs.com/Ran-Chen/p/9462825.html

close all;
clear;
clc;
load hahasvmtrain.mat;
model = libsvmtrain(z,t','-s 4 -t 2 -c 5 -g 2.8 -p 0.006');
% 利用建立的模型看其在训练集合上的回归效果
[py,accuracy,decision_values] =libsvmpredict(z,t',model);
figure(1)
plot(t,z','r--','linewidth',3);
hold on
plot(t,py','b-','linewidth',1);
title('支持向量机辨识')
xlabel('系统输入')
ylabel('系统输出')
legend('原始函数输出','支持向量机辨识测试输出')
figure(2)
plot(t,z'-py')
title('误差曲线')
xlabel('系统输入')
ylabel('误差值')

libsvm实现分类与回归(附参数详细说明、程序、数据集)_第1张图片

libsvm实现分类与回归(附参数详细说明、程序、数据集)_第2张图片

svm分类的例程:实现0-9数字图片识别(使用的训练集为mnist)

clear;
clc;
load('mnist_train.mat');
mnist_train=double(mnist_train/256);
load('mnist_train_labels.mat');
mnist_train_labels=double(mnist_train_labels);
load('mnist_test.mat');
mnist_test=double(mnist_test/256);
load('mnist_test_labels.mat');
mnist_test_labels=double(mnist_test_labels);
model = libsvmtrain(mnist_train_labels,mnist_train , '-c 1 -g     0.01');
[predict_label, accuracy, dec_values] = libsvmpredict(mnist_test_labels, mnist_test, model);

libsvm实现分类与回归(附参数详细说明、程序、数据集)_第3张图片

数据集以及libsvm3.23软件压缩包自行提取,失效联系我!!!!

链接:https://pan.baidu.com/s/1dKoWlOPrhRjTJyjYaNAV_w 
提取码:mu5j 
复制这段内容后打开百度网盘手机App,操作更方便哦

你可能感兴趣的:(支持向量机(svm))