【问题标题】:MATLAB: How to find the confusion matrix after k-NN classification?MATLAB:如何找到 k-NN 分类后的混淆矩阵?
【发布时间】:2016-05-06 05:06:17
【问题描述】:

我正在 MATLAB 上进行模式识别项目。我正在研究费希尔鸢尾花数据集。我已经编写了一些适用于 k-NN 分类的代码,完成分类后我想计算混淆矩阵。我所有的尝试都失败了。因此,我请求您帮助找到计算混淆矩阵 C 的方法。

相关部分代码如下:

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);

我期待着阅读您的答案!

【问题讨论】:

    标签: matlab classification knn confusion-matrix


    【解决方案1】:

    如果您有权访问统计信息和机器学习工具箱,则只需使用confusionmat function。如果您将已知分类 (knownGroups) 和 k-nn 输出(在您的示例中由 class 给出的预测分类)作为输入,它将返回混淆矩阵 C

    [C] = confusionmat(knownGroups,class)
    

    【讨论】:

    • 写knownGroups是指物种矩阵吗?因为当我放置confusionmat(species,class) 时出现此错误:“G 和GHAT 需要具有相同的行数”。 Species 有 150 行,class 有 30 行。
    • 好吧,我想我解决了。我改变的是: knowngroup = species(randperm(n)); [C] = 混淆垫(已知组(1:30),类)。对吗?
    • 据我所见,我认为结果不正确..C矩阵似乎是错误的。
    • 我看到的方式是您正在尝试对测试数据的物种进行分类,并且由于您正在构建模型,因此您应该已经拥有正确的物种(因此它是您的 knownGroups)。但是,根据您的火车数据,您可以使用knnclassify 进行估算,这是由班级给出的。从理论上讲,[C]=confusionmat(species(test),class)应该可以工作。此外,说出究竟是什么问题总是令人鼓舞
    猜你喜欢
    • 2012-01-20
    • 2016-02-05
    • 2015-04-29
    • 2020-08-15
    • 2017-01-13
    • 2015-07-31
    • 2020-08-21
    • 2019-05-26
    • 2017-03-20
    相关资源
    最近更新 更多