Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

metrics.F1Score

Keras F1Score computes the harmonic mean of precision and recall for multiclass or multilabel classification. Configure average for per-class, micro, macro, or weighted aggregation, and threshold for binary label decisions.

How it works

F1Score accumulates precision and recall statistics from true and predicted labels, then returns their harmonic mean. It supports multiclass and multilabel classification; averaging controls whether the result is per class or aggregated.

Syntax

keras.metrics.F1Score(average=None, threshold=None, name="f1_score", dtype=None)

Parameters

NameTypeDescription
averagestr | NoneAveraging mode: None, "micro", "macro", or "weighted"; defaults to None.
thresholdfloat | NoneDecision cutoff for converting predictions to binary labels. If omitted, predictions use last-axis argmax.
namestrName assigned to the metric; defaults to "f1_score".
dtypestr | dtype | NoneOptional data type for the metric result.

Returns

A float F-1 Score; with average=None, result() returns one score per class.

Key points

  • The metric computes F1, combining precision and recall into one classification score.
  • F1 uses the harmonic mean, so a low precision or recall limits the combined score.
  • Results range from 0 to 1.
  • The class supports both multiclass and multilabel classification tasks.
  • Averaging accepts None, micro, macro, or weighted.
  • The optional dtype controls the result data type.

Common mistakes

  • The default average=None produces one score per class rather than a single scalar.

Example: Compute multilabel F1 scores

import numpy as np
import keras

metric = keras.metrics.F1Score(threshold=0.5)
y_true = np.array([[1, 1, 1], [1, 0, 0], [1, 1, 0]], np.int32)
y_pred = np.array([[0.2, 0.6, 0.7], [0.2, 0.6, 0.6], [0.6, 0.8, 0.0]], np.float32)
metric.update_state(y_true, y_pred)
print(metric.result())

Creates a multilabel F1 metric, accumulates one batch, and reads the per-class result.

When to use it

Compare predicted class assignments with ground-truth labels.

Multiclass evaluation

Score independent labels using a decision threshold.

Multilabel evaluation

Use macro or weighted aggregation to summarize class-level performance.

Imbalanced classification

Interview questions

QWhat does Keras F1Score measure?

F1Score is the harmonic mean of precision and recall: 2 * (precision * recall) / (precision + recall). Its output is bounded between 0 and 1.

Follow-up: How would you obtain one value across all classes? Set average="micro", which computes statistics globally from total counts.

QExplain the average options.

average=None returns a score for every class. "macro" averages class scores equally, while "weighted" averages them using class support.

Follow-up: When can macro and weighted averages differ substantially? They diverge when class frequencies are imbalanced, because weighted averaging gives more influence to frequent classes.

QHow does threshold affect predictions?

With threshold=None, Keras selects the class with the largest prediction on the last axis and converts it to one-hot form. A numeric threshold instead converts predictions to binary labels using that cutoff.

Follow-up: Which setting is generally appropriate for multilabel outputs? Use a threshold chosen for the application rather than argmax, because multiple labels may be positive simultaneously.

  • keras.metrics.FBetaScore
  • keras.metrics.Precision
  • keras.metrics.Recall
  • keras.metrics.Metric

Source: Classification metrics based on True/False positives & .... Checked 2026-10-15.