annotate toolboxes/FullBNT-1.0.7/netlab3.3/confmat.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [C,rate]=confmat(Y,T)
wolffd@0 2 %CONFMAT Compute a confusion matrix.
wolffd@0 3 %
wolffd@0 4 % Description
wolffd@0 5 % [C, RATE] = CONFMAT(Y, T) computes the confusion matrix C and
wolffd@0 6 % classification performance RATE for the predictions mat{y} compared
wolffd@0 7 % with the targets T. The data is assumed to be in a 1-of-N encoding,
wolffd@0 8 % unless there is just one column, when it is assumed to be a 2 class
wolffd@0 9 % problem with a 0-1 encoding. Each row of Y and T corresponds to a
wolffd@0 10 % single example.
wolffd@0 11 %
wolffd@0 12 % In the confusion matrix, the rows represent the true classes and the
wolffd@0 13 % columns the predicted classes. The vector RATE has two entries: the
wolffd@0 14 % percentage of correct classifications and the total number of correct
wolffd@0 15 % classifications.
wolffd@0 16 %
wolffd@0 17 % See also
wolffd@0 18 % CONFFIG, DEMTRAIN
wolffd@0 19 %
wolffd@0 20
wolffd@0 21 % Copyright (c) Ian T Nabney (1996-2001)
wolffd@0 22
wolffd@0 23 [n c]=size(Y);
wolffd@0 24 [n2 c2]=size(T);
wolffd@0 25
wolffd@0 26 if n~=n2 | c~=c2
wolffd@0 27 error('Outputs and targets are different sizes')
wolffd@0 28 end
wolffd@0 29
wolffd@0 30 if c > 1
wolffd@0 31 % Find the winning class assuming 1-of-N encoding
wolffd@0 32 [maximum Yclass] = max(Y', [], 1);
wolffd@0 33
wolffd@0 34 TL=[1:c]*T';
wolffd@0 35 else
wolffd@0 36 % Assume two classes with 0-1 encoding
wolffd@0 37 c = 2;
wolffd@0 38 class2 = find(T > 0.5);
wolffd@0 39 TL = ones(n, 1);
wolffd@0 40 TL(class2) = 2;
wolffd@0 41 class2 = find(Y > 0.5);
wolffd@0 42 Yclass = ones(n, 1);
wolffd@0 43 Yclass(class2) = 2;
wolffd@0 44 end
wolffd@0 45
wolffd@0 46 % Compute
wolffd@0 47 correct = (Yclass==TL);
wolffd@0 48 total=sum(sum(correct));
wolffd@0 49 rate=[total*100/n total];
wolffd@0 50
wolffd@0 51 C=zeros(c,c);
wolffd@0 52 for i=1:c
wolffd@0 53 for j=1:c
wolffd@0 54 C(i,j) = sum((Yclass==j).*(TL==i));
wolffd@0 55 end
wolffd@0 56 end