⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 confmat.m

📁 模式识别的主要工具集合
💻 M
字号:
%CONFMAT Construct confusion matrix% %  [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%  LABLIST     Unique labels in LAB1 and LAB2%% DESCRIPTION% Constructs a confusion matrix C between two sets of labels LAB1 % (corresponding to the rows in C) and LAB2 (the columns in C). The order of % the rows and columns is returned in LABLIST. NE is the total number of % errors (sum of non-diagonal elements in C).%% When METHOD = 'count' (default), co-occurences in LAB1 and LAB2 are counted % and returned in C. When METHOD = 'disagreement', the relative disagreement % is returned in NE, and is split over all combinations of labels in C% (such that the rows sum to 1).%%   [C,NE,LABLIST] = CONFMAT(D,METHOD)%% If D is a classification result D = A*W, the labels LAB1 and LAB2 are % internally retrieved by CONFMAT before computing the confusion matrix.%% When no output argument is specified, or when FID is given, the% confusion matrix is displayed or written a to a text file.%% EXAMPLE% Typical use of CONFMAT is the comparison of true and and estimated labels% of a testset A by application to a trained classifier W: % LAB1 = GETLABELS(A); LAB2 = A*W*LABELD.% More examples can be found in PREX_CONFMAT, PREX_MATCHLAB.% % SEE ALSO% MAPPINGS, DATASETS, GETLABELS, LABELD% Copyright: R.P.W. Duin, r.p.w.duin@prtools.org% Faculty EWI, Delft University of Technology% P.O. Box 5031, 2600 GA Delft, The Netherlands% $Id: confmat.m,v 1.7 2004/09/26 13:10:26 duin Exp $function [C,ne,lablist] = confmat (arg1,arg2,arg3,fid)	prtrace(mfilename);	% Check arguments.  if nargin < 4, fid = 1; end	if nargin < 3 | isempty(arg3)		if isdataset(arg1)			lab1 = getlabels(arg1); lab2 = arg1*labeld;			if nargin < 2| isempty(arg2)				method = 'count';				prwarning(4,'no method supplied, assuming count');			else				method = arg2;			end		else			method = 'count';			prwarning(4,'no method supplied, assuming count');			lab1 = arg1;			if (nargin < 2 | isempty(arg2))				error('Second label list not supplied')			end			lab2 = arg2;		end	else		lab1 = arg1;		lab2 = arg2;		method = arg3;	end	if nargin < 2		if ~isdataset(arg1)			error('two labellists or one dataset should be supplied')		end	end			% Renumber LAB1 and LAB2 and find number of unique labels.	m = size(lab1,1); [nlab1,nlab2,lablist] = renumlab(lab1,lab2);	n = max([nlab1;nlab2]); 	% Construct matrix of co-occurences (confusion matrix).	C = zeros(n,n);	for i = 1:n		K = find(nlab1==i);		if (isempty(K))			C(i,:) = zeros(1,n);		else			for j = 1:n				C(i,j) = length(find(nlab2(K)==j));			end		end	end	% Calculate number of errors ('count') or disagreement ('disagreement').	switch (method)		case 'count'											ne = sum(sum(C)) - sum(diag(C));       % Diagonal entries are correctly                                                % classified, so all off-diagonal                                                % entries denote wrong ones.		case 'disagreement'			ne = (sum(sum(C)) - sum(diag(C)))/m;   % Relative sum of off-diagonal                                                 % entries.			D = repmat(sum(C,2),1,n);               % Disagreement = 1 - 			C = ones(n,n)-C./D;                    % relative co-occurence.			C = C / (n-1);		otherwise			error('unknown method');	end    % If no output argument is specified, pretty-print C.	if (nargout == 0) | nargin == 4				if nargin < 4, fid = 1; end		% Make sure labels are stored in LABC as matrix of characters,       % max. 6 per label.		labc = strlab(lablist);		if (size(labc,2) > 6), labc = labc(:,1:6); end		if (size(labc,2) < 5), labc = [labc repmat(' ',n,ceil((5-size(labc,2))/2))]; end		fprintf(fid,'\n         | Estimated Labels\n');		fprintf(fid,'   True  |');		fprintf(fid,'\n  Labels |');		for j = 1:n, fprintf(fid,'%7s',labc(j,:)); end		fprintf(fid,'|');		fprintf(fid,' Totals');		fprintf(fid,'\n  ');		fprintf(fid,'-------|%s',repmat('-',1,7*n));		fprintf(fid,'|-------');		fprintf(fid,'\n');			for j = 1:n			fprintf(fid,'  %-7s|',labc(j,:));			switch (method)				case 'count'					fprintf(fid,'%5i  ',C(j,:)');					fprintf(fid,'|');					fprintf(fid,'%5i',sum(C(j,:)));				case 'disagreement'					fprintf(fid,' %5.3f ',C(j,:)');					fprintf(fid,'|');					fprintf(fid,' %5.3f ',sum(C(j,:)));			end			fprintf(fid,'\n');		end		fprintf(fid,'  -------|%s',repmat('-',1,7*n));		fprintf(fid,'|-------');		fprintf(fid,'\n  Totals |');		switch (method)			case 'count'				fprintf(fid,'%5i  ',sum(C));				fprintf(fid,'|');				fprintf(fid,'%5i',sum(C(:)));			case 'disagreement'				fprintf(fid,' %5.3f ',sum(C));				fprintf(fid,'|');				fprintf(fid,' %5.3f ',sum(C(:)));		end		fprintf(fid,'\n\n');	endreturn

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -