|
import numpy as np
|
|
|
|
|
|
def compute_accuracy(tp, tn, fn, fp):
|
|
"""
|
|
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):
|
|
"""
|
|
Recall = TP / (FN + TP)
|
|
"""
|
|
if tp + fn == 0:
|
|
return 0.0
|
|
return (tp * 100) / float(tp + fn)
|
|
|
|
|
|
def compute_precision(tp, fp):
|
|
"""
|
|
Precision = TP / (FP + TP)
|
|
"""
|
|
if tp + fp == 0:
|
|
return 0.0
|
|
return (tp * 100) / float(tp + fp)
|
|
|
|
|
|
def compute_recall(tp, fn):
|
|
"""
|
|
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):
|
|
|
|
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 = tn / (tn + fn)
|
|
"""
|
|
if tn + fn == 0:
|
|
return 0.0
|
|
return (tn * 100) / float(tn + fn)
|
|
|
|
|