threshold_optimizer package

Submodules

threshold_optimizer.threshold_optimizer module

class threshold_optimizer.threshold_optimizer.ThresholdOptimizer(y_score: Union[numpy.ndarray, pandas.core.series.Series, list], y_true: Union[numpy.ndarray, pandas.core.series.Series, list], search_space_size: int = 100)

Bases: object

convert_classes(threshold: float) → numpy.ndarray

Convert predicted probabilities into binary classes based on a threshold/cutoff value

Parameters

threshold – The probability threshold value to determine predicted classes. This follows a greater than or equal to format for determining class 1

Returns: 1 dimensional numpy array of classes

get_best_accuracy_metrics(verbose: bool = True) → Tuple[int, int]

Optimizes threshold for accuracy

Returns: best accuracy score and threshold at which best accuracy score occurs

Parameters

verbose – Optional. Option of whether to output results of optimization

get_best_f1_metrics(verbose: bool = True) → Tuple[int, int]

Optimizes threshold for F1 score

Returns: best F1 score and threshold at which best F1 score occurs

Parameters

verbose – Optional. Option of whether to output results of optimization

get_best_precision_metrics(verbose: bool = True) → Tuple[int, int]

Optimizes threshold for precision

Returns: best precision score and threshold at which best precision score occurs

Parameters

verbose – Optional. Option of whether to output results of optimization

get_best_recall_metrics(verbose: bool = True) → Tuple[int, int]

Optimizes threshold for recall

Returns: best recall score and threshold at which best recall score occurs

Parameters

verbose – Optional. Option of whether to output results of optimization

get_best_sensitivity_metrics(verbose: bool = True) → Tuple[int, int]

Optimizes threshold for sensitivity score

Returns: best sensitivity score and threshold at which best sensitivity score occurs

Parameters

verbose – Optional. Option of whether to output results of optimization

get_best_specificity_metrics(verbose: bool = True) → Tuple[int, int]

Optimizes threshold for specificity

Returns: best specificity score and threshold at which best specificity score occurs

Parameters

verbose – Optional. Option of whether to output results of optimization

optimize_metrics(metrics: list = None, verbose: bool = True)

Function to optimize for supported metrics in a batch format

Parameters
  • verbose – Optional. Option of whether to output results of optimization

  • metrics – Optional. Should be specified if only specific supported metrics are to be optimized. input must be a subset one of the supported metrics. If no metrics are applied, all metrics will be optimized for.

set_search_space(search_space_size: int)

set the number of possible probability threshold values to optimze for

This function is useful to reset the size of the search space after initializing the ThresholdOptimizer object.

Parameters

search_space_size – The number of possible probability threshold values to optimze for

Module contents