在Matlab中使用KNN分类器进行交叉验证

时间:2016-01-16 12:15:51

标签: matlab machine-learning neural-network classification cross-validation

我正在尝试将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>

0 个答案:

没有答案