Skip to content

Instantly share code, notes, and snippets.

@Tony363
Created March 20, 2023 16:10
Show Gist options
  • Save Tony363/292c4bdcc81c667fbc78ba97d343892f to your computer and use it in GitHub Desktop.
Save Tony363/292c4bdcc81c667fbc78ba97d343892f to your computer and use it in GitHub Desktop.
def classification_report(y_test, y_pred):
# calculate precision, recall, f1-score
# TODO:
cm = confusion_matrix(y_test,y_pred)
precision = cm[1,1]/(cm[1,1] + cm[0,1])
recall = cm[1,1]/(cm[1,1] + cm[1,0])
f1 = 2*(precision * recall)/(precision + recall)
acc = (cm[1,1] + cm[0,0]) / np.sum(cm.flatten())
# end TODO
return(precision,recall,f1,acc)
def confusion_matrix(y_test, y_pred):
# return the 2x2 matrix
# TODO:
# https://stackoverflow.com/questions/68157408/using-numpy-to-test-for-false-positives-and-false-negatives
result = np.array([[0, 0], [0, 0]])
result[1,1] = np.sum(np.logical_and(y_pred == 1, y_test == 1))
result[0,0] = np.sum(np.logical_and(y_pred == 0, y_test == 0))
result[0,1] = np.sum(np.logical_and(y_pred == 1, y_test == 0))
result[1,0] = np.sum(np.logical_and(y_pred == 0, y_test == 1))
# end TODO
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment