MATLAB-从分类器生成混淆矩阵 [英] MATLAB - generate confusion matrix from classifier

查看:785
本文介绍了MATLAB-从分类器生成混淆矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些测试数据和标签:

I have some test data and labels:

testZ = [0.25, 0.29, 0.62, 0.27, 0.82, 1.18, 0.93, 0.54, 0.78, 0.31, 1.11, 1.08, 1.02];

testY = [1 1 1 1 1 2 2 2 2 2 2 2 2];

然后我对它们进行排序:

I then sort them:

[sZ, ind] = sort(testZ); %%Sorts Z, and gets indexes of Z
sY = testY(ind); %%Sorts Y by index
[N, n] = size(testZ');

然后将给出排序的Y数据.在排序的Y数据的每个元素上,我想将左侧的每个点归类为1,将右侧的所有点归类为2;然后将对数据的每个点重复此操作.我该怎么做,并为每个元素找出变量:

This will then give the sorted Y data. At each element of the sorted Y data, I want to classify each point to the left as being of type 1 and everything to the right being class 2; This will then be repeated for every point of the data. How can I do this and find out for each element the variables:

  • TP(真阳性)-正确标记为1的元素
  • FP(假阳性)-元素被错误地标记为1
  • TN(真阴性)-正确标记为2的元素
  • FN(假阴性)-错误地标记为2的元素

这样做的目的是使我可以在一些学校作业中为分类器创建ROC曲线.

The purpose of this is so that I can create an ROC curve for the classifier as part of some school work.

推荐答案

以下是绘制ROC和查找AUC值的代码:

Here is the code for plotting ROC and finding AUC value:

tot_op = testZ;
targets = testY;
th_vals= sort(tot_op);

for i = 1:length(th_vals)
  b_pred = (tot_op>=th_vals(i,1));
  TP = sum(b_pred == 1 & targets == 2);
  FP = sum(b_pred == 1 & targets == 1);
  TN = sum(b_pred == 0 & targets == 1);
  FN = sum(b_pred == 0 & targets == 2);
  sens(i) = TP/(TP+FN);
  spec(i) = TN/(TN+FP);
end


figure(2);
cspec = 1-spec;
cspec = cspec(end:-1:1);
sens = sens(end:-1:1);
plot(cspec,sens,'k');

AUC = sum(0.5*(sens(2:end)+sens(1:end-1)).*(cspec(2:end) - cspec(1:end-1)));
fprintf('\nAUC: %g \n',AUC);

上面的代码是在 http ://www.dcs.gla.ac.uk/~srogers/firstcourseml/matlab/chapter5/svmroc.html

这篇关于MATLAB-从分类器生成混淆矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆