/
pycm_class_func.py
675 lines (594 loc) · 15.2 KB
/
pycm_class_func.py
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# -*- coding: utf-8 -*-
"""Class statistics functions."""
from __future__ import division
import math
from .pycm_interpret import *
def TTPN_calc(item1, item2):
"""
Calculate TPR,TNR,PPV,NPV.
:param item1: item1 in fractional expression
:type item1 : int
:param item2: item2 in fractional expression
:type item2: int
:return: result as float
"""
try:
result = item1 / (item1 + item2)
return result
except ZeroDivisionError:
return "None"
def FXR_calc(item):
"""
Calculate FNR,FPR,FDR,FOR.
:param item: item In expression
:type item:float
:return: result as float
"""
try:
result = 1 - item
return result
except Exception:
return "None"
def ACC_calc(TP, TN, FP, FN):
"""
Calculate accuracy.
:param TP: true positive
:type TP : int
:param TN: true negative
:type TN : int
:param FP: false positive
:type FP : int
:param FN: false negative
:type FN : int
:return: accuracy as float
"""
try:
result = (TP + TN) / (TP + TN + FN + FP)
return result
except ZeroDivisionError:
return "None"
def F_calc(TP, FP, FN, beta):
"""
Calculate F-score.
:param TP: true positive
:type TP : int
:param FP: false positive
:type FP : int
:param FN: false negative
:type FN : int
:param beta : beta coefficient
:type beta : float
:return: F score as float
"""
try:
result = ((1 + (beta)**2) * TP) / \
((1 + (beta)**2) * TP + FP + (beta**2) * FN)
return result
except ZeroDivisionError:
return "None"
def MCC_calc(TP, TN, FP, FN):
"""
Calculate MCC (Matthews correlation coefficient).
:param TP: true positive
:type TP : int
:param TN: true negative
:type TN : int
:param FP: false positive
:type FP : int
:param FN: false negative
:type FN : int
:return: MCC as float
"""
try:
result = (TP * TN - FP * FN) / \
(math.sqrt((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN)))
return result
except ZeroDivisionError:
return "None"
def MK_BM_calc(item1, item2):
"""
Calculate BM (Informedness) and MK (Markedness).
:param item1: item1 in expression
:type item1:float
:param item2: item2 in expression
:type item2:float
:return: MK and BM as float
"""
try:
result = item1 + item2 - 1
return result
except Exception:
return "None"
def LR_calc(item1, item2):
"""
Calculate likelihood ratio.
:param item1: item1 in expression
:type item1:float
:param item2: item2 in expression
:type item2:float
:return: LR+ and LR- as float
"""
try:
result = item1 / item2
return result
except Exception:
return "None"
def PRE_calc(P, POP):
"""
Calculate prevalence.
:param P: condition positive
:type P : int
:param POP: population
:type POP : int
:return: prevalence as float
"""
try:
result = P / POP
return result
except Exception:
return "None"
def G_calc(item1, item2):
"""
Calculate G-measure & G-mean.
:param item1: PPV or TPR or TNR
:type item1 : float
:param item2: PPV or TPR or TNR
:type item2 : float
:return: G-measure or G-mean as float
"""
try:
result = math.sqrt(item1 * item2)
return result
except Exception:
return "None"
def RACC_calc(TOP, P, POP):
"""
Calculate random accuracy.
:param TOP: test outcome positive
:type TOP : int
:param P: condition positive
:type P : int
:param POP: population
:type POP:int
:return: RACC as float
"""
try:
result = (TOP * P) / ((POP) ** 2)
return result
except Exception:
return "None"
def RACCU_calc(TOP, P, POP):
"""
Calculate RACCU (Random accuracy unbiased).
:param TOP: test outcome positive
:type TOP : int
:param P: condition positive
:type P : int
:param POP: population
:type POP : int
:return: RACCU as float
"""
try:
result = ((TOP + P) / (2 * POP))**2
return result
except Exception:
return "None"
def ERR_calc(ACC):
"""
Calculate error rate.
:param ACC: accuracy
:type ACC: float
:return: error rate as float
"""
try:
return 1 - ACC
except Exception:
return "None"
def jaccard_index_calc(TP, TOP, P):
"""
Calculate Jaccard index for each class.
:param TP: true positive
:type TP : int
:param TOP: test outcome positive
:type TOP : int
:param P: condition positive
:type P : int
:return: Jaccard index as float
"""
try:
return TP / (TOP + P - TP)
except Exception:
return "None"
def IS_calc(TP, FP, FN, POP):
"""
Calculate IS (Information score).
:param TP: true positive
:type TP : int
:param FP: false positive
:type FP : int
:param FN: false negative
:type FN : int
:param POP: population
:type POP : int
:return: IS as float
"""
try:
result = -math.log(((TP + FN) / POP), 2) + \
math.log((TP / (TP + FP)), 2)
return result
except Exception:
return "None"
def CEN_misclassification_calc(
table,
TOP,
P,
i,
j,
subject_class,
modified=False):
"""
Calculate misclassification probability of classifying.
:param table: input matrix
:type table : dict
:param TOP: test outcome positive
:type TOP : int
:param P: condition positive
:type P : int
:param i: table row index (class name)
:type i : any valid type
:param j: table col index (class name)
:type j : any valid type
:param subject_class: subject to class (class name)
:type subject_class: any valid type
:param modified : modified mode flag
:type modified : bool
:return: misclassification probability of classifying as float
"""
try:
result = TOP + P
if modified:
result -= table[subject_class][subject_class]
result = table[i][j] / result
return result
except Exception:
return "None"
def CEN_calc(classes, table, TOP, P, class_name, modified=False):
"""
Calculate CEN (Confusion Entropy)/ MCEN(Modified Confusion Entropy).
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
:param TOP: test outcome positive
:type TOP : int
:param P: condition positive
:type P : int
:param class_name: reviewed class name
:type class_name : any valid type
:param modified : modified mode flag
:type modified : bool
:return: CEN(MCEN) as float
"""
try:
result = 0
class_number = len(classes)
for k in classes:
if k != class_name:
P_j_k = CEN_misclassification_calc(
table, TOP, P, class_name, k, class_name, modified)
P_k_j = CEN_misclassification_calc(
table, TOP, P, k, class_name, class_name, modified)
if P_j_k != 0:
result += P_j_k * math.log(P_j_k, 2 * (class_number - 1))
if P_k_j != 0:
result += P_k_j * math.log(P_k_j, 2 * (class_number - 1))
if result != 0:
result = result * (-1)
return result
except Exception:
return "None"
def AUC_calc(TNR, TPR):
"""
Calculate AUC (Area under the ROC curve for each class).
:param TNR: specificity or true negative rate
:type TNR : float
:param TPR: sensitivity, recall, hit rate, or true positive rate
:type TPR : float
:return: AUC as float
"""
try:
return (TNR + TPR) / 2
except Exception:
return "None"
def dInd_calc(TNR, TPR):
"""
Calculate dInd (Distance index).
:param TNR: specificity or true negative rate
:type TNR : float
:param TPR: sensitivity, recall, hit rate, or true positive rate
:type TPR : float
:return: dInd as float
"""
try:
result = math.sqrt(((1 - TNR)**2) + ((1 - TPR)**2))
return result
except Exception:
return "None"
def sInd_calc(dInd):
"""
Calculate sInd (Similarity index).
:param dInd: dInd
:type dInd : float
:return: sInd as float
"""
try:
return 1 - (dInd / (math.sqrt(2)))
except Exception:
return "None"
def DP_calc(TPR, TNR):
"""
Calculate DP (Discriminant power).
:param TNR: specificity or true negative rate
:type TNR : float
:param TPR: sensitivity, recall, hit rate, or true positive rate
:type TPR : float
:return: DP as float
"""
try:
X = TPR / (1 - TPR)
Y = TNR / (1 - TNR)
return (math.sqrt(3) / math.pi) * (math.log(X, 10) + math.log(Y, 10))
except Exception:
return "None"
def GI_calc(AUC):
"""
Calculate Gini index.
:param AUC: AUC (Area under the ROC curve)
:type AUC: float
:return: Gini index as float
"""
try:
return 2 * AUC - 1
except Exception:
return "None"
def lift_calc(PPV, PRE):
"""
Calculate lift score.
:param PPV: precision or positive predictive value
:type PPV : float
:param PRE: Prevalence
:type PRE : float
:return: lift score as float
"""
try:
return PPV / PRE
except Exception:
return "None"
def AM_calc(TOP, P):
"""
Calculate AM (Automatic/Manual).
:param TOP: test outcome positive
:type TOP : int
:param P: condition positive
:type P : int
:return: AM as int
"""
try:
return TOP - P
except Exception:
return "None"
def OP_calc(ACC, TPR, TNR):
"""
Calculate OP (Optimized precision).
:param ACC: accuracy
:type ACC : float
:param TNR: specificity or true negative rate
:type TNR : float
:param TPR: sensitivity, recall, hit rate, or true positive rate
:type TPR : float
:return: OP as float
"""
try:
RI = abs(TNR - TPR) / (TPR + TNR)
return ACC - RI
except Exception:
return "None"
def IBA_calc(TPR, TNR, alpha=1):
"""
Calculate IBA (Index of balanced accuracy).
:param TNR: specificity or true negative rate
:type TNR : float
:param TPR: sensitivity, recall, hit rate, or true positive rate
:type TPR : float
:param alpha : alpha coefficient
:type alpha : float
:return: IBA as float
"""
try:
IBA = (1 + alpha * (TPR - TNR)) * TPR * TNR
return IBA
except Exception:
return "None"
def BCD_calc(TOP, P, AM):
"""
Calculate BCD (Bray-Curtis dissimilarity).
:param TOP: test outcome positive
:type TOP : dict
:param P: condition positive
:type P : dict
:param AM: Automatic/Manual
:type AM : int
:return: BCD as float
"""
try:
TOP_sum = sum(TOP.values())
P_sum = sum(P.values())
return abs(AM) / (P_sum + TOP_sum)
except Exception:
return "None"
def class_statistics(TP, TN, FP, FN, classes, table):
"""
Return all class statistics.
:param TP: true positive dict for all classes
:type TP : dict
:param TN: true negative dict for all classes
:type TN : dict
:param FP: false positive dict for all classes
:type FP : dict
:param FN: false negative dict for all classes
:type FN : dict
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
:return: result as dict
"""
TPR = {}
TNR = {}
PPV = {}
NPV = {}
FNR = {}
FPR = {}
FDR = {}
FOR = {}
ACC = {}
F1_SCORE = {}
MCC = {}
BM = {}
MK = {}
PLR = {}
NLR = {}
DOR = {}
POP = {}
P = {}
N = {}
TOP = {}
TON = {}
PRE = {}
G = {}
RACC = {}
F05_Score = {}
F2_Score = {}
ERR = {}
RACCU = {}
Jaccrd_Index = {}
IS = {}
CEN = {}
MCEN = {}
AUC = {}
dInd = {}
sInd = {}
DP = {}
Y = {}
PLRI = {}
DPI = {}
AUCI = {}
GI = {}
LS = {}
AM = {}
BCD = {}
OP = {}
IBA = {}
GM = {}
for i in TP.keys():
POP[i] = TP[i] + TN[i] + FP[i] + FN[i]
P[i] = TP[i] + FN[i]
N[i] = TN[i] + FP[i]
TOP[i] = TP[i] + FP[i]
TON[i] = TN[i] + FN[i]
TPR[i] = TTPN_calc(TP[i], FN[i])
TNR[i] = TTPN_calc(TN[i], FP[i])
PPV[i] = TTPN_calc(TP[i], FP[i])
NPV[i] = TTPN_calc(TN[i], FN[i])
FNR[i] = FXR_calc(TPR[i])
FPR[i] = FXR_calc(TNR[i])
FDR[i] = FXR_calc(PPV[i])
FOR[i] = FXR_calc(NPV[i])
ACC[i] = ACC_calc(TP[i], TN[i], FP[i], FN[i])
F1_SCORE[i] = F_calc(TP[i], FP[i], FN[i], 1)
F05_Score[i] = F_calc(TP[i], FP[i], FN[i], 0.5)
F2_Score[i] = F_calc(TP[i], FP[i], FN[i], 2)
MCC[i] = MCC_calc(TP[i], TN[i], FP[i], FN[i])
BM[i] = MK_BM_calc(TPR[i], TNR[i])
MK[i] = MK_BM_calc(PPV[i], NPV[i])
PLR[i] = LR_calc(TPR[i], FPR[i])
NLR[i] = LR_calc(FNR[i], TNR[i])
DOR[i] = LR_calc(PLR[i], NLR[i])
PRE[i] = PRE_calc(P[i], POP[i])
G[i] = G_calc(PPV[i], TPR[i])
RACC[i] = RACC_calc(TOP[i], P[i], POP[i])
ERR[i] = ERR_calc(ACC[i])
RACCU[i] = RACCU_calc(TOP[i], P[i], POP[i])
Jaccrd_Index[i] = jaccard_index_calc(TP[i], TOP[i], P[i])
IS[i] = IS_calc(TP[i], FP[i], FN[i], POP[i])
CEN[i] = CEN_calc(classes, table, TOP[i], P[i], i)
MCEN[i] = CEN_calc(classes, table, TOP[i], P[i], i, True)
AUC[i] = AUC_calc(TNR[i], TPR[i])
dInd[i] = dInd_calc(TNR[i], TPR[i])
sInd[i] = sInd_calc(dInd[i])
DP[i] = DP_calc(TPR[i], TNR[i])
Y[i] = BM[i]
PLRI[i] = PLR_analysis(PLR[i])
DPI[i] = DP_analysis(DP[i])
AUCI[i] = AUC_analysis(AUC[i])
GI[i] = GI_calc(AUC[i])
LS[i] = lift_calc(PPV[i], PRE[i])
AM[i] = AM_calc(TOP[i], P[i])
OP[i] = OP_calc(ACC[i], TPR[i], TNR[i])
IBA[i] = IBA_calc(TPR[i], TNR[i])
GM[i] = G_calc(TNR[i], TPR[i])
for i in TP.keys():
BCD[i] = BCD_calc(TOP, P, AM[i])
result = {
"TPR": TPR,
"TNR": TNR,
"PPV": PPV,
"NPV": NPV,
"FNR": FNR,
"FPR": FPR,
"FDR": FDR,
"FOR": FOR,
"ACC": ACC,
"F1": F1_SCORE,
"MCC": MCC,
"BM": BM,
"MK": MK,
"PLR": PLR,
"NLR": NLR,
"DOR": DOR,
"TP": TP,
"TN": TN,
"FP": FP,
"FN": FN,
"POP": POP,
"P": P,
"N": N,
"TOP": TOP,
"TON": TON,
"PRE": PRE,
"G": G,
"RACC": RACC,
"F0.5": F05_Score,
"F2": F2_Score,
"ERR": ERR,
"RACCU": RACCU,
"J": Jaccrd_Index,
"IS": IS,
"CEN": CEN,
"MCEN": MCEN,
"AUC": AUC,
"sInd": sInd,
"dInd": dInd,
"DP": DP,
"Y": Y,
"PLRI": PLRI,
"DPI": DPI,
"AUCI": AUCI,
"GI": GI,
"LS": LS,
"AM": AM,
"BCD": BCD,
"OP": OP,
"IBA": IBA,
"GM": GM}
return result