Daniel@0: function [C,rate]=confmat(Y,T) Daniel@0: %CONFMAT Compute a confusion matrix. Daniel@0: % Daniel@0: % Description Daniel@0: % [C, RATE] = CONFMAT(Y, T) computes the confusion matrix C and Daniel@0: % classification performance RATE for the predictions mat{y} compared Daniel@0: % with the targets T. The data is assumed to be in a 1-of-N encoding, Daniel@0: % unless there is just one column, when it is assumed to be a 2 class Daniel@0: % problem with a 0-1 encoding. Each row of Y and T corresponds to a Daniel@0: % single example. Daniel@0: % Daniel@0: % In the confusion matrix, the rows represent the true classes and the Daniel@0: % columns the predicted classes. The vector RATE has two entries: the Daniel@0: % percentage of correct classifications and the total number of correct Daniel@0: % classifications. Daniel@0: % Daniel@0: % See also Daniel@0: % CONFFIG, DEMTRAIN Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: [n c]=size(Y); Daniel@0: [n2 c2]=size(T); Daniel@0: Daniel@0: if n~=n2 | c~=c2 Daniel@0: error('Outputs and targets are different sizes') Daniel@0: end Daniel@0: Daniel@0: if c > 1 Daniel@0: % Find the winning class assuming 1-of-N encoding Daniel@0: [maximum Yclass] = max(Y', [], 1); Daniel@0: Daniel@0: TL=[1:c]*T'; Daniel@0: else Daniel@0: % Assume two classes with 0-1 encoding Daniel@0: c = 2; Daniel@0: class2 = find(T > 0.5); Daniel@0: TL = ones(n, 1); Daniel@0: TL(class2) = 2; Daniel@0: class2 = find(Y > 0.5); Daniel@0: Yclass = ones(n, 1); Daniel@0: Yclass(class2) = 2; Daniel@0: end Daniel@0: Daniel@0: % Compute Daniel@0: correct = (Yclass==TL); Daniel@0: total=sum(sum(correct)); Daniel@0: rate=[total*100/n total]; Daniel@0: Daniel@0: C=zeros(c,c); Daniel@0: for i=1:c Daniel@0: for j=1:c Daniel@0: C(i,j) = sum((Yclass==j).*(TL==i)); Daniel@0: end Daniel@0: end