0

I tried to run this code found online, but it does not work. The error is

Error using svmclassify (line 53)
The first input should be a `struct` generated by `SVMTRAIN`.

Error in fisheriris_classification (line 27)
pred = svmclassify(svmModel, meas(testIdx,:), 'Showplot',false);

Can anyone help me fix this problem? Thank you so much!

clear all;
close all;
load fisheriris                              %# load iris dataset
groups = ismember(species,'setosa');         %# create a two-class problem

%# number of cross-validation folds:
%# If you have 50 samples, divide them into 10 groups of 5 samples each,
%# then train with 9 groups (45 samples) and test with 1 group (5 samples).
%# This is repeated ten times, with each group used exactly once as a test set.
%# Finally the 10 results from the folds are averaged to produce a single 
%# performance estimation.
k=10;

cvFolds = crossvalind('Kfold', groups, k);   %# get indices of 10-fold CV
cp = classperf(groups);                      %# init performance tracker

for i = 1:k                                  %# for each fold
    testIdx = (cvFolds == i);                %# get indices of test instances
    trainIdx = ~testIdx;                     %# get indices training instances

    %# train an SVM model over training instances
    svmModel = svmtrain(meas(trainIdx,:), groups(trainIdx), ...
                 'Autoscale',true, 'Showplot',false, 'Method','QP', ...
                 'BoxConstraint',2e-1, 'Kernel_Function','rbf', 'RBF_Sigma',1);

    %# test using test instances
    pred = svmclassify(svmModel, meas(testIdx,:), 'Showplot',false);

    %# evaluate and update performance object
    cp = classperf(cp, pred, testIdx);
end

%# get accuracy
cp.CorrectRate

%# get confusion matrix
%# columns:actual, rows:predicted, last-row: unclassified instances
cp.CountingMatrix
%with the output:

%ans =
%      0.99333
%ans =
%   100     1
%     0    49
%     0     0
Autonomous
  • 8,935
  • 1
  • 38
  • 77
user3761566
  • 87
  • 1
  • 8
  • if `svmModel` is not a `struct`, then what is it? Can you type `whos svmModel` in the command window and paste the output here? – Autonomous Aug 09 '14 at 23:37
  • >> whos svmModel Name Size Bytes Class Attributes svmModel 0x0 0 double This is the result – user3761566 Aug 09 '14 at 23:48
  • That means its an empty matrix and the command `svmtrain` is not working at all. Can you try `svmModel = svmtrain(meas(trainIdx,:), groups(trainIdx));` – Autonomous Aug 09 '14 at 23:49
  • Also, make sure that you calling MATLAB's `svmtrain` command. If you have `libsvm`, it will call libsvm's `svmtrain`, but in that case you get an error. So I am guessing you are calling MATLAB's command only. Can you debug the code and "step into" the command `svmtrain` – Autonomous Aug 09 '14 at 23:53
  • svmModel = svmtrain(meas(trainIdx,:), groups(trainIdx)); Error: label vector and instance matrix must be double – user3761566 Aug 09 '14 at 23:55
  • I download libsvm from this website:http://www.csie.ntu.edu.tw/~cjlin/libsvm/ – user3761566 Aug 09 '14 at 23:56
  • But when I put the package under Matlab fold and type"make" in the command window. It shows like this: >> make Building with 'Microsoft Visual C++ 2013 Professional (C)'. MEX completed successfully. Building with 'Microsoft Visual C++ 2013 Professional (C)'. MEX completed successfully. – user3761566 Aug 09 '14 at 23:58
  • Building with 'Microsoft Visual C++ 2013 Professional'. Could Not Find C:\D disk\MATLAB\R2014a\libsvm-3.18\matlab\svmtrain.exp Could Not Find C:\D disk\MATLAB\R2014a\libsvm-3.18\matlab\svmtrain.exp MEX completed successfully. Building with 'Microsoft Visual C++ 2013 Professional'. Could Not Find C:\D disk\MATLAB\R2014a\libsvm-3.18\matlab\svmpredict.exp Could Not Find C:\D disk\MATLAB\R2014a\libsvm-3.18\matlab\svmpredict.exp MEX completed successfully. – user3761566 Aug 09 '14 at 23:58
  • I do not know why it cannot find svmtrain.exp – user3761566 Aug 09 '14 at 23:59
  • So now are you trying with LIBSVM? Please post a different question for that. – Autonomous Aug 10 '14 at 00:00
  • @ Parag S. Chandakkar,can you also take a look at this question I posted?http://stackoverflow.com/questions/25219140/how-to-do-cross-validation-svm-classifier/25219514?noredirect=1#comment39282879_25219514 – user3761566 Aug 10 '14 at 00:01
  • I thought I download LIBSVM from this link:http://www.csie.ntu.edu.tw/~cjlin/libsvm/ – user3761566 Aug 10 '14 at 00:03
  • and I follow the README, type"make" in the command window. It says:Could Not Find C:\D disk\MATLAB\R2014a\libsvm-3.18\matlab\svmtrain.exp MEX completed successfully. – user3761566 Aug 10 '14 at 00:03
  • @ Parag S. Chandakkar,Please also take a look at this question I posted. http://stackoverflow.com/questions/25219140/how-to-do-cross-validation-svm-classifier/25219514?noredirect=1#comment39282879_25219514 – user3761566 Aug 10 '14 at 00:05
  • Read the `Readme` in `./libsvm3.1/matlab/` folder. Follow it carefully, you should be done. – Autonomous Aug 10 '14 at 00:06
  • yes, i read it and follow it, but not ok – user3761566 Aug 10 '14 at 00:07

1 Answers1

0

The reason for the issue seems to me the way MATLAB finds functions on the search path. I am fairly certain that it is still attempting to use the LIBSVM function rather than the built-in MATLAB function. Here is more information about the search path:

http://www.mathworks.com/help/matlab/matlab_env/what-is-the-matlab-search-path.html

To verify whether this is the issue, please try the following command in the command window:

>> which -all svmtrain

You should find that the built-in function is being shadowed by the LIBSVM function. You can either remove LIBSVM from the MATLAB search path using the "Set Path" tool in the Toolstrip, or run your code from a different directory that does not contain the LIBSVM files. I would recommend the first option. To read more about the built-in MATLAB functions, check these links:

http://www.mathworks.com/help/stats/svmtrain.html

http://www.mathworks.com/help/stats/svmclassify.html

If you would like to continue use LIBSVM, I would recommend checking the following site out.

https://www.csie.ntu.edu.tw/~cjlin/index.html Hope this helps.

Desta Haileselassie Hagos
  • 23,140
  • 7
  • 48
  • 53