声明:本文用到的代码均来自于PRTools(http://www.prtools.org)模式识别工具箱,并以matlab软件进行实验。

混淆矩阵是模式识别中的常用工具,在PRTools工具箱中有直接的函数confmat可供引用。具体使用方法如下所示:

  [C,NE,LABLIST] = CONFMAT(LAB1,LAB2,METHOD,FID)

  INPUT
LAB1 Set of labels
LAB2 Set of labels
METHOD 'count' (default) to count number of co-occurences in
LAB1 and LAB2, 'disagreement' to count relative
non-co-occurrence.
FID Write text result to file OUTPUT
C Confusion matrix
NE Total number of errors (empty labels are neglected)
LABLIST Unique labels in LAB1 and LAB2

首先简单理解一些词语:

TP(True Positive):被分类器正确分类的正元组。

TN(True Negative):被分类器正确分类的负元组。

FP(False Positive):被错误标记为正元组的负元组。

FN(False Negative):被错误标记为负元组的正元组。

TP与TN告诉我们分类器何时分类正确,FP与FN告诉我们分类器何时分类错误。

对一个M类的数据集, 混淆矩阵(Confusion Matrix)是一个至少M×M的表,它的第i行第j列的数值表示为第i类的元组被标记为第j类的个数。

一个例子,以UCI数据集中的Ionosphere数据集为例,调用PRtools工具箱中的混淆矩阵函数:

(1)首先初始化ionosphere数据集合:

data=load('ionosphere.txt');
[m,k]=size(data);
data1=ones(m,k-);
for i=:k-
data1(:,i)=(data(:,i)-min(data(:,i)))/(max(data(:,i))-min(data(:,i)));
end
label=data(:,k);
[Y,I]=min(label);
if Y()==
for i=:m
label(i)=label(i)+;
end
end
a=dataset(data1,label);

(2)然后调用confmat.m函数:

[train,test]=gendat(a,0.5);
w=treec(train);
conf=confmat(test*w)

运行结果:

conf就是混淆矩阵,其矩阵数值含义对应上述表格。

如果不想用PRtools工具箱中的混淆矩阵函数,可以直接自行编写混淆矩阵代码,如下所示,运行时可直接调用。

function [confmatrix] = cfmatrix(actual, predict, classlist, per)
% CFMATRIX calculates the confusion matrix for any prediction
% algorithm that generates a list of classes to which the test
% feature vectors are assigned
%
% Outputs: confusion matrix
%
% Actual Classes
% p n
% ___|_____|______|
% Predicted p'| | |
% Classes n'| | |
%
% Inputs:
% . actual / . predict
% The inputs provided are the 'actual' classes vector
% and the 'predict'ed classes vector. The actual classes are the classes
% to which the input feature vectors belong. The predicted classes are the
% class to which the input feature vectors are predicted to belong to,
% based on a prediction algorithm.
% The length of actual class vector and the predicted class vector need to
% be the same. If they are not the same, an error message is displayed.
% . classlist
% The third input provides the list of all the classes {p,n,...} for which
% the classification is being done. All classes are numbers.
% . per = / (default = )
% This parameter when set to provides the values in the confusion matrix
% as percentages. The default provides the values in numbers.
%
% Example:
% >> a = [ ];
% >> b = [ ];
% >> Cf = cfmatrix(a, b);
%
% [Avinash Uppuluri: avinash_uv@yahoo.com: Last modified: //] % If classlist not entered: make classlist equal to all
% unique elements of actual
if (nargin < )
error('Not enough input arguments.');
elseif (nargin == )
classlist = unique(actual); % default values from actual
per = ; % default is numbers and input for percentage
elseif (nargin == )
per = ; % default is numbers and input for percentage
end if (length(actual) ~= length(predict))
error('First two inputs need to be vectors with equal size.');
elseif ((size(actual,) ~= ) && (size(actual,) ~= ))
error('First input needs to be a vector and not a matrix');
elseif ((size(predict,) ~= ) && (size(predict,) ~= ))
error('Second input needs to be a vector and not a matrix');
end
format short g;
n_class = length(classlist);
line_two = '----------';
line_three = '_________|';
for i = :n_class
obind_class_i = find(actual == classlist(i));
prind_class_i = find(predict == classlist(i));
confmatrix(i,i) = length(intersect(obind_class_i,prind_class_i));
for j = :n_class
%if (j ~= i)
if (j < i)
% observed j predicted i
confmatrix(i,j) = length(find(actual(prind_class_i) == classlist(j)));
% observed i predicted j
confmatrix(j,i) = length(find(predict(obind_class_i) == classlist(j)));
end
end
line_two = strcat(line_two,'---',num2str(classlist(i)),'-----');
line_three = strcat(line_three,'__________');
end if (per == )
confmatrix = (confmatrix ./ length(actual)).*;
end % output to screen
disp('------------------------------------------');
disp(' Actual Classes');
disp(line_two);
disp('Predicted| ');
disp(' Classes| ');
disp(line_three); for i = :n_class
temps = sprintf(' %d ',i);
for j = :n_class
temps = strcat(temps,sprintf(' | %2.1f ',confmatrix(i,j)));
end
disp(temps);
clear temps
end
disp('------------------------------------------');

混淆矩阵的概念其实很好理解,接下来引申几个很好理解的术语的概念(P:正元组数目,N:负元组数目):
      准确率:TP+TN/P+N

错误率:FP+FN/P+N

敏感度、召回率:TP/P

精度:TP/TP+FP

本文主要是从PRtools工具箱中混淆矩阵函数的使用来简单介绍了解混淆矩阵的概念,如有不正确的地方,欢迎指正。

混淆矩阵在Matlab中PRtools模式识别工具箱的应用的更多相关文章

  1. matlab中使用fuzzy工具箱

    4步教你学会使用matlab模糊控制工具箱 Matlab模糊控制工具箱为模糊控制器的设计提供了一种非常便捷的途径,通过它我们不需要进行复杂的模糊化.模糊推理及反模糊化运算,只需要设定相应参数,就可以很 ...

  2. 混淆矩阵-MATLAB代码详解

    一.混淆矩阵 (一).简介 在人工智能中,混淆矩阵(confusion matrix)是可视化工具,特别用于监督学习,在无监督学习一般叫做匹配矩阵.在图像精度评价中,主要用于比较分类结果和实际测得值, ...

  3. matlab中矩阵的表示与简单操作

    原文地址为:matlab矩阵的表示和简单操作 一.矩阵的表示在MATLAB中创建矩阵有以下规则: a.矩阵元素必须在”[ ]”内: b.矩阵的同行元素之间用空格(或”,”)隔开: c.矩阵的行与行之间 ...

  4. matlab中reshape 重构数组

    来源:https://ww2.mathworks.cn/help/matlab/ref/reshape.html?searchHighlight=reshape&s_tid=doc_srcht ...

  5. matlab中元胞数组(cell)转换为矩阵

    matlab中元胞数组(cell)转换为矩阵. cell转换为矩阵函数为:cell2mat(c),其中c为待转换的元胞数组: 转化之后的矩阵可能不满足我们对矩阵维数的要求,那么也许还需要下面两个函数: ...

  6. MATLAB中求矩阵非零元的坐标

    MATLAB中求矩阵非零元的坐标: 方法1: index=find(a); [i,j]=ind2sub(size(a),index); disp([i,j]) 方法2: [i,j]=find(a> ...

  7. Matlab中矩阵的平方和矩阵中每个元素的平方介绍

    该文章讲述了Matlab中矩阵的平方和矩阵中每个元素的平方介绍.   设t = [2 4 2 4] 则>> t.^2 ans = 4 164 16 而>> t^2 ans = ...

  8. 利用sklearn对MNIST手写数据集开始一个简单的二分类判别器项目(在这个过程中学习关于模型性能的评价指标,如accuracy,precision,recall,混淆矩阵)

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  9. MATLAB 中 ksvdbox和ompbox 工具箱的安装和使用

    下载工具箱 链接: http://www.cs.technion.ac.il/~ronrubin/software.html 下载好工具箱之后, 要将解压后的文件夹添加到MATLAB的安装目录下的to ...

随机推荐

  1. Java中过滤器和拦截器的区别

    1.拦截器是基于java反射机制的,而过滤器是基于函数回调的. 2.过滤器依赖于servlet容器,而拦截器不依赖于servlet容器. 3.拦截器只对action起作用,而过滤器几乎可以对所有请求起 ...

  2. Nginx/LVS/HAProxy负载均衡软件的优缺点

    一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术.具体的应用需求还得具体分析,如果是中小型的Web应用,比如日PV小于1000万,用Nginx就完全可以了:如果机器不少,可以用D ...

  3. 原生js(三)

    客户端js的时间线: 1.web浏览器创建Document对象,开始解析html和文本.生成Element对象和Text节点添加到文档中.这个阶段的document.readystate==" ...

  4. win7 查看当前java路径

    C:\Users\zh>where javaC:\Windows\System32\java.exeD:\TOOL\jdk1.8.0_91\bin\java.exeD:\TOOL\jdk1.8. ...

  5. MAC升级node及npm

    清除node.js的cache: sudo npm cache clean -f 安装 n 工具,这个工具是专门用来管理node.js版本的,别怀疑这个工具的名字,是他是他就是他,他的名字就是 &qu ...

  6. (转载)解决AndroidStudio导入项目在 Building gradle project info 一直卡住

    源地址http://blog.csdn.net/yyh352091626/article/details/51490976 Android Studio导入项目的时候,一直卡在Building gra ...

  7. minix中二分查找bsearch的实现

    在看minix中bsearch实现的源代码之前,先学习一下C 语言中void类型以及void*类型的使用方法与技巧. void的含义: void的字面意思是“无类型”,void *则为“无类型指针”, ...

  8. jQuery相同id元素 全部获取问题解决办法

    问题:今天在做页面链接的点击效果时,让部分a链接跳转到同一个地址,即使使用$().each()也同样无法获取所有相同id的值. 用以下方法只有第一个a链接点击可以正常跳转 例如: html代码: &l ...

  9. windows本地启动tomcat闪退

    da开cmd, 进入tomcat所在目录的bin目录: 执行startup.bat 查看设置的环境变量是否正确:如果不正确则在windows中设置正确的相关环境变量即可:

  10. css如何设置label的字间距

    css.html如何设置label的字间距 .myClass  label{ letter-spacing: 10px; } 如果label需要居中,需加上 text-indent: 10px; 首行 ...