English 中文(简体)
matlab中的支持向量机
原标题:support vector machines in matlab

你能举一个在matlab中使用支持向量机(SVM)对4类进行分类的例子吗

atribute_1  atribute_2 atribute_3 atribute_4 class
1           2          3           4             0
1           2          3           5             0
0           2          6           4             1
0           3          3           8             1
7           2          6           4             2
9           1          7           10            3
最佳回答

MATLAB目前不支持多类SVM。您可以使用svmtrain(2类)来实现这一点,但使用标准SVM包会容易得多。

我使用过libsvm并且可以确认它非常容易使用。


%%# Your data
D = [
1           2          3           4             0
1           2          3           5             0
0           2          6           4             1
0           3          3           8             1
7           2          6           4             2
9           1          7           10            3];
%%# For clarity
Attributes = D(:,1:4);
Classes = D(:,5);
train = [1 3 5 6];
test = [2 4];

%%# Train
model = svmtrain(Classes(train),Attributes(train,:), -s 0 -t 2 );

%%# Test
[predict_label, accuracy, prob_estimates] = svmpredict(Classes(test), Attributes(test,:), model);
问题回答

SVM最初是为二进制分类而设计的。然后,它们被扩展到处理多类问题。其思想是将问题分解为许多二元类问题,然后将它们组合起来以获得预测。

一种被称为一对一的方法,构建尽可能多的二元分类器,每个分类器都经过训练,将一个类与其他类分离。为了预测新实例,我们选择决策函数值最大的分类器。

另一种称为一对一的方法(我相信它在LibSVM中使用),构建k(k-1)/2二元分类器,训练以将每对类彼此分离,并使用多数投票方案(最大获胜策略)来确定输出预测。

还有其他方法,如使用纠错输出码(ECOC)来构建许多有点冗余的二进制分类器,并使用这种冗余来获得更稳健的分类(使用与汉明码相同的思想)。

示例(一对一):

%# load dataset
load fisheriris
[g gn] = grp2idx(species);                      %# nominal class to numeric

%# split training/testing sets
[trainIdx testIdx] = crossvalind( HoldOut , species, 1/3);

pairwise = nchoosek(1:length(gn),2);            %# 1-vs-1 pairwise models
svmModel = cell(size(pairwise,1),1);            %# store binary-classifers
predTest = zeros(sum(testIdx),numel(svmModel)); %# store binary predictions

%# classify using one-against-one approach, SVM with 3rd degree poly kernel
for k=1:numel(svmModel)
    %# get only training instances belonging to this pair
    idx = trainIdx & any( bsxfun(@eq, g, pairwise(k,:)) , 2 );

    %# train
    svmModel{k} = svmtrain(meas(idx,:), g(idx), ...
         BoxConstraint ,2e-1,  Kernel_Function , polynomial ,  Polyorder ,3);

    %# test
    predTest(:,k) = svmclassify(svmModel{k}, meas(testIdx,:));
end
pred = mode(predTest,2);   %# voting: clasify as the class receiving most votes

%# performance
cmat = confusionmat(g(testIdx),pred);
acc = 100*sum(diag(cmat))./sum(cmat(:));
fprintf( SVM (1-against-1):
accuracy = %.2f%%
 , acc);
fprintf( Confusion Matrix:
 ), disp(cmat)

以下是输出示例:

SVM (1-against-1):
accuracy = 93.75%
Confusion Matrix:
    16     0     0
     0    14     2
     0     1    15




相关问题
MATLAB Solving equations problem

I want to solve these equations using MATLAB and I am sure there is a non zero solution. The equations are: 0.7071*x + 0.7071*z = x -0.5*x + 0.7071*y + 0.5*z = y -0.5*x - 0.7071*y +...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

Divide two polynomials using MATLAB

I want to divide p(x) by q(x) given that: p(x)=-5x^4+3x^2-6x q(x)=x^2+1 I tried: p=inline( -5*(x^4)+3*(x^2) , x ) p = Inline function: p(x) = -5*(x^4)+3*(x^2) q=inline( x^2+1 , x ) q = ...

matlab deals with Fibbonacci

The Fibonacci series is given as follows: 1, 2, 3, 5, 8, 13, 21, ... How can I write a script which calculates and prints the n-th Fibonacci term (for n>2), where n is inputed by the user. This ...

How do I plot lines between all points in a vector?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...

热门标签