Track positive prediction quality
Binary classification with probability outputs.
Keras · Class
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.
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.
keras.metrics.Precision(thresholds=None, top_k=None, class_id=None, name=None, dtype=None)| Name | Type | Description |
|---|---|---|
thresholds | float | list/tuple[float] | None | Optional float or list/tuple of floats in [0, 1] used to classify predictions. |
top_k | int | None | Optional number of highest-scoring predictions to consider. |
class_id | int | None | Optional class index for class-specific precision. |
name | str | None | Optional metric name. |
dtype | str | dtype | None | Optional metric data type. |
A scalar precision result, computed from accumulated true-positive and false-positive state.
thresholds nor top_k is supplied, the default classification threshold is 0.5.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.
Binary classification with probability outputs.
Models that emit raw logits.
Multiclass classification where correct labels should appear among the best predictions.
Monitoring one category in a multiclass model.
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.
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.
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.Recallkeras.metrics.TruePositiveskeras.metrics.FalsePositiveskeras.metrics.PrecisionAtRecallkeras.metrics.AUC