File Exchange

Confusion matrix / Matching matrix along with Precision, Sensitivity, Specificity and Model Accuracy

version 1.1.0.0 (4.33 KB) by
CFMATRIX2 calculates the confusion matrix and related parameters for a classification algo.

Updated 29 Mar 2012

View Version History

function [confmatrix] = cfmatrix2(actual, predict, classlist, per, printout)

CFMATRIX2 calculates the confusion matrix for any prediction algorithm ( prediction algorithm generates a list of classes to which each test feature vector is assigned );

Outputs:

Confusion matrix

Also the TP, FP, FN and TN are output for each class based on

http://en.wikipedia.org/wiki/Confusion_matrix

The Precision, Sensitivity and Specificity for each class have also been added in this update along with the overall accuracy of the model.

% Further description of the outputs:
%
% True Postive [TP] = Condition Present + Positive result
% False Positive [FP] = Condition absent + Positive result [Type I error]
% False (invalid) Negative [FN] = Condition present + Negative result [Type II error]
% True (accurate) Negative [TN] = Condition absent + Negative result
% Precision(class) = TP(class) / ( TP(class) + FP(class) )
% Sensitivity(class) = Recall(class) = TruePositiveRate(class)
% = TP(class) / ( TP(class) + FN(class) )
% Specificity ( mostly used in 2 class problems )=
% TrueNegativeRate(class)
% = TN(class) / ( TN(class) + FP(class) )
%
% Inputs:
%
% 1. actual / 2. predict
The inputs provided are the 'actual' classes vector and the 'predict'ed classes vector. The actual classes are the classes to which the input feature vectors belong. The predicted classes are the class to which the input feature vectors are predicted to belong to, based on a prediction algorithm. The length of actual class vector and the predicted class vector need to be the same. If they are not the same, an error message is displayed.
% 3. classlist
The third input provides the list of all the classes {p,n,...} for which the classification is being done. All classes are numbers.
% 4. per = 1/0 (default = 0)
This parameter when set to 1 provides the values in the confusion matrix as percentages. The default provides the values in numbers.
% 5. printout = 1/0 ( default = 1 )
This parameter when set to 1 provides output on the matlab terminal and can be used to suppress output by setting to 0 ( default = 1 ). Assuming 'printout' of output use case would be more common and at the same time provided option to suppress output when the number of classes can be very large.

% Example:
% >> a = [ 1 2 3 1 2 3 1 1 2 3 2 1 1 2 3];
% >> b = [ 1 2 3 1 2 3 1 1 1 2 2 1 2 1 3];
% >> Cf = cfmatrix2(a, b, [1 2 3], 0, 1);
% is equivalent to
% >> Cf = cfmatrix2(a, b);
% The values of classlist(unique from actual), per(0), printout(1) are set
% to the respective defaults.
%
%

% a. Pre-initialize confmatrix
% b. Simplified logic making the code more readable and faster (based on comments from an interviewer who reviewed the code)
% c. Provide input variable 'printout' as an option to suppress output to screen ( output to display is still the default (printout = 1) assuming that will be the more common use case ).
% d. Added Precision(class), Sensitivity(class), Specificity(class) and the overall accuracy of model calculations

Cite As

Avinash Uppuluri (2020). Confusion matrix / Matching matrix along with Precision, Sensitivity, Specificity and Model Accuracy (https://www.mathworks.com/matlabcentral/fileexchange/21212-confusion-matrix-matching-matrix-along-with-precision-sensitivity-specificity-and-model-accuracy), MATLAB Central File Exchange. Retrieved .

Avinash Uppuluri

Hi Atam,

Sorry about the delay in response but I have uploaded code to calculate the Precision(class), Sensitivity(class), Specificity(class) and the overall accuracy of model ( along with TP, TN, FN, FP) from the confusion matrix.

Thanks

Avinash Uppuluri

Thanks for pointing out the mistake in calculating TN in my earlier post. Here is the corrected code.

TPFPFNTN = zeros(4, classlist);

disp('------------------------------------------');
disp(' Actual Classes');
disp(line_two);

temps1 = sprintf(' TP ');
temps2 = sprintf(' FP ');
temps3 = sprintf(' FN ');
temps4 = sprintf(' TN ');
for i = 1:n_class
TPFPFNTN(1, i) = confmatrix(i,i); % TP
temps1 = strcat(temps1,sprintf(' | %2.1f ',TPFPFNTN(1, i)));
TPFPFNTN(2, i) = sum(confmatrix(i,:))-confmatrix(i,i); % FP
temps2 = strcat(temps2,sprintf(' | %2.1f ',TPFPFNTN(2, i) ));
TPFPFNTN(3, i) = sum(confmatrix(:,i))-confmatrix(i,i); % FN
temps3 = strcat(temps3,sprintf(' | %2.1f ',TPFPFNTN(3, i) ));
TPFPFNTN(4, i) = sum(confmatrix(:)) - sum(confmatrix(i,:)) - sum(confmatrix(:,i)) + confmatrix(i,i);
temps4 = strcat(temps4,sprintf(' | %2.1f ',TPFPFNTN(4, i) ));
end

disp(temps1); disp(temps2); disp(temps3); disp(temps4);
clear temps1 temps2 temps3 temps4

Thanks

Avinash Uppuluri

Hi Stuart,

That is a good point. I will submit shortly an update that provides option to silence outputs along with a few other changes to the code.

Thanks

Stuart Layton

Why do you display the matrix by default? This is a real problem for large matrices.

Hi avinash, i think the formula for calculated TN (True Negative) is something wrong... I'm sorry if wrong...

Avinash Uppuluri

Hi Atam,
If you can give me a definition of how you want to measure accuracy and precision I can include it in the code.

Thanks.

Atam Tsalikian

Hi. How can we obtain accuracy and precision from this code?
Thanxs!

Avinash Uppuluri

Hi Avinash,

I stumbled into your work about confusion matrix in matlab forum....My question, how do obtain true positives,true negative, false positive and false negative from your code?

Thanks
--
Fess Iyuke

Hi Fess,

Please add this piece of code at the end of the program you downloaded and it should give you the TP, FP, FN, TN values. Hope this helps. If there are any issues with the code please let me know. I will try to update my submission soon.

% True Postive [TP] = Condition Present + Positive result
% False Positive [FP] = Condition absent + Positive result [Type
% I error]
% False (invalid) Negative [FN] = Condition present + Negative result [Type
% II error]
% True (accurate) Negative [TN] = Condition absent + Negative result

disp('------------------------------------------');
disp(' Actual Classes');
disp(line_two);

temps = sprintf(' TP ');
for i = 1:n_class
temps = strcat(temps,sprintf(' | %2.1f ',confmatrix(i,i)));
end
disp(temps);
clear temps

temps = sprintf(' FP ');
for i = 1:n_class
temps = strcat(temps,sprintf(' | %2.1f ',sum(confmatrix(i,:))-confmatrix(i,i) ));
end
disp(temps);
clear temps

temps = sprintf(' FN ');
for i = 1:n_class
temps = strcat(temps,sprintf(' | %2.1f ',sum(confmatrix(:,i))-confmatrix(i,i) ));
end
disp(temps);
clear temps

temps = sprintf(' TN ');
for i = 1:n_class
temps = strcat(temps,sprintf(' | %2.1f ',sum(diag(confmatrix))-confmatrix(i,i) ));
end
disp(temps);
clear temps

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!