Repository: scikit-learn/scikit-learn
File: sklearn/linear_model/_ridge.py (Python)

Code before:
        sample_weight : ndarray of shape (n_samples,)
            Validated sample weights.

        Y : ndarray of shape (n_samples, n_classes)
            The binarized version of `y`.
        """
        accept_sparse = _get_valid_accept_sparse(sparse.issparse(X), solver)
        sample_weight = ensure_common_namespace_device(X, sample_weight)[0]
        original_X = X
        X, y = validate_data(
            self,
            X,
            y,
            accept_sparse=accept_sparse,
            multi_output=True,
            y_numeric=False,
            force_writeable=True,
        )

        self._label_binarizer = LabelBinarizer(pos_label=1, neg_label=-1)
        y_xp, y_is_array_api = get_namespace(y)
        Y = self._label_binarizer.fit_transform(
            _convert_to_numpy(y, y_xp) if y_is_array_api else y
        )
        Y = ensure_common_namespace_device(original_X, Y)[0]
        if y_is_array_api and y_xp.isdtype(y.dtype, "numeric"):
            self.classes_ = ensure_common_namespace_device(
                original_X, self._label_binarizer.classes_
            )[0]
        else:
            self.classes_ = self._label_binarizer.classes_
        if not self._label_binarizer.y_type_.startswith("multilabel"):
            y = column_or_1d(y, warn=True)

        sample_weight = _check_sample_weight(sample_weight, X, dtype=X.dtype)
        if self.class_weight:
            reweighting = compute_sample_weight(self.class_weight, y)
            reweighting = ensure_common_namespace_device(original_X, reweighting)[0]
            sample_weight = sample_weight * reweighting
        return X, y, sample_weight, Y

    def predict(self, X):
        """Predict class labels for samples in `X`.

        Parameters
        ----------
        X : {array-like, spare matrix} of shape (n_samples, n_features)
            The data matrix for which we want to predict the targets.

        Returns
        -------

Review comment (suggestion):
I find it more natural to name `xp_y` the namespace of `y`. In the tests, `y_xp` stands for `y` moved to a namespace `xp` instead.

```suggestion
        xp_y, y_is_array_api = get_namespace(y)
        # TODO: Update this line to avoid calling `_convert_to_numpy`
        # once LabelBinarizer has been updated to accept non-NumPy array API
        # compatible inputs.
        Y = self._label_binarizer.fit_transform(
            _convert_to_numpy(y, xp_y) if y_is_array_api else y
        )
        Y = ensure_common_namespace_device(original_X, Y)[0]
        if y_is_array_api and xp_y.isdtype(y.dtype, "numeric"):
```

Code after:
        sample_weight : ndarray of shape (n_samples,)
            Validated sample weights.

        Y : ndarray of shape (n_samples, n_classes)
            The binarized version of `y`.
        """
        accept_sparse = _get_valid_accept_sparse(sparse.issparse(X), solver)
        sample_weight = ensure_common_namespace_device(X, sample_weight)[0]
        original_X = X
        X, y = validate_data(
            self,
            X,
            y,
            accept_sparse=accept_sparse,
            multi_output=True,
            y_numeric=False,
            force_writeable=True,
        )

        self._label_binarizer = LabelBinarizer(pos_label=1, neg_label=-1)
        xp_y, y_is_array_api = get_namespace(y)
        # TODO: Update this line to avoid calling `_convert_to_numpy`
        # once LabelBinarizer has been updated to accept non-NumPy array API
        # compatible inputs.
        Y = self._label_binarizer.fit_transform(
            _convert_to_numpy(y, xp_y) if y_is_array_api else y
        )
        Y = ensure_common_namespace_device(original_X, Y)[0]
        if y_is_array_api and xp_y.isdtype(y.dtype, "numeric"):
            self.classes_ = ensure_common_namespace_device(
                original_X, self._label_binarizer.classes_
            )[0]
        else:
            self.classes_ = self._label_binarizer.classes_
        if not self._label_binarizer.y_type_.startswith("multilabel"):
            y = column_or_1d(y, warn=True)

        sample_weight = _check_sample_weight(sample_weight, X, dtype=X.dtype)
        if self.class_weight:
            reweighting = compute_sample_weight(self.class_weight, y)
            reweighting = ensure_common_namespace_device(original_X, reweighting)[0]
            sample_weight = sample_weight * reweighting
        return X, y, sample_weight, Y

    def predict(self, X):
        """Predict class labels for samples in `X`.

        Parameters
        ----------
        X : {array-like, spare matrix} of shape (n_samples, n_features)
            The data matrix for which we want to predict the targets.