autointent.modules.decision.ThresholdDecision#

class autointent.modules.decision.ThresholdDecision(thresh=0.5)#

Bases: autointent.modules.base.BaseDecision

Threshold predictor module.

ThresholdDecision uses a predefined threshold (or array of thresholds) to predict labels for single-label or multi-label classification tasks.

Parameters:

thresh (autointent.custom_types.FloatFromZeroToOne | list[autointent.custom_types.FloatFromZeroToOne]) – Threshold for the scores, shape (n_classes,) or float

Examples:#

Single-label classification#

from autointent.modules import ThresholdDecision
import numpy as np
scores = np.array([[0.2, 0.8], [0.6, 0.4], [0.1, 0.9]])
labels = [1, 0, 1]
threshold = 0.5
predictor = ThresholdDecision(thresh=threshold)
predictor.fit(scores, labels)
test_scores = np.array([[0.3, 0.7], [0.5, 0.5]])
predictions = predictor.predict(test_scores)
print(predictions)
[1, 0]

Multi-label classification#

labels = [[1, 0], [0, 1], [1, 1]]
predictor = ThresholdDecision(thresh=[0.5, 0.5])
predictor.fit(scores, labels)
test_scores = np.array([[0.3, 0.7], [0.6, 0.4]])
predictions = predictor.predict(test_scores)
print(predictions)
[[0, 1], [1, 0]]
tags: list[autointent.schemas.Tag] | None#
name = 'threshold'#

Name of the module.

supports_oos = True#

Whether the module supports oos data

supports_multilabel = True#

Whether the module supports multilabel classification

supports_multiclass = True#

Whether the module supports multiclass classification

thresh = 0.5#
classmethod from_context(context, thresh=0.5)#

Initialize from context.

Parameters:
  • context (autointent.Context) – Context containing configurations and utilities

  • thresh (autointent.custom_types.FloatFromZeroToOne | list[autointent.custom_types.FloatFromZeroToOne]) – Threshold for classification

Return type:

ThresholdDecision

fit(scores, labels, tags=None)#

Fit the model.

Parameters:
  • scores (numpy.typing.NDArray[Any]) – Array of shape (n_samples, n_classes) with predicted scores

  • labels (autointent.custom_types.ListOfGenericLabels) – List of true labels

  • tags (list[autointent.schemas.Tag] | None) – List of Tag objects for mutually exclusive classes, or None

Raises:

MismatchNumClassesError – If number of thresholds doesn’t match number of classes

Return type:

None

predict(scores)#

Predict labels using thresholds.

Parameters:

scores (numpy.typing.NDArray[Any]) – Array of shape (n_samples, n_classes) with predicted scores

Returns:

Predicted labels (either single-label or multi-label)

Raises:

MismatchNumClassesError – If number of classes in scores doesn’t match training data

Return type:

autointent.custom_types.ListOfGenericLabels