to_class_indices#

sdm.evaluation.to_class_indices(pred: TableTensor, target: TableTensor | CategoricalTensor, *, missing_score: float | None = None) → tuple[Tensor, Tensor]#

Convert classification predictions and targets to class-index form.

Parameters:
  • pred (TableTensor) – Prediction table whose numerical columns contain class scores. Column names define the class order.

  • target (TableTensor | CategoricalTensor) – Single-column categorical target.

  • missing_score (float | None) – Score assigned to target classes that are absent from the prediction columns. If None, will raise an error when a target value refers to an absent class in pred.

Return type:

tuple[Tensor, Tensor]

>>> import torch
>>> import sdm
>>> from sdm.evaluation import to_class_indices
>>> pred = sdm.TableTensor.from_tensor(
...     torch.tensor([[0.8, 0.1, 0.1], [0.1, 0.2, 0.7]]),
...     columns=["cat", "dog", "bird"],
... )
>>> target = sdm.CategoricalTensor(
...     code=torch.tensor([[0], [2]]),
...     categories=(sdm.StringTensor.from_list(["cat", "dog", "bird"]),),
... )
>>> class_scores, class_indices = to_class_indices(pred, target)
>>> class_scores
tensor([[0.8000, 0.1000, 0.1000],
        [0.1000, 0.2000, 0.7000]])
>>> class_indices
tensor([0, 2])
>>> pred = sdm.TableTensor.from_tensor(
...     torch.tensor([[0.1, 0.8, 0.1], [0.7, 0.1, 0.2]]),
...     columns=["bird", "cat", "dog"],
... )
>>> class_scores, class_indices = to_class_indices(pred, target)
>>> class_scores
tensor([[0.8000, 0.1000, 0.1000],
        [0.1000, 0.2000, 0.7000]])
>>> class_indices
tensor([0, 2])