parameter optimization of support vector machine

hi i am new in matlab , i am using support vector machine for parameter optimizing purpose.I using the sample code which is given below-
grnpop = mvnrnd([1,0],eye(2),10);
>> redpop = mvnrnd([0,1],eye(2),10);
>> redpts = zeros(100,2);grnpts = redpts;
>> for i = 1:100
grnpts(i,:) = mvnrnd(grnpop(randi(10),:),eye(2)*0.2);
redpts(i,:) = mvnrnd(redpop(randi(10),:),eye(2)*0.2);
end;
>> k=10;
>> cdata = [grnpts;redpts];
grp = ones(200,1);
% green label 1, red label -1
grp(101:200) = -1;
>> cdata = [grnpts;redpts];
grp = ones(200,1);
% green label 1, red label -1
grp(101:200) = -1;
>> cvFolds = crossvalind('Kfold', grp, 10);
>> z=[3 5];
>> testIdx = (cvFolds == 1);
>> trainIdx = ~testIdx;
>> xtrain=cdata(trainIdx,:);
>> xtest=cdata(testIdx,:);
>> ytrain=grp(trainIdx);
>> svmStruct = svmtrain(xtrain,ytrain,'Kernel_Function','rbf',...
'rbf_sigma',z(1),'boxconstraint',z(2));
>> yfit = svmclassify(svmStruct,xtest);
>> minfn = @(z)crossval('mcr',cdata,grp,'Predfun',yfit,.....
@(xtrain,ytrain,xtest)crossfun(xtrain,ytrain,...
xtest,exp(z(1)),exp(z(2))));
>> opts = optimset('TolX',5e-4,'TolFun',5e-4);
>> [searchmin fval] = fminsearch(minfn,randn(2,1),opts)
but i found the following error-
Error using crossval (line 204)
Wrong number of arguments.
Error in
@(z)crossval('mcr',cdata,grp,'Predfun',yfit,@(xtrain,ytrain,xtest)crossfun(xtrain,ytrain,xtest,exp(z(1)),exp(z(2))))
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
please any one tell me why this error is comeing ,and also suggest the code.

 Accepted Answer

You seem to be looking at this example from the documentation. Copy the example carefully, I guarantee that it runs as shown.
Alan Weiss
MATLAB mathematical toolbox documentation

5 Comments

thank you Alan for reply.I follow your suggestion but step(7),create me problem . It is difficult to understand me.please briefly explain it and if it is possible then tell me this step taking a example. I waiting for your suggestion.Thank you.
If I understand you, this is step 7:
7.Write a function called crossfun to calculate the predicted classification yfit from a test vector xtest, when the SVM is trained on a sample xtrain that has classification ytrain. Since you want to find the best parameters rbf_sigma and boxconstraint, include those in the function.
function yfit = ...
crossfun(xtrain,ytrain,xtest,rbf_sigma,boxconstraint)
% Train the model on xtrain, ytrain,
% and get predictions of class of xtest
svmStruct = svmtrain(xtrain,ytrain,'Kernel_Function','rbf',...
'rbf_sigma',rbf_sigma,'boxconstraint',boxconstraint);
yfit = svmclassify(svmStruct,xtest);
If this is what is confusing you: copy the code into a text file named crossfun.m and make sure that this function file is on your MATLAB path.
If you are confused about something else, then ask again.
Alan Weiss
MATLAB mathematical toolbox documentation
many many thanks for your response sir.This is works very nicely,but i have a another problem.suppose we have train data and corresponding data lable and test data. Then how can i calculate the classification accuracy.Please give me a sample code sir.
The 'mcr' argument in the crossval command gives the misclassification rate. So the objective function you were minimizing is the misclassification rate. That is the reported fval.
If you want to write a function to examine a misclassification rate, take your data, run your classifier, and see how many data points it classified correctly.
Alan Weiss
MATLAB mathematical toolbox documentation
Alan sir i have a problem to implement the common spatial pattern for channel selection in multi class domain.So,Please tell me how it can be implemented.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!