Creating a wrapper using support vector machines
Show older comments
Can anyone tell me how to implement a wrapper with Support vector machines.I've been trying to use the following code snippet for the purpose but it is always returning me one feature(which is the first one in case of forward selection and last one in case of sequential backward selection. Can anyone explain why this is happening or give some other example as a demo to explain the feature selection process by using SVM. I have tried a different database as well but encountered the same problem..Many thanks in advance!
%%FISHERIRIS DATA
load fisheriris
X = randn(150,20);
X(:,1:4)= meas(:,:);
y = species(1:150,:);
groups = ismember(species,'setosa');
y= groups(:,:)
X= scaleData(X); % to scale data in range [0,1]
%%CROSS VALIDATING
cc = cvpartition(y,'k',10);
%%SVM TRAINING AND TESTING FOR FEATURE %%SELECTION
opts = statset('display','iter');
OPTIONS=optimset('MaxIter',1000);
fun = @(Xtrain,Ytrain,Xtest,Ytest)...
(sum(~strcmp(Ytest,svmclassify(svmtrain(Xtrain,Ytrain),Xtest))))
[fs,history] = sequentialfs(fun,X,y,'cv',cc,'options',opts,'nfeatures',3)
%%END OF CODE
3 Comments
Barry Greene
on 19 Jan 2012
Your function scaleData is not shared however you don't need to do this step as the matlab SVMfunction will do this by default. This may be causing sequentialfs to stop at the first local minima (1st feature encountered). Also the maxiter value given is far too low compared to default value.
Rok Martincic
on 12 Nov 2012
I am having a similar problem, but with regression, not classification. The script started giving some meaningful results after I removed '~' from the part: 'fun = @(Xtrain,Ytrain,Xtest,Ytest)... (sum(~strcmp(Ytest,svmclassify(svmtrain(Xtrain,Ytrain),Xtest))))'. But since I don't exactly understand the meaning of '~', I'm not sure if these results are correct or just random. If you have some knowledge on the meaning of '~', please let me know.
Jan
on 12 Nov 2012
@Rok: Please posrt a new question in a new thread. Using the comment section of another question is not convenient for the ones, who wnat to answer.
Btw.: ~ is the not() operator, as the documentation reveals when you search for it.
Answers (1)
NIRANJAN KOTHA
on 1 Sep 2016
0 votes
strcmp compares strings not numbers. that might be the problem
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!