我正在尝试将this answer扩展为knn分类器:
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);
fun_knn
功能是:
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
我收到此错误:Ground truth must have at least two classes.
似乎问题是knnclassify
产生空结果。我想使用更像fitcknn
这样的现代功能,但我不知道如何使用此功能的训练和任务输入。 / p>