## 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

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 (2023). 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 .

##### 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!

#### cfmatrix/

Version Published Release Notes
1.1.0.0