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