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

Keras · Class

metrics.Precision

keras.metrics.Precision measures the fraction of predicted positives that are correct. It accumulates true and false positives and supports threshold-based, top-k, and class-specific evaluation.

How it works

Precision accumulates counts of true and false positives across updates, then returns true positives divided by true positives plus false positives. By default, predictions at or above a 0.5 threshold are treated as positive; the threshold, top-k selection, or class can be configured.

Syntax

keras.metrics.Precision(thresholds=None, top_k=None, class_id=None, name=None, dtype=None)

Parameters

NameTypeDescription
thresholdsfloat | list/tuple[float] | NoneOptional float or list/tuple of floats in [0, 1] used to classify predictions.
top_kint | NoneOptional number of highest-scoring predictions to consider.
class_idint | NoneOptional class index for class-specific precision.
namestr | NoneOptional metric name.
dtypestr | dtype | NoneOptional metric data type.

Returns

A scalar precision result, computed from accumulated true-positive and false-positive state.

Key points

  • The metric measures how many predicted positives correspond to actual positive labels.
  • Its running state consists of true-positive and false-positive counts.
  • The reported value is the true-positive count divided by the combined true- and false-positive counts.
  • Thresholding converts prediction scores into positive or negative decisions.
  • The thresholds argument accepts one value or multiple float values within the 0-to-1 range.
  • top_k accepts an integer that determines how many highest-scoring predictions are considered.

Common mistakes

  • If neither thresholds nor top_k is supplied, the default classification threshold is 0.5.
  • When predictions are logits rather than sigmoid probabilities, use a threshold of 0.
  • Setting top_k changes the calculation from thresholding to evaluating membership among the highest-scoring predictions.

Example: Compute binary precision

import keras

metric = keras.metrics.Precision()
metric.update_state([0, 1, 1, 0], [0.2, 0.8, 0.4, 0.7])
print(float(metric.result()))  # 0.5

Creates a precision metric with the default 0.5 threshold, updates it with labels and prediction scores, then reads the accumulated result.

When to use it

Track positive prediction quality

Binary classification with probability outputs.

Evaluate logits with threshold zero

Models that emit raw logits.

Measure top-k precision

Multiclass classification where correct labels should appear among the best predictions.

Inspect class-specific precision

Monitoring one category in a multiclass model.

Interview questions

QHow does Keras Precision compute its result?

Precision is the fraction of predicted positives that are correct: true positives divided by true positives plus false positives. The metric maintains true-positive and false-positive state and returns this ratio.

Follow-up: How would you select positives from multiclass scores? Use top_k, optionally together with class_id when evaluating a particular class.

QWhat is the default threshold, and how can it be configured?

With no threshold or top_k, Keras uses a threshold of 0.5. A supplied threshold can be a single float or a list or tuple of floats in the interval from 0 through 1.

Follow-up: What should the threshold be for raw logits? Set it to 0 when the scores have not passed through sigmoid.

QWhat do top_k and class_id do?

top_k restricts evaluation to the highest-scoring k predictions for each batch entry. class_id restricts the calculation to entries where that class meets the threshold or top-k condition, then checks whether it is a correct label.

Follow-up: Why can top_k be preferable for multiclass output? It evaluates whether the true class appears among the selected highest-scoring classes instead of applying one global probability cutoff.

  • keras.metrics.Recall
  • keras.metrics.TruePositives
  • keras.metrics.FalsePositives
  • keras.metrics.PrecisionAtRecall
  • keras.metrics.AUC

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