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.