首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MATLAB:如何在k-NN分类后找到混淆矩阵?

MATLAB:如何在k-NN分类后找到混淆矩阵?
EN

Stack Overflow用户
提问于 2016-01-28 17:24:52
回答 1查看 1.6K关注 0票数 0

我正在MATLAB上做一个模式识别项目。我在做费舍尔虹膜数据集。我编写了一些用于k-NN分类的代码,在完成分类之后,我想要计算混淆矩阵。我所有的尝试都失败了。因此,我请求你帮助我找到一种计算混淆矩阵C的方法。

守则的有关部分如下:

代码语言:javascript
复制
fold_number = 5;
indices = crossvalind('Kfold',species, fold_number);

val = 1:2:5; % for these small k values there will not be an important difference
         % regarding the cp ErrorRates. The difference is going to be
         % observable for val = 1:2:100, for example!!! But the
         % exercise asks only for k = 1,3,5.

err_arr = [];

for k=val

    cp = classperf(species); % Reinitialize the cp-structure!

    for i = 1:fold_number
        test = (indices == i); 
        train = ~test;
        class = knnclassify(meas(test,:),meas(train,:),species(train), k);
        %class = knnclassify(meas(test,2),meas(train,2),species(train), k); % To experiment only with the 2nd feature

        classperf(cp,class,test);
    end
    err_arr = [err_arr; cp.ErrorRate];
    fprintf('The k-NN classification error rate for k = %d is: %f\n', k,cp.ErrorRate);
end  

fprintf('\n The error array is: \n');
disp(err_arr);

我期待着阅读你的答案!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-28 19:50:06

如果您可以访问统计信息和机器学习工具箱,您可以简单地使用混淆垫函数。如果将已知的分类(knownGroups)和known ( class在示例中给出的预测分类)作为输入,则返回混淆矩阵class

代码语言:javascript
复制
[C] = confusionmat(knownGroups,class)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35067994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档