Skip to content

Instantly share code, notes, and snippets.

@jednano
Last active October 26, 2022 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jednano/920c92cc03be5021ee3d2a0a5895b67e to your computer and use it in GitHub Desktop.
Save jednano/920c92cc03be5021ee3d2a0a5895b67e to your computer and use it in GitHub Desktop.
Classification Metrics
# TP = True Positive
# TN = True Negative
# FP = False Positive
# FN = False Negative
def precision(TP, FP):
return TP / (TP + FP)
def recall(TP, FN):
return TP / (TP + FN)
def negative_predictive_value(TN, FN):
return TN / (TN + FN)
def sensitivity(TP, FN):
return TP / (TP + FN)
def specificity(TN, FP):
return TN / (TN + FP)
def accuracy(TP, TN, FP, FN):
return (TP + TN) / (TP + TN + FP + FN)
def f1_score(TP, FP, FN):
P = precision(TP, FP)
R = recall(TP, FN)
return 2 * ((P * R) / (P + R))
def f1_score_alt(TP, FP, FN):
return TP / (TP + ((FP + FN) / 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment