matlab 中 svm的使用
2015-02-27 15:22
447 查看
matlab是工程计算的神器。最近需要做一个svm的小程序,公共的svm库时libsvm,但是在新的matlab版本中也添加了svm工具箱。简要示例如下:
程序运行结果如下:
上述主要用到了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.
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.
相关文章推荐
- SVM学习笔记(1)LIBSVM在matlab下的使用安装
- matlab中SVM工具箱的使用方法
- LibLinear(SVM包)使用说明之(二)MATLAB接口
- matlab中的分类器使用小结(SVM、KNN、RF、AdaBoost、Naive Bayes、DAC)
- Matlab-SVM工具箱初使用-分类小例子(学生性别判断)
- matlab中使用SVM
- LIBSVM在MATLAB中的使用及SVM最优参数选取示例代码
- matlab 使用SVM 对鸢尾鼠植物2分类
- LibLinear(SVM包)使用说明之(二)MATLAB接口
- LibLinear(SVM包)使用说明之(二)MATLAB接口
- matlab SVM工具箱安装和使用
- LibLinear(SVM包)使用说明之(二)MATLAB接口
- 安装了Libsvm后还想使用Matlab自带的SVM?
- svm在matlab中的使用过程
- matlab中SVM工具箱的使用方法
- svm理论与实验之22: 自定义核函数在Matlab平台上的使用
- MATLAB SVM Toolbox 使用
- LibLinear(SVM包)使用说明之(二)MATLAB接口
- Matlab中解决出现的错误使用 svmtrain (line 234) Y must be a vector or a character array.问题
- LibLinear(SVM包)使用说明之(二)MATLAB接口