Created
March 20, 2023 16:10
-
-
Save Tony363/292c4bdcc81c667fbc78ba97d343892f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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