mpu.ml

Machine Learning functions.

mpu.ml.indices2one_hot(indices: Iterable, nb_classes: int) List[source]

Convert an iterable of indices to one-hot encoded list.

You might also be interested in sklearn.preprocessing.OneHotEncoder

Parameters
  • indices (Iterable) – iterable of indices

  • nb_classes (int) – Number of classes

Returns

one_hot

Return type

List

Examples

>>> indices2one_hot([0, 1, 1], 3)
[[1, 0, 0], [0, 1, 0], [0, 1, 0]]
>>> indices2one_hot([0, 1, 1], 2)
[[1, 0], [0, 1], [0, 1]]
mpu.ml.one_hot2indices(one_hots: List) List[source]

Convert an iterable of one-hot encoded targets to a list of indices.

Parameters

one_hots (List) –

Returns

indices

Return type

List

Examples

>>> one_hot2indices([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
[0, 1, 2]
>>> one_hot2indices([[1, 0], [1, 0], [0, 1]])
[0, 0, 1]