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

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