matlab是工程计算的神器。最近需要做一个svm的小程序,公共的svm库时libsvm,但是在新的matlab版本中也添加了svm工具箱。简要示例如下:
clc;
clear;
close all;
traindata = [0 1; -1 0; 2 2; 3 3; -2 -1;-4.5 -4; 2 -1; -1 -3];
group = [1 1 -1 -1 1 1 -1 -1]';
testdata = [5 2;3 1;-4 -3];
svm_struct = svmtrain(traindata,group,'Showplot',true); % training
Group = svmclassify(svm_struct,testdata,'Showplot',true);
hold on;
plot(testdata(:,1),testdata(:,2),'ro','MarkerSize',12); % testing
hold off
程序运行结果如下: