Created
July 1, 2019 19:43
-
-
Save victorouttes/6c638f15669908cc5502db79e95e7f33 to your computer and use it in GitHub Desktop.
Keras Recall, Precision and F1 score for binary classification
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 recall(y_true, y_pred): | |
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) | |
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1))) | |
recall = true_positives / (possible_positives + K.epsilon()) | |
return recall | |
def precision(y_true, y_pred): | |
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) | |
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1))) | |
precision = true_positives / (predicted_positives + K.epsilon()) | |
return precision | |
def f1(y_true, y_pred): | |
precision = precision(y_true, y_pred) | |
recall = recall(y_true, y_pred) | |
return 2*((precision*recall)/(precision+recall+K.epsilon())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment