0
votes

I am trying to do a 5 fold cross validation with libsvm (matlab) using a precomputed kernel, but, I get the following error message : Undefined function 'ge' for input arguments of type 'struct'. this is because the Libsvm return a structure instead of a value in cross validation, How can I solve this problem, this is my code:

load('iris.dat')
data=iris(:,1:4);
class=iris(:,5);

% normalize the data
range=repmat((max(data)-min(data)),size(data,1),1);
data=(data-repmat(min(data),size(data,1),1))./range;

% train  
tr_data=[data(1:5,:);data(52:56,:);data(101:105,:)];
tr_lbl=[ones(5,1);2*ones(5,1);3*ones(5,1)];


% kernel computation
sigma=.8
rbfKernel = @(X,Y,sigma) exp((-pdist2(X,Y,'euclidean').^2)./(2*sigma^2));
Ktr=[(1:15)',rbfKernel(tr_data,tr_data,sigma)];
kts=[ (1:150)',rbfKernel(data,tr_data,sigma)];

% svmptrain
bestcv = 0;
for log2c = -1:3
    cmd = ['Ktr -t 4 -v 5 -c ', num2str(2^log2c)];  
    cv = svmtrain2(tr_lbl,tr_data, cmd);
    if (cv >= bestcv)
      bestcv = cv; 
      bestc = 2^log2c;
    end 
end


cmd=['-s 0 -c ', num2str(bestc), 'Ktr -t 4']
model=svmtrain2(tr_lbl,tr_data,cmd)

% svm predict
labels=svmpredict(class,data,model,kts)
2
first of all, you shouldn't define a variable named 'class'. Then, can you specify the line that producing the error? - NKN
I renamed the variable 'class' but this did not solve the problem, the error message in line 29 ( if (cv >= bestcv)). - Saied niazmardy

2 Answers

0
votes

The function svmtrain2 you are using is not part of standard MATLAB and also the output of the function is not a structure. But if you insist to use that, you can calculate an score for data using the other existing function:

[f,K] = svmeval(X_eval,varargin)

that evaluates the trained svm using the outputs from svmtrain2. But I prefer to use first the standard functions embedded in MATLAB. In standard MATLAB library there is:

SVMStruct = svmtrain(Training,Group) 

that returns a structure, SVMStruct, containing information about the trained support vector machine (SVM) classifier. or

SVMModel = fitcsvm(X,Y) 

that returns a support vector machine classifier SVMModel, trained by predictors X and class labels Y for one- or two-class classification. and then you can get some score for each prediction using:

[label,Score] = predict(SVMModel,X) 

that returns class likelihood measures, i.e., either scores or posterior probabilities.

0
votes

You get that error because you are trying to compare a struct and a number.

If what you want is to find the best performance in the training set (as it seems from you comparison), I don't think you can get it directly from the structure returned from svmtrain. You should first use svmpredict with the training set and the trained model, and you can get the accuracy from the resulting structure.