one_hot#

metatensor.one_hot(labels: Labels, dimension: Labels)[source]#

Generates one-hot encoding from a Labels object.

This function takes two Labels objects as inputs. The first is the one to be converted to one-hot-encoded format, and the second contains the name of the label to be extracted and all possible values of the one-hot encoding.

Parameters:
  • labels (Labels) – A Labels object from which one label will be extracted and transformed into a one-hot-encoded array.

  • dimension (Labels) – A Labels object that contains a single dimension. The name of this label is the same that will be selected from labels, and its values correspond to all possible values that the label can take.

Returns:

A two-dimensional numpy.ndarray or torch.Tensor containing the one-hot encoding along the selected dimension: its first dimension matches the one in labels, while the second contains 1 at the position corresponding to the original label and 0 everywhere else

>>> import numpy as np
>>> import metatensor
>>> from metatensor import Labels
>>> # Let's say we have 6 atoms, whose chemical identities
>>> # are C, H, H, H, C, H:
>>> original_labels = Labels(
...     names=["atom", "types"],
...     values=np.array([[0, 6], [1, 1], [2, 1], [3, 1], [4, 6], [5, 1]]),
... )
>>> # Set up a Labels object with all possible elements,
>>> # including, for example, also O:
>>> possible_labels = Labels(names=["types"], values=np.array([[1], [6], [8]]))
>>> # Get the one-hot encoded labels:
>>> one_hot_encoding = metatensor.one_hot(original_labels, possible_labels)
>>> print(one_hot_encoding)
[[0 1 0]
 [1 0 0]
 [1 0 0]
 [1 0 0]
 [0 1 0]
 [1 0 0]]