-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
pycm_func.py
1727 lines (1578 loc) · 46.3 KB
/
pycm_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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from __future__ import division
import math
import sys
import numpy
import operator as op
from functools import reduce
def isfile(f):
'''
This function check file object in python 2.7 & 3.x
:param f: input object
:type f : file object
:return: file type check as boolean
'''
return isinstance(
f, file) if sys.version_info[0] == 2 else hasattr(
f, 'read')
def DP_calc(TPR, TNR):
'''
This function 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 RCI_calc(mutual_information, reference_entropy):
'''
This function calculate RCI
:param mutual_information: Mutual Information
:type mutual_information : float
:param reference_entropy: Reference Entropy
:type reference_entropy : float
:return: RCI as float
'''
try:
return mutual_information / reference_entropy
except Exception:
return "None"
def dInd_calc(TNR, TPR):
'''
This function calculate dInd
: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):
'''
This function calculate sInd
:param dInd: dInd
:type dInd : float
:return: sInd as float
'''
try:
return 1 - (dInd / (math.sqrt(2)))
except Exception:
return "None"
def AUNP_calc(classes, P, POP, AUC_dict):
'''
This function calculate AUNP
:param classes: classes
:type classes : list
:param P: Condition positive
:type P : dict
:param POP: Population
:type POP : dict
:param AUC_dict: AUC (Area Under ROC Curve) for each class
:type AUC_dict : dict
:return: AUNP as float
'''
try:
result = 0
for i in classes:
result += (P[i] / POP[i]) * AUC_dict[i]
return result
except Exception:
return "None"
def AUC_calc(TNR, TPR):
'''
This function calculate Area Under 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 CBA_calc(classes, table, TOP, P):
'''
This function calculate CBA
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
:param TOP: Test outcome positive
:type TOP : dict
:param P: Condition positive
:type P : dict
:return: CBA as float
'''
try:
result = 0
class_number = len(classes)
for i in classes:
result += ((table[i][i]) / (max(TOP[i], P[i])))
return result / class_number
except Exception:
return "None"
def RR_calc(classes, table):
'''
This function calculate RR (Global Performance Index)
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
:return: RR as float
'''
try:
result = 0
class_number = len(classes)
for i in classes:
result += sum(list(table[i].values()))
return result / class_number
except Exception:
return "None"
def one_vs_all_func(classes, table, TP, TN, FP, FN, class_name):
'''
One-Vs-All mode handler
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
: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 class_name : target class name for One-Vs-All mode
:type class_name : any valid type
:return: [classes , table ] as list
'''
try:
report_classes = [str(class_name), "~"]
report_table = {str(class_name): {str(class_name): TP[class_name],
"~": FN[class_name]},
"~": {str(class_name): FP[class_name],
"~": TN[class_name]}}
return [report_classes, report_table]
except Exception:
return [classes, table]
def overall_MCC_calc(classes, table):
'''
This function calculate Overall MCC
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
:return: Overall_MCC as float
'''
try:
cov_x_y = 0
cov_x_x = 0
cov_y_y = 0
sigma1_x_x = 0
sigma2_x_x = 0
sigma1_y_y = 0
sigma2_y_y = 0
for i in classes:
for j in classes:
sigma1_x_x += table[j][i]
sigma1_y_y += table[i][j]
for k in classes:
cov_x_y += table[i][i] * table[k][j] - \
table[j][i] * table[i][k]
if i != j:
sigma2_x_x += table[k][j]
sigma2_y_y += table[j][k]
cov_x_x += sigma1_x_x * sigma2_x_x
cov_y_y += sigma1_y_y * sigma2_y_y
sigma1_x_x = 0
sigma2_x_x = 0
sigma1_y_y = 0
sigma2_y_y = 0
return cov_x_y / (math.sqrt(cov_y_y * cov_x_x))
except Exception:
return "None"
def CEN_misclassification_calc(classes, table, i, j, subject_class,
modified=False):
'''
This function calculate misclassification probability of classifying
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
: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 = 0
for k in classes:
result += (table[subject_class][k] + table[k][subject_class])
if modified:
result -= table[subject_class][subject_class]
result = table[i][j] / result
return result
except Exception:
return "None"
def CEN_calc(classes, table, class_name, modified=False):
'''
This function calculate CEN (Confusion Entropy)
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
: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(classes, table,
class_name, k,
class_name, modified)
P_k_j = CEN_misclassification_calc(classes, table, 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 convex_combination(classes, table, class_name, modified=False):
'''
This function calculate Overall_CEN coefficient
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
:param class_name: reviewed class name
:type class_name : any valid type
:param modified : modified mode flag
:type modified : bool
:return: coefficient as float
'''
try:
up = 0
down = 0
class_number = len(classes)
alpha = 1
if class_number == 2:
alpha = 0
for k in classes:
up += (table[class_name][k] + table[k][class_name])
for l in classes:
down += (2 * table[k][l])
if modified:
down -= (alpha * table[k][k])
if modified:
up -= table[class_name][class_name]
return up / down
except Exception:
return "None"
def overall_CEN_calc(classes, table, CEN_dict, modified=False):
'''
This function calculate Overall_CEN (Overall Confusion Entropy)
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
:param CEN_dict: CEN dictionary for each class
:type CEN_dict : dict
:param modified : modified mode flag
:type modified : bool
:return: Overall_CEN(MCEN) as float
'''
try:
result = 0
for i in classes:
result += (convex_combination(classes, table, i, modified) *
CEN_dict[i])
return result
except Exception:
return "None"
def IS_calc(TP, FP, FN, POP):
'''
This function calculate Information Score (IS)
: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 transpose_func(classes, table):
'''
This function transpose table
:param classes: classes
:type classes : list
:param table: input matrix
:type table : dict
:return: transposed table as dict
'''
transposed_table = table
for i, item1 in enumerate(classes):
for j, item2 in enumerate(classes):
if i > j:
temp = transposed_table[item1][item2]
transposed_table[item1][item2] = transposed_table[item2][item1]
transposed_table[item2][item1] = temp
return transposed_table
def ncr(n, r):
'''
This function calculate n choose r
:param n: n
:type n : int
:param r: r
:type r :int
:return: n choose r as int
'''
r = min(r, n - r)
numer = reduce(op.mul, range(n, n - r, -1), 1)
denom = reduce(op.mul, range(1, r + 1), 1)
return numer // denom
def p_value_calc(TP, POP, NIR):
'''
This function calculate p_value
:param TP: True Positive
:type TP : dict
:param POP: Population
:type POP : dict
:param NIR: No Information Rate
:type NIR : float
:return: p_value as float
'''
try:
n = list(POP.values())[0]
x = sum(list(TP.values()))
p = NIR
result = 0
for j in range(x):
result += ncr(n, j) * (p ** j) * ((1 - p) ** (n - j))
return 1 - result
except Exception:
return "None"
def NIR_calc(P, POP):
'''
This function calculate No Information Rate
:param P: Condition positive
:type P : dict
:param POP: Population
:type POP : dict
:return: NIR as float
'''
try:
max_P = max(list(P.values()))
length = list(POP.values())[0]
return max_P / length
except Exception:
return "None"
def hamming_calc(TP, POP):
'''
This function calculate hamming_loss
:param TP: True Positive
:type TP : dict
:param POP: Population
:type POP : dict
:return: hamming loss as float
'''
try:
length = list(POP.values())[0]
return (1 / length) * (length - sum(TP.values()))
except Exception:
return "None"
def zero_one_loss_calc(TP, POP):
'''
This function zero_one_loss
:param TP: True Positive
:type TP : dict
:param POP: Population
:type POP : dict
:return: zero_one loss as integer
'''
try:
length = list(POP.values())[0]
return (length - sum(TP.values()))
except Exception:
return "None"
def vector_filter(actual_vector, predict_vector):
'''
This function convert different type of items in vectors to str
:param actual_vector: actual values
:type actual_vector : list
:param predict_vector: predict value
:type predict_vector : list
:return: new actual and predict vector
'''
temp = []
temp.extend(actual_vector)
temp.extend(predict_vector)
for i in temp:
if not isinstance(i, type(temp[0])):
return [list(map(str, actual_vector)),
list(map(str, predict_vector))]
return [actual_vector, predict_vector]
def vector_check(vector):
'''
This function check input vector items type
:param vector: input vector
:type vector : list
:return: bool
'''
for i in vector:
if isinstance(i, int) == False:
return False
if i < 0:
return False
return True
def class_check(vector):
'''
This function check different items in matrix classes
:param vector: input vector
:type vector : list
:return: bool
'''
for i in vector:
if not isinstance(i, type(vector[0])):
return False
return True
def matrix_check(table):
'''
This function check input matrix format
:param table: input matrix
:type table : dict
:return: bool
'''
try:
if len(table.keys()) == 0:
return False
for i in table.keys():
if table.keys() != table[i].keys() or vector_check(
list(table[i].values())) == False:
return False
return True
except Exception:
return False
def matrix_params_from_table(table, transpose=False):
'''
This function calculate TP,TN,FP,FN from confusion matrix
:param table: input matrix
:type table : dict
:param transpose : transpose flag
:type transpose : bool
:return: [classes_list,table,TP,TN,FP,FN]
'''
classes = sorted(table.keys())
map_dict = {k: 0 for k in classes}
TP_dict = map_dict.copy()
TN_dict = map_dict.copy()
FP_dict = map_dict.copy()
FN_dict = map_dict.copy()
for i in classes:
TP_dict[i] = table[i][i]
for j in classes:
if j != i:
FN_dict[i] += table[i][j]
FP_dict[j] += table[i][j]
TN_dict[j] += sum(list(table[i].values())) - table[i][j]
if transpose:
temp = FN_dict
FN_dict = FP_dict
FP_dict = temp
table = transpose_func(classes, table)
return [classes, table, TP_dict, TN_dict, FP_dict, FN_dict]
def matrix_params_calc(actual_vector, predict_vector, sample_weight):
'''
This function calculate TP,TN,FP,FN for each class
:param actual_vector: actual values
:type actual_vector : list
:param predict_vector: predict value
:type predict_vector : list
:param sample_weight : sample weights list
:type sample_weight : list
:return: [classes_list,table,TP,TN,FP,FN]
'''
if isinstance(actual_vector, numpy.ndarray):
actual_vector = actual_vector.tolist()
if isinstance(predict_vector, numpy.ndarray):
predict_vector = predict_vector.tolist()
classes = set(actual_vector).union(set(predict_vector))
classes = sorted(classes)
map_dict = {k: 0 for k in classes}
TP_dict = map_dict.copy()
TN_dict = map_dict.copy()
FP_dict = map_dict.copy()
FN_dict = map_dict.copy()
table = {k: map_dict.copy() for k in classes}
weight_vector = [1] * len(actual_vector)
if isinstance(sample_weight, (list, numpy.ndarray)):
if len(sample_weight) == len(actual_vector):
weight_vector = sample_weight
for index, item in enumerate(actual_vector):
if (item in classes) and (predict_vector[index] in classes):
table[item][predict_vector[index]] += 1 * weight_vector[index]
if item == predict_vector[index]:
TP_dict[item] += 1 * weight_vector[index]
else:
FN_dict[item] += 1 * weight_vector[index]
FP_dict[predict_vector[index]] += 1 * weight_vector[index]
for i in classes:
if i != item and predict_vector[index] != i:
TN_dict[i] += 1 * weight_vector[index]
return [classes, table, TP_dict, TN_dict, FP_dict, FN_dict]
def entropy_calc(item, POP):
'''
This function calculate reference and response likelihood
:param item : TOP or P
:type item : dict
:param POP: Population
:type POP : dict
:return: reference or response likelihood
'''
try:
result = 0
for i in item.keys():
likelihood = item[i] / POP[i]
if likelihood != 0:
result += likelihood * math.log(likelihood, 2)
return -result
except Exception:
return "None"
def kappa_no_prevalence_calc(overall_accuracy):
'''
This function calculate Kappa No Prevalence
:param overall_accuracy: overall accuracy
:type overall_accuracy : float
:return: Kappa No Prevalence as float
'''
try:
result = 2 * overall_accuracy - 1
return result
except Exception:
return "None"
def cross_entropy_calc(TOP, P, POP):
'''
This function calculate cross entropy
:param TOP: Test outcome positive
:type TOP : dict
:param P: Condition positive
:type P : dict
:param POP: Population
:type POP : dict
:return: cross entropy as float
'''
try:
result = 0
for i in TOP.keys():
reference_likelihood = P[i] / POP[i]
response_likelihood = TOP[i] / POP[i]
if response_likelihood != 0 and reference_likelihood != 0:
result += reference_likelihood * \
math.log(response_likelihood, 2)
return -result
except Exception:
return "None"
def joint_entropy_calc(classes, table, POP):
'''
This function calculate joint entropy
:param classes: confusion matrix classes
:type classes : list
:param table: confusion matrix table
:type table : dict
:param POP: Population
:type POP : dict
:return: joint entropy as float
'''
try:
result = 0
classes.sort()
for i in classes:
for index, j in enumerate(classes):
p_prime = table[i][j] / POP[i]
if p_prime != 0:
result += p_prime * math.log(p_prime, 2)
return -result
except Exception:
return "None"
def conditional_entropy_calc(classes, table, P, POP):
'''
This function calculate conditional entropy
:param classes: confusion matrix classes
:type classes : list
:param table: confusion matrix table
:type table : dict
:param P: Condition positive
:type P : dict
:param POP: Population
:type POP : dict
:return: conditional entropy as float
'''
try:
result = 0
classes.sort()
for i in classes:
temp = 0
for index, j in enumerate(classes):
p_prime = 0
if P[i] != 0:
p_prime = table[i][j] / P[i]
if p_prime != 0:
temp += p_prime * math.log(p_prime, 2)
result += temp * (P[i] / POP[i])
return -result
except Exception:
return "None"
def mutual_information_calc(response_entropy, conditional_entropy):
'''
This function calculate mutual information
:param response_entropy: response entropy
:type response_entropy : float
:param conditional_entropy: conditional entropy
:type conditional_entropy : float
:return: mutual information as float
'''
try:
return response_entropy - conditional_entropy
except Exception:
return "None"
def kl_divergence_calc(P, TOP, POP):
'''
This function calculate Kullback-Liebler (KL) divergence
:param P: Condition positive
:type P : dict
:param TOP: Test outcome positive
:type TOP : dict
:param POP: Population
:type POP : dict
:return: Kullback-Liebler (KL) divergence as float
'''
try:
result = 0
for i in TOP.keys():
reference_likelihood = P[i] / POP[i]
response_likelihood = TOP[i] / POP[i]
result += reference_likelihood * \
math.log((reference_likelihood / response_likelihood), 2)
return result
except Exception:
return "None"
def lambda_B_calc(classes, table, TOP, POP):
'''
This function calculate Goodman and Kruskal's lambda B
:param classes: confusion matrix classes
:type classes : list
:param table: confusion matrix table
:type table : dict
:param TOP: Test outcome positive
:type TOP : dict
:param POP: Population
:type POP : dict
:return: Goodman and Kruskal's lambda B as float
'''
try:
result = 0
classes.sort()
length = list(POP.values())[0]
maxresponse = max(list(TOP.values()))
for i in classes:
result += max(list(table[i].values()))
result = (result - maxresponse) / (length - maxresponse)
return result
except Exception:
return "None"
def lambda_A_calc(classes, table, P, POP):
'''
This function calculate Goodman and Kruskal's lambda A
:param classes: confusion matrix classes
:type classes : list
:param table: confusion matrix table
:type table : dict
:param P: Condition positive
:type P : dict
:param POP: Population
:type POP : dict
:return: Goodman and Kruskal's lambda A as float
'''
try:
result = 0
classes.sort()
maxreference = max(list(P.values()))
length = list(POP.values())[0]
for i in classes:
col = []
for col_item in table.values():
col.append(col_item[i])
result += max(col)
result = (result - maxreference) / (length - maxreference)
return result
except Exception:
return "None"
def chi_square_calc(classes, table, TOP, P, POP):
'''
This function calculate chi-squared
:param classes: confusion matrix classes
:type classes : list
:param table: confusion matrix table
:type table : dict
:param TOP: Test outcome positive
:type TOP : dict
:param P: Condition positive
:type P : dict
:param POP: Population
:type POP : dict
:return: chi_squared as float
'''
try:
result = 0
classes.sort()
for i in classes:
for index, j in enumerate(classes):
expected = (TOP[j] * P[i]) / (POP[i])
result += ((table[i][j] - expected)**2) / expected
return result
except Exception:
return "None"
def phi_square_calc(chi_square, POP):
'''
This function calculate phi_squared
:param chi_square: chi_squared
:type chi_square : float
:param POP: Population
:type POP : dict
:return: phi_squared as float
'''
try:
return chi_square / (list(POP.values())[0])
except Exception:
return "None"
def cramers_V_calc(phi_square, classes):
'''
This function calculate Cramer's V
:param phi_square: phi_squared
:type phi_square : float
:param classes: confusion matrix classes
:type classes : list
:return: phi_squared as float
'''
try:
return math.sqrt((phi_square / (len(classes) - 1)))
except Exception:
return "None"
def DF_calc(classes):
'''
This function calculate chi squared degree of freedom
:param classes: confusion matrix classes
:type classes : list
:return: DF as int
'''
try:
return (len(classes) - 1)**2
except Exception:
return "None"
def TTPN_calc(Item1, Item2):
'''
This function 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 (5 Decimal Precision)
'''
try:
result = Item1 / (Item1 + Item2)
return result
except ZeroDivisionError:
return "None"
def FXR_calc(Item1):
'''
This function calculate FNR,FPR,FDR,FOR
:param Item1: Item In Expression
:type Item1:float
:return: result as float (5 Decimal Precision)
'''
try:
result = 1 - Item1
return result
except Exception:
return "None"
def ACC_calc(TP, TN, FP, FN):
'''
This functuon 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 ERR_calc(ACC):
'''
This function calculate Error Rate
:param ACC: Accuracy
:type ACC: float
:return: Error Rate as float
'''
try:
return 1 - ACC
except Exception:
return "None"
def F_calc(TP, FP, FN, Beta):
'''
This function 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 : 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):
'''
This function calculate Matthews correlation coefficient (MCC)
: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):
'''
This function calculate Informedness or Bookmaker Informedness (BM) and Markedness (MK)
:param Item1: Item1 in expression
:type Item1:float
:param Item2: Item2 in expression