Code covered by the BSD License  

Highlights from
Confusion matrix / Matching matrix

Be the first to rate this file! 20 Downloads (last 30 days) File Size: 2.14 KB File ID: #21212

Confusion matrix / Matching matrix

by Avinash Uppuluri

 

25 Aug 2008 (Updated 23 Sep 2008)

CFMATRIX calculates the confusion matrix for any classification algorithm that generates a list of c

| Watch this File

File Information
Description

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

% Outputs: confusion matrix
%
%
% 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.
%
% 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 = cfmatrix(a, b);

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
18 Jul 2010 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

06 Nov 2010 Atam Tsalikian

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

12 Mar 2011 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.

17 Apr 2011 Atam Tsalikian

Take a look here: http://www2.cs.uregina.ca/~hamilton/courses/831/notes/confusion_matrix/confusion_matrix.html
I think it's helpfull.

19 Nov 2011 Madi

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

Please login to add a comment or rating.
Updates
23 Sep 2008

Improve description

Tag Activity for this File
Tag Applied By Date/Time
statistics Avinash Uppuluri 22 Oct 2008 10:16:01
probability Avinash Uppuluri 22 Oct 2008 10:16:01
confusion matrix Avinash Uppuluri 22 Oct 2008 10:16:01
algorithm Avinash Uppuluri 22 Oct 2008 10:16:01

Contact us at files@mathworks.com