-
Notifications
You must be signed in to change notification settings - Fork 0
/
confusion.getMatrix.m
253 lines (233 loc) · 9.94 KB
/
confusion.getMatrix.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
classdef confusion
methods (Static)
function [c_matrix,Result,RefereceResult]= getMatrix(actual,predict,Display)
%confusion matrix for multiple class start
%Inputs-1.Actual Class Labels,2.Predict Class Labels and 3.Display if need
%Outputs
%1.C-matrix-Confution Matrix
%2.Result-Struct Over all output Which has follwing
%3.RefereceResult indidual output Which has follwing
%%%%%%%%1acuuracy
%%%%%%%%2.error
%%%%%%%%3.Sensitivity (Recall or True positive rate)
%%%%%%%%4.Specificity
%%%%%%%%5.Precision
%%%%%%%%6.FPR-False positive rate
%%%%%%%%7.F_score
%%%%%%%%8.MCC-Matthews correlation coefficient
%%%%%%%%9.kappa-Cohen's kappa
%%Developer Er.Abbas Manthiri S
%%Date 25-12-2016
%%Mail Id: [email protected]
%%http://www.dataschool.io/simple-guide-to-confusion-matrix-terminology/
%%https://en.wikipedia.org/wiki/Confusion_matrix
% clc
% clear all
% close all
% %%Multiclass
% n=100;m=2;
% actual=round(rand(1,n)*m);
% predict=round(rand(1,n)*m);
% [c_matrix,Result,RefereceResult]= confusionmat(actual,predict)
%
% %DIsplay off
% % [c_matrix,Result,RefereceResult]= confusionmat(actual,predict,0)
%
% %%
% %single Class
% n=100;m=1;
% actual=round(rand(1,n)*m);
% predict=round(rand(1,n)*m);
% [c_matrix,Result]= confusionmat(actual,predict)
%%
%Condition Check
if (nargin < 2)
error('Not enough input arguments. Need atleast two vectors as input');
elseif (nargin == 2)
Display=1;
elseif (nargin > 3)
error('Too many input arguments.');
end
actual=actual(:);
predict=predict(:);
if length(actual) ~= length(predict)
error('Input have different lengths')
end
un_actual=unique(actual);
un_predict=unique(predict);
condition=length(un_actual)==length(un_predict);
if ~condition
error('Class List is not same in given inputs')
end
condition=(sum(un_actual==un_predict)==length(un_actual));
if ~condition
error('Class List in given inputs are different')
end
%%
%Start process
%Build Confusion matrix
%Set variables
class_list=un_actual;
disp('Class List in given sample')
disp(class_list)
fprintf('\nTotal Instance = %d\n',length(actual));
n_class=length(un_actual);
c_matrix=zeros(n_class);
predict_class=cell(1,n_class);
class_ref=cell(n_class,1);
row_name=cell(1,n_class);
%Calculate conufsion for all classes
for i=1:n_class
class_ref{i,1}=strcat('class',num2str(i),'==>',num2str(class_list(i)));
for j=1:n_class
val=(actual==class_list(i)) & (predict==class_list(j));
c_matrix(i,j)=sum(val);
predict_class{i,j}=sum(val);
end
row_name{i}=strcat('Actual_class',num2str(i));
disp(class_ref{i})
end
c_matrix_table=cell2table(predict_class);
c_matrix_table.Properties.RowNames=row_name;
disp('Confusion Matrix')
disp(c_matrix_table)
[Result,RefereceResult]=confusion.getValues(c_matrix);
%Output Struct for individual Classes
RefereceResult.Class=class_ref;
%%
%Diplay
% Display=1;
if Display
if n_class>2
disp('Multi-Class Confusion Matrix Output')
TruePositive=RefereceResult.TruePositive;
FalsePositive=RefereceResult.FalsePositive;
FalseNegative=RefereceResult.FalseNegative;
TrueNegative=RefereceResult.TrueNegative;
TFPN=table(TruePositive,FalsePositive,FalseNegative,TrueNegative,...
'RowNames',row_name);
disp(TFPN);
Param=struct2table(RefereceResult);
disp(Param)
else
disp('Two-Class Confution Matrix')
param={'','TruePositive','FalsePositive';...
'FalseNegative',c_matrix(1,1),c_matrix(1,2);...
'TrueNegative=TN',c_matrix(2,1),c_matrix(2,2)};
disp(param)
end
disp('Over all valuses')
disp(Result)
end
end
function [Result,RefereceResult]= getValues(c_matrix)
%%
%Finding
%1.TP-True Positive
%2.FP-False Positive
%3.FN-False Negative
%4.TN-True Negative
if (nargin < 1)
error('Not enough input arguments. Need atleast two vectors as input');
elseif (nargin > 1)
error('Too many input arguments.');
end
[row,col]=size(c_matrix);
if row~=col
error('Confusion matrix dimention is wrong')
end
n_class=row;
switch n_class
case 2
TP=c_matrix(1,1);
FN=c_matrix(1,2);
FP=c_matrix(2,1);
TN=c_matrix(2,2);
otherwise
TP=zeros(1,n_class);
FN=zeros(1,n_class);
FP=zeros(1,n_class);
TN=zeros(1,n_class);
for i=1:n_class
TP(i)=c_matrix(i,i);
FN(i)=sum(c_matrix(i,:))-c_matrix(i,i);
FP(i)=sum(c_matrix(:,i))-c_matrix(i,i);
TN(i)=sum(c_matrix(:))-TP(i)-FP(i)-FN(i);
end
end
%%
%Calulations
%1.P-Positive
%2.N-Negative
%3.acuuracy
%4.error
%5.Sensitivity (Recall or True positive rate)
%6.Specificity
%7.Precision
%8.FPR-False positive rate
%9.F_score
%10.MCC-Matthews correlation coefficient
%11.kappa-Cohen's kappa
P=TP+FN;
N=FP+TN;
switch n_class
case 2
accuracy=(TP+TN)/(P+N);
Error=1-accuracy;
Result.Accuracy=(accuracy);
Result.Error=(Error);
otherwise
accuracy=(TP)./(P+N);
Error=(FP)./(P+N);
Result.Accuracy=sum(accuracy);
Result.Error=sum(Error);
end
RefereceResult.AccuracyOfSingle=(TP ./ P)';
RefereceResult.ErrorOfSingle=1-RefereceResult.AccuracyOfSingle;
Sensitivity=TP./P;
Specificity=TN./N;
Precision=TP./(TP+FP);
FPR=1-Specificity;
beta=1;
F1_score=( (1+(beta^2))*(Sensitivity.*Precision) ) ./ ( (beta^2)*(Precision+Sensitivity) );
MCC=[( TP.*TN - FP.*FN ) ./ ( ( (TP+FP).*P.*N.*(TN+FN) ).^(0.5) );...
( FP.*FN - TP.*TN ) ./ ( ( (TP+FP).*P.*N.*(TN+FN) ).^(0.5) )] ;
MCC=max(MCC);
%Kappa Calculation BY 2x2 Matrix Shape
pox=sum(accuracy);
Px=sum(P);TPx=sum(TP);FPx=sum(FP);TNx=sum(TN);FNx=sum(FN);Nx=sum(N);
pex=( (Px.*(TPx+FPx))+(Nx.*(FNx+TNx)) ) ./ ( (TPx+TNx+FPx+FNx).^2 );
kappa_overall=([( pox-pex ) ./ ( 1-pex );( pex-pox ) ./ ( 1-pox )]);
kappa_overall=max(kappa_overall);
%Kappa Calculation BY n_class x n_class Matrix Shape
po=accuracy;
pe=( (P.*(TP+FP))+(N.*(FN+TN)) ) ./ ( (TP+TN+FP+FN).^2 );
kappa=([( po-pe ) ./ ( 1-pe );( pe-po ) ./ ( 1-po )]);
kappa=max(kappa);
%%
%Output Struct for individual Classes
% RefereceResult.Class=class_ref;
RefereceResult.AccuracyInTotal=accuracy';
RefereceResult.ErrorInTotal=Error';
RefereceResult.Sensitivity=Sensitivity';
RefereceResult.Specificity=Specificity';
RefereceResult.Precision=Precision';
RefereceResult.FalsePositiveRate=FPR';
RefereceResult.F1_score=F1_score';
RefereceResult.MatthewsCorrelationCoefficient=MCC';
RefereceResult.Kappa=kappa';
RefereceResult.TruePositive=TP';
RefereceResult.FalsePositive=FP';
RefereceResult.FalseNegative=FN';
RefereceResult.TrueNegative=TN';
%Output Struct for over all class lists
Result.Sensitivity=mean(Sensitivity);
Result.Specificity=mean(Specificity);
Result.Precision=mean(Precision);
Result.FalsePositiveRate=mean(FPR);
Result.F1_score=mean(F1_score);
Result.MatthewsCorrelationCoefficient=mean(MCC);
Result.Kappa=kappa_overall;
end
end
end