Repository: scikit-learn/scikit-learn
File: sklearn/preprocessing/_label.py (Python)

Code before:
                return sp.csr_matrix((n_samples, 1), dtype=int)
            else:
                Y = xp.zeros((n_samples, 1), dtype=dtype_)
                Y += neg_label
                return Y
        elif n_classes >= 3:
            y_type = "multiclass"

    sorted_class = xp.sort(classes)
    if y_type == "multilabel-indicator":
        y_n_classes = y.shape[1] if hasattr(y, "shape") else len(y[0])
        if n_classes != y_n_classes:
            raise ValueError(
                "classes {0} mismatch with the labels {1} found in the data".format(
                    classes, unique_labels(y)
                )
            )

    if y_type in ("binary", "multiclass"):
        y = column_or_1d(y)

        # pick out the known labels from y
        y_in_classes = _isin(y, classes, xp=xp)
        y_seen = y[y_in_classes]
        indices = xp.searchsorted(sorted_class, y_seen)
        # cast `y_in_classes`` to integer dtype for `xp.cumulative_sum`
        y_in_classes = xp.astype(y_in_classes, int_dtype_)
        indptr = xp.concat(
            (
                xp.asarray([0], device=device_),
                xp.cumulative_sum(y_in_classes, axis=0),
            )
        )
        data = xp.full_like(indices, pos_label)

        # Fall back to NumPy namespace to construct the sparse matrix of one-hot labels
        Y = sp.csr_matrix(
            (
                _convert_to_numpy(data, xp=xp),
                _convert_to_numpy(indices, xp=xp),
                _convert_to_numpy(indptr, xp=xp),
            ),
            shape=(n_samples, n_classes),
        )

        if not sparse_output:
            Y = xp.asarray(Y.toarray(), device=device_)

    elif y_type == "multilabel-indicator":
        if sparse_output:
            Y = sp.csr_matrix(y)

Review comment (suggestion):
```suggestion
        # cast `y_in_classes` to integer dtype for `xp.cumulative_sum`
```

Code after:
                return sp.csr_matrix((n_samples, 1), dtype=int)
            else:
                Y = xp.zeros((n_samples, 1), dtype=int_dtype_)
                Y += neg_label
                return Y
        elif n_classes >= 3:
            y_type = "multiclass"

    sorted_class = xp.sort(classes)
    if y_type == "multilabel-indicator":
        y_n_classes = y.shape[1] if hasattr(y, "shape") else len(y[0])
        if n_classes != y_n_classes:
            raise ValueError(
                "classes {0} mismatch with the labels {1} found in the data".format(
                    classes, unique_labels(y)
                )
            )

    if y_type in ("binary", "multiclass"):
        y = column_or_1d(y)

        # pick out the known labels from y
        y_in_classes = _isin(y, classes, xp=xp)
        y_seen = y[y_in_classes]
        indices = xp.searchsorted(sorted_class, y_seen)
        # cast `y_in_classes` to integer dtype for `xp.cumulative_sum`
        y_in_classes = xp.astype(y_in_classes, int_dtype_)
        indptr = xp.concat(
            (
                xp.asarray([0], device=device_),
                xp.cumulative_sum(y_in_classes, axis=0),
            )
        )
        data = xp.full_like(indices, pos_label)

        # Use NumPy to construct the sparse matrix of one-hot labels
        Y = sp.csr_matrix(
            (
                _convert_to_numpy(data, xp=xp),
                _convert_to_numpy(indices, xp=xp),
                _convert_to_numpy(indptr, xp=xp),
            ),
            shape=(n_samples, n_classes),
        )

        if not sparse_output:
            Y = xp.asarray(Y.toarray(), device=device_)

    elif y_type == "multilabel-indicator":
        if sparse_output:
            Y = sp.csr_matrix(y)