% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % extended_cm = compute_extended_confusion_matrix(predicted_class, % % real_class, n_classes) % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % FUNCTIONALITY: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This function aim is to compute the extended confusion matrix for a %%%% % vector of predicted classes (predicted_class) by comparing them to the % % corresponding real class (real_class). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % INPUT ARGUMENTS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % predicted_class: Vector containing the predicted class for a set of %%%% % observations. This variable must a vector of integer %% % values to facilitate the confusion matrix %%%%%%%%%%%%% % computation. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % real_class: Vector containing the real class associated to the %%%% % same observations that were predicted and passed as %%% % an input argument (predicted_class). %%%%%%%%%%%%%%%%%% % n_classes: Integer value that reprsents the number of different %% % classes present in the data set. Classes must be %%%%%% % represented by integer values in range of 1 to %%%%%%%% % n_classes. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % OUTPUT ARGUMENTS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % extende_cm: Matrix containing the extended matrix for the set of %% % observations passed as an input argument. Its %%%%%%%%% % dimensions are n_classes times n_classes. %%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% First of all, we initialise the varibale that will contain the extended confusion matrix.
0001 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0002 %% extended_cm = compute_extended_confusion_matrix(predicted_class, % 0003 %% real_class, n_classes) % 0004 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 %% FUNCTIONALITY: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0006 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0007 %% This function aim is to compute the extended confusion matrix for a %%%% 0008 %% vector of predicted classes (predicted_class) by comparing them to the % 0009 %% corresponding real class (real_class). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0010 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0011 %% INPUT ARGUMENTS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0012 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0013 %% predicted_class: Vector containing the predicted class for a set of %%%% 0014 %% observations. This variable must a vector of integer %% 0015 %% values to facilitate the confusion matrix %%%%%%%%%%%%% 0016 %% computation. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0017 %% real_class: Vector containing the real class associated to the %%%% 0018 %% same observations that were predicted and passed as %%% 0019 %% an input argument (predicted_class). %%%%%%%%%%%%%%%%%% 0020 %% n_classes: Integer value that reprsents the number of different %% 0021 %% classes present in the data set. Classes must be %%%%%% 0022 %% represented by integer values in range of 1 to %%%%%%%% 0023 %% n_classes. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0024 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0025 %% OUTPUT ARGUMENTS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0026 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0027 %% extende_cm: Matrix containing the extended matrix for the set of %% 0028 %% observations passed as an input argument. Its %%%%%%%%% 0029 %% dimensions are n_classes times n_classes. %%%%%%%%%%%%% 0030 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0031 function extended_cm = compute_extended_confusion_matrix(... 0032 predicted_class, real_class, n_classes) 0033 % First of all, we initialise the varibale that will contain the extended 0034 % confusion matrix. 0035 extended_cm = zeros(n_classes); 0036 % Now, for each different class... 0037 for i = 1:n_classes 0038 % - Firstly, we create a vector that will contain all the different 0039 % values of the classes. 0040 class_vector = 1:n_classes; 0041 % - We remove the class we are looking for in this iteration. 0042 class_vector(i) = []; 0043 % - We find the positions of the cases whose real class is i. 0044 pos_obs_class_i = real_class == i; 0045 % - Now, we get the predicted class for those cases. 0046 pred_class_obs_class_i = predicted_class(pos_obs_class_i); 0047 % - Now, we compute the number of true positives and false positives 0048 % for the i-th class. 0049 % -- True positives. 0050 extended_cm(i,i) = sum(real_class(pos_obs_class_i) == pred_class_obs_class_i); 0051 % -- False positives. To compute this values, we will need to loop for 0052 % all the other classes. 0053 for j = 1:length(class_vector) 0054 % --- We seek for the cases misclassified for the j-th class. 0055 extended_cm(i,class_vector(j)) = sum(pred_class_obs_class_i == class_vector(j)); 0056 end 0057 end