您的位置:首页 > 编程语言 > MATLAB

matlab 中 svm的使用

2015-02-27 15:22 447 查看
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


程序运行结果如下:



上述主要用到了svmtrain 和 svmclassify两个函数,一个是训练,一个是分类。matlab doc介绍:

svmtrainTrain support vector machine classifier

SyntaxSVMStruct = svmtrain(Training,Group)SVMStruct = svmtrain(Training,Group,Name,Value)DescriptionSVMStruct = svmtrain(Training,Group) returns

a structure, SVMStruct, containing information

about the trained support vector machine (SVM) classifier.SVMStruct = svmtrain(Training,Group,Name,Value) returns

a structure with additional options specified by one or more Name,Value pair

arguments.Input ArgumentsTraining

Matrix of training data, where each row corresponds to an observation

or replicate, and each column corresponds to a feature or variable. svmtrain treats NaNs

or empty strings in Training as missing values

and ignores the corresponding rows of Group.

Group

Grouping variable, which can be a categorical, numeric, or logical

vector, a cell vector of strings, or a character matrix with each

row representing a class label. Each element of Group specifies

the group of the corresponding row of Training. Group should

divide Training into two groups. Group has

the same number of elements as there are rows in Training. svmtrain treats

each NaN, empty string, or 'undefined' in Group as

a missing value, and ignores the corresponding row of Training.

svmclassifyClassify using support vector machine (SVM)

SyntaxGroup = svmclassify(SVMStruct,Sample)Group = svmclassify(SVMStruct,Sample,'Showplot',true)DescriptionGroup = svmclassify(SVMStruct,Sample) classifies

each row of the data in Sample, a matrix of data,

using the information in a support vector machine classifier structure SVMStruct,

created using the svmtrain function.

Like the training data used to create SVMStruct, Sample is

a matrix where each row corresponds to an observation or replicate,

and each column corresponds to a feature or variable. Therefore, Sample must

have the same number of columns as the training data. This is because

the number of columns defines the number of features. Group indicates

the group to which each row of Sample has been

assigned.Group = svmclassify(SVMStruct,Sample,'Showplot',true) plots

the Sample data in the figure created using

the Showplot property with the svmtrain function.

This plot appears only when the data is two-dimensional. Input ArgumentsSVMStruct

Support vector machine classifier structure created using the svmtrain function.

Sample

A matrix where each row corresponds to an observation or replicate,

and each column corresponds to a feature or variable. Therefore, Sample must

have the same number of columns as the training data. This is because

the number of columns defines the dimensionality of the data space.

Showplot

Describes whether to display a plot of the classification. Displays

only for 2-D problems. Follow with a Boolean argument: true to

display the plot, false to give no display.

Output ArgumentsGroup

Column vector with the same number of rows as Sample.

Each entry (row) in Group represents the class

of the corresponding row of Sample.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: