optimize_metric_threshold#

class imbal.classification.optimize_metric_threshold(predictions, labels, metric, sample_weight=None, maximize=True, threshold_range=(0, 1), step_size=0.1)#

Bases:

Finds the threshold at which a particular metric is optimized.

Parameters:
  • predictions – A NumPy array of predictions confidences.

  • labels – A NumPy array of data labels. Should have the same shape as predictions.

  • metric – A keras.metrics.Metric instance, or a function of the form f(predictions, labels, sample_weight=None). Note: If a Metric object with a pre-specified threshold is provided, the function will still work as intended. Prediction values are clamped to 0/1 based on the thresholds being tested before the metric value is calculated.

  • sample_weight – Optional, default None. Sample weights used to weight the metric calculation.

  • maximize – Optional, default True. Whether to maximize the metric. Should be set to False if your goal is to minimize the metric.

  • threshold_range – Optional, default (0, 1). A tuple containing the minimum and maximum value threshold values to test.

  • step_size – Optional, default 0.1. The step size between tested threshold values.

Returns:

The threshold at which the provided metric is optimized.

Example:

>>> from imbal.classification import optimize_metric_threshold
>>> import numpy as np
>>> import keras

>>> labels = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]).reshape(-1, 1)
>>> predictions = np.array([0.5, 0.2, 0.7, 0.4, 0.1, 0.3, 0.8, 0.9, 0.8, 0.6]).reshape(-1, 1)
>>> metric = keras.metrics.F1Score(threshold=0.5)

>>> best_threshold = optimize_metric_threshold(
>>>     predictions,
>>>     labels,
>>>     metric
>>> )

>>> print(best_threshold)
0.7