/
multiclass_metrics_special.m
161 lines (146 loc) · 5.45 KB
/
multiclass_metrics_special.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
function [Result,ReferenceResult] = multiclass_metrics_special(confMatrix)
%%***********************************************************************%
%* Multiclass metrics *%
%* Finds the multiclss metrics provided a confusion matrix. *%
%* *%
%* Code author: Preetham Manjunatha *%
%* Github link: https://github.com/preethamam %*
%* Date: 11/24/2021 *%
%************************************************************************%
%
%************************************************************************%
% %confusion matrix for multiple class start
% %Inputs-1.Actual Class Labels,2.Predict Class Labels and 3.Display if need
% %Outputs
%
% %1.Result-Struct Over all output Which has follwing
% %2.RefereceResult indidual output Which has follwing
% %%%%%%%%1.acuuracy
% %%%%%%%%2.error
% %%%%%%%%3.Recall (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
%
% %%Original Developer Mr.Abbas Manthiri S
% %%Date 25-12-2016
% %%Mail Id: abbasmanthiribe@gmail.com
% %%http://www.dataschool.io/simple-guide-to-confusion-matrix-terminology/
% %%https://en.wikipedia.org/wiki/Confusion_matrix
%
% %%Modified by Preetham Manjunatha
% %%Date 02-03-2020
% %%Note: Fixed Abbas written code where the NaN and Inf values were
% not considered while performaing the class average.
%
% % clc
% % clear all
% % close all
% % %%Multiclass or two class
% % [Result,RefereceResult] = multiclass_metrics(confMatrix)
% Size of confusion matrix
[row,col]=size(confMatrix);
if row~=col
error('Confusion matrix dimention is wrong')
end
% Number of classes
n_class=row;
% Find TP, FN, FP and TN
switch n_class
case 2
TP=confMatrix(1,1);
FN=confMatrix(1,2);
FP=confMatrix(2,1);
TN=confMatrix(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)=confMatrix(i,i);
FN(i)=sum(confMatrix(i,:))-confMatrix(i,i);
FP(i)=sum(confMatrix(:,i))-confMatrix(i,i);
TN(i)=sum(confMatrix(:))-TP(i)-FP(i)-FN(i);
end
end
%% Calulations
%1.P-Positive
%2.N-Negative
%3.acuuracy
%4.error
%5.Recall (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
ReferenceResult.AccuracyOfSingle=(TP ./ P)';
ReferenceResult.ErrorOfSingle=1-ReferenceResult.AccuracyOfSingle;
Recall=TP./P;
Specificity=TN./N;
Precision=TP./(TP+FP);
FPR=1-Specificity;
beta=1;
F1_score=( (1+(beta^2))*(Recall.*Precision) ) ./ ( (beta^2)*(Precision+Recall) );
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;
ReferenceResult.AccuracyInTotal=accuracy';
ReferenceResult.ErrorInTotal=Error';
ReferenceResult.Recall=Recall';
ReferenceResult.Specificity=Specificity';
ReferenceResult.Precision=Precision';
ReferenceResult.FalsePositiveRate=FPR';
ReferenceResult.F1_score=F1_score';
ReferenceResult.MatthewsCorrelationCoefficient=MCC';
ReferenceResult.Kappa=kappa';
ReferenceResult.TruePositive=TP';
ReferenceResult.FalsePositive=FP';
ReferenceResult.FalseNegative=FN';
ReferenceResult.TrueNegative=TN';
% Remove NANs and INFs
stats = [Precision', Recall', F1_score', MCC'];
stats(any(isinf(stats),2),:) = 0;
stats(any(isnan(stats),2),:) = 0;
%Output Struct for over all class lists
Result.Recall=mean(stats(:,2));
Result.Specificity=mean(Specificity);
Result.Precision=mean(stats(:,1));
Result.FalsePositiveRate=mean(FPR);
Result.F1_score=mean(stats(:,3));
Result.MatthewsCorrelationCoefficient=mean(stats(:,4));
Result.Kappa=kappa_overall;
end