转自http://www.matlabsky.com/thread-12379-1-1.html
一 基本示例
代码(属性:身高,体重;标签:男 / 女)
data = [17670;180 80; 161 45; 163 47];
label = [1;1;-1;-1];
model = svmtrain(label,data);
test = [190 85];
testlabel = 1;
[predicted_label] = svmpredict(testlabel,test,model);
运行结果
optimizationfinished, #iter = 3
nu = 0.990884
obj = -1.981851, rho = -0.009074
nSV = 4, nBSV = 0
Total nSV = 4
Accuracy = 100% (1/1) (classification)
ps:accuracy = 100%,因为此处testlabel =1,而预测值也为1;若testlabel = -1,则accuracy = 0%
ps:[predicted_label,accuracy] =svmpredict(testlabel,test,model),MATLAB提示:
Usage:
[predicted_label,accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector,testing_instance_matrix, model, 'libsvm_options')
[predicted_label] = svmpredict(testing_label_vector, testing_instance_matrix,model, 'libsvm_options')
Parameters:
model: SVM model structure from svmtrain.
libsvm_options:
-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-classSVM not supported yet
-q : quiet mode (no outputs)
故预测函数亦可为:
[predicted_label,accuracy, decision_values]= svmpredict(testlabel,test,model, model);
[predicted_label,accuracy, prob_estimates] = svmpredict(testlabel,test,model, model);
二 验证数据集breast_cancer
Ps:切记把数据集所在文件夹设置为当前文件夹,否则无法打开数据文件
[label_vector, instance_matrix] = libsvmread('breast_cancer.txt');
%以前500个数据为训练集,其余数据为测试集
ind = 500;
trainlabel = label_vector(1:ind,:);
traindata = instance_matrix(1:ind,:);
testlabel = label_vector(ind+1:end,:);
testdata = instance_matrix(ind+1:end,:);
model = svmtrain(trainlabel,traindata);
%检测训练模型在训练集上的分类效果
[ptrain] = svmpredict(trainlabel,traindata,model);
%检测训练模型在测试集上的分类效果
[ptest] = svmpredict(testlabel,testdata,model);
训练结果
optimization finished, #iter = 94
nu = 0.114641
obj = -48.983625, rho = 0.438680
nSV = 71, nBSV = 53
Total nSV = 71
Accuracy = 96.6% (483/500) (classification)
Accuracy = 99.4536% (182/183) (classification)
Ps:svmtrain函数(model = svmtrain())
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 basisfunction: exp(-gamma*|u-v|^2)
3 -- sigmoid:tanh(gamma*u'*v + coef0)
4 -- precomputedkernel (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 forprobability 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)