File size: 1,545 Bytes
edcf5ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np


def compute_accuracy(tp, tn, fn, fp):  # only fit 2 cls condition
    """

    Accuracy = (TP + TN) / (FP + FN + TP + TN)

    """
    if tp + tn + fn + fp == 0:
        return 0.0
    return ((tp + tn) * 100) / float(tp + tn + fn + fp)


def compute_specificity(tn, fp):
    """

    Precision = TN  / (FN + TP)

    """
    if tn + fp == 0:
        return 0.0
    return (tn * 100) / float(tn + fp)


def compute_sensitivity(tp, fn):  # equal to recall
    """

    Recall = TP / (FN + TP)

    """
    if tp + fn == 0:
        return 0.0
    return (tp * 100) / float(tp + fn)


def compute_precision(tp, fp):  # equal to Positive Predictive Value(PPV)
    """

    Precision = TP  / (FP + TP)

    """
    if tp + fp == 0:
        return 0.0
    return (tp * 100) / float(tp + fp)


def compute_recall(tp, fn):  # equal to Sensitivity
    """

    Recall = TP / (FN + TP)

    """
    if tp + fn == 0:
        return 0.0
    return (tp * 100) / float(tp + fn)


def compute_f1_score(tp, tn, fp, fn):
    # calculates the F1 score
    precision = compute_precision(tp, fp) / 100
    recall = compute_recall(tp, fn) / 100

    if precision + recall == 0:
        return 0.0

    f1_score = (2 * precision * recall) / (precision + recall)
    return f1_score * 100


def compute_NPV(tn, fn):  # Negative Predictive Value
    """

    Negative Predictive Value = tn  / (tn + fn)

    """
    if tn + fn == 0:
        return 0.0
    return (tn * 100) / float(tn + fn)