Features selection with sequentialfs with libsvm

ε祈祈猫儿з 提交于 2019-12-07 07:58:33

You'll need to wrap the libsvm functions to train and test an SVM on a particular featureset. I'd suggest writing things in a separate .m file (though in priciple I think it could go in an anonymous function). Something like:

function err = svmwrapper(xTrain, yTrain, xTest, yTest)
  model = svmtrain(yTrain, xTrain, <svm parameters>);
  err = sum(svmpredict(yTest, xTest, model) ~= yTest);
end

and then you can call sequentialfs with:

[fs history] = sequentialfs(@svmwrapper, ...);

(You may need to check the order of the arguments to svmtrain, I can never remember which way round they should be).

The idea is that svmwrapper will train an SVM and return its error on the test set.

The anonymous equivalent would be:

svmwrapper = @(xTrain, yTrain, xTest, yTest)sum(svmpredict(yTest, xTest, svmtrain(yTrain, xTrain, <svm parameters>) ~= yTest);

which doesn't look very nice.

Atesh

I don't know if this question is still open, but i got the function working using the following handle:

% The order of the inputs is important as svmpredict and svmtrain for libsvm take input as
% ytrain, xtrain but sequentialfs sends data as xtrain, ytrain, xtest, ytest

svfun = @(xtrain,ytrain,xtest,ytest)sum(svmpredict(ytest,xtest,svmtrain(ytrain,xtrain,<svm options>)) ~= ytest);

[fs history] = sequentialfs(svfun,x,y)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!