MCPcopy Create free account
hub / github.com/PythonOT/POT / labels_to_masks

Function labels_to_masks

ot/utils.py:530–554  ·  view source on GitHub ↗

r"""Transforms (n_samples,) vector of labels into a (n_samples, n_labels) matrix of masks. Parameters ---------- y : array-like, shape (n_samples, ) The vector of labels. type_as : array_like Array of the same type of the expected output. nx : Backend, optional

(y, type_as=None, nx=None)

Source from the content-addressed store, hash-verified

528
529
530def labels_to_masks(y, type_as=None, nx=None):
531 r"""Transforms (n_samples,) vector of labels into a (n_samples, n_labels) matrix of masks.
532
533 Parameters
534 ----------
535 y : array-like, shape (n_samples, )
536 The vector of labels.
537 type_as : array_like
538 Array of the same type of the expected output.
539 nx : Backend, optional
540 Backend to perform computations on. If omitted, the backend defaults to that of `y`.
541
542 Returns
543 -------
544 masks : array-like, shape (n_samples, n_labels)
545 The (n_samples, n_labels) matrix of label masks.
546 """
547 if nx is None:
548 nx = get_backend(y)
549 if type_as is None:
550 type_as = y
551 labels_u, labels_idx = nx.unique(y, return_inverse=True)
552 n_labels = labels_u.shape[0]
553 masks = nx.eye(n_labels, type_as=type_as)[labels_idx]
554 return masks
555
556
557def parmap(f, X, nprocs="default"):

Callers 2

transform_labelsMethod · 0.85

Calls 3

get_backendFunction · 0.85
uniqueMethod · 0.45
eyeMethod · 0.45

Tested by

no test coverage detected