torch_ecg.utils.cls_to_bin

torch_ecg.utils.cls_to_bin(cls_array: ndarray | Tensor, num_classes: int | None = None) ndarray[source]

Convert a categorical array to a one-hot array.

Convert a categorical (class indices) array of shape (n,) to a one-hot (binary) array of shape (n, num_classes).

Parameters:
  • cls_array (numpy.ndarray or torch.Tensor) – Class indices array (tensor) of shape (num_samples,); or of shape (num_samples, num_samples) if num_classes is not None, in which case cls_array should be consistant with num_classes, and the function will return cls_array directly.

  • num_classes (int, optional) – Number of classes. If not specified, it will be inferred from the values of cls_array.

Returns:

Binary array of shape (num_samples, num_classes).

Return type:

numpy.ndarray

Examples

>>> cls_array = torch.randint(0, 26, size=(1000,))
>>> bin_array = cls_to_bin(cls_array)
>>> cls_array = np.random.randint(0, 26, size=(1000,))
>>> bin_array = cls_to_bin(cls_array)