2

I am trying to extend this answer to knn classifier:

load fisheriris;

% // convert species to double
isnum = cellfun(@isnumeric,species);
result = NaN(size(species));
result(isnum) = [species{isnum}];

% // Crossvalidation
vals = crossval(@(XTRAIN, YTRAIN, XTEST, YTEST)fun_knn(XTRAIN, YTRAIN, XTEST, YTEST), meas, result);

the fun_knn funcion is:

function testval = fun_knn(XTRAIN, YTRAIN, XTEST, YTEST)
    yknn = knnclassify(XTEST, XTRAIN, YTRAIN);      
    [~,classNet] = max(yknn,[],2);
    [~,classTest] = max(YTEST,[],2);
    [~,classTest] = find(YTEST);    
    cp = classperf(classTest, classNet);        
    testval = cp.CorrectRate;
end

I receive this error: Ground truth must have at least two classes.

Seems like the problem is that knnclassify produces empty result.I would like to use more modern funcitons like fitcknn, however I dont know how can I use training and task input for this function.

Sadegh
  • 865
  • 1
  • 23
  • 47
  • How does YTRAIN look like just before knnclassify is called? "Ground truth must have at least two classes." seems to indicate that something is wrong with it (e.g. wrong shape or only zeros). – Robin Spiess Jan 18 '16 at 15:22

0 Answers0