9951 explained code solutions for 126 technologies


python-kerasHow can I calculate the F1 score using Python and Keras?


The F1 score is a metric used to measure the accuracy of a model. It is the harmonic mean of precision and recall. In Python and Keras, the F1 score can be calculated using the Keras backend functions precision() and recall().

Example code

from keras import backend as K

y_true = K.variable([[0, 1, 0], [0, 0, 1]])
y_pred = K.variable([[0, 0, 1], [0, 0, 1]])

def f1_score(y_true, y_pred):
    precision = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) / (K.sum(K.round(K.clip(y_pred, 0, 1))) + K.epsilon())
    recall = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) / (K.sum(K.round(K.clip(y_true, 0, 1))) + K.epsilon())
    return 2 * ((precision * recall) / (precision + recall + K.epsilon()))

f1_score = f1_score(y_true, y_pred)

Output example

0.6666666865348816

The code above calculates the F1 score using the Keras backend functions precision() and recall(). First, two variables y_true and y_pred are created to store the true labels and predicted labels, respectively. Then, a function f1_score() is defined to calculate the F1 score using the precision and recall values. The precision and recall values are calculated using the Keras backend functions precision() and recall(), and the F1 score is calculated using the formula for harmonic mean. Finally, the F1 score is calculated by calling the f1_score() function.

Parts of Code:

  1. y_true = K.variable([[0, 1, 0], [0, 0, 1]]): This creates a variable y_true to store the true labels.
  2. y_pred = K.variable([[0, 0, 1], [0, 0, 1]]): This creates a variable y_pred to store the predicted labels.
  3. precision = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) / (K.sum(K.round(K.clip(y_pred, 0, 1))) + K.epsilon()): This calculates the precision value using the Keras backend function precision().
  4. recall = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) / (K.sum(K.round(K.clip(y_true, 0, 1))) + K.epsilon()): This calculates the recall value using the Keras backend function recall().
  5. return 2 ((precision recall) / (precision + recall + K.epsilon())): This calculates the F1 score using the formula for harmonic mean.
  6. f1_score = f1_score(y_true, y_pred): This calls the f1_score() function to calculate the F1 score.

Helpful links

Edit this code on GitHub