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)
| 528 | |
| 529 | |
| 530 | def 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 | |
| 557 | def parmap(f, X, nprocs="default"): |
no test coverage detected