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

Code before:

    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
        -------
        y_pred : ndarray of shape (n_samples,) or (n_samples, n_outputs)
            Vector or matrix containing the predictions. In binary and
            multiclass problems, this is a vector containing `n_samples`. In
            a multilabel problem, it returns a matrix of shape
            `(n_samples, n_outputs)`.
        """
        check_is_fitted(self, attributes=["_label_binarizer"])
        if self._label_binarizer.y_type_.startswith("multilabel"):
            # Threshold such that the negative label is -1 and positive label
            # is 1 to use the inverse transform of the label binarizer fitted
            # during fit.
            decision = self.decision_function(X)
            xp, is_array_api, device_ = get_namespace_and_device(decision)
            max_float_dtype = _max_precision_float_dtype(xp, device=device_)
            scores = 2.0 * xp.astype(decision > 0, max_float_dtype) - 1.0
            if is_array_api:
                scores = _convert_to_numpy(scores, xp)
            return self._label_binarizer.inverse_transform(scores)
        return super().predict(X)

    def __sklearn_tags__(self):
        tags = super().__sklearn_tags__()
        tags.classifier_tags.multi_label = True
        return tags

    def _get_scorer_instance(self):
        """Return a scorer which corresponds to what's defined in ClassiferMixin
        parent class. This is used for routing `sample_weight`.
        """
        return get_scorer("accuracy")


class RidgeClassifier(_RidgeClassifierMixin, _BaseRidge):
    """Classifier using Ridge regression.

    This classifier first converts the target values into ``{-1, 1}`` and
    then treats the problem as a regression task (multi-output regression in
    the multiclass case).

    Read more in the :ref:`User Guide <ridge_regression>`.

Review comment (suggestion):
The `xp.astype(decision > 0, max_float_dtype)` was previously hardcoded to 
`xp.astype(decision > 0, xp.float32)` but I decided to use the `max_float_dtype` instead. Let me know if we should instead revert back to `xp.float32`

Code after:
            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
        -------
        y_pred : ndarray of shape (n_samples,) or (n_samples, n_outputs)
            Vector or matrix containing the predictions. In binary and
            multiclass problems, this is a vector containing `n_samples`. In
            a multilabel problem, it returns a matrix of shape
            `(n_samples, n_outputs)`.
        """
        check_is_fitted(self, attributes=["_label_binarizer"])
        if self._label_binarizer.y_type_.startswith("multilabel"):
            # Threshold such that the negative label is -1 and positive label
            # is 1 to use the inverse transform of the label binarizer fitted
            # during fit.
            decision = self.decision_function(X)
            xp, is_array_api = get_namespace(decision)
            scores = 2.0 * xp.astype(decision > 0, decision.dtype) - 1.0
            if is_array_api:
                scores = _convert_to_numpy(scores, xp)
            return self._label_binarizer.inverse_transform(scores)
        return super().predict(X)

    def __sklearn_tags__(self):
        tags = super().__sklearn_tags__()
        tags.classifier_tags.multi_label = True
        return tags

    def _get_scorer_instance(self):
        """Return a scorer which corresponds to what's defined in ClassiferMixin
        parent class. This is used for routing `sample_weight`.
        """
        return get_scorer("accuracy")


class RidgeClassifier(_RidgeClassifierMixin, _BaseRidge):
    """Classifier using Ridge regression.

    This classifier first converts the target values into ``{-1, 1}`` and
    then treats the problem as a regression task (multi-output regression in
    the multiclass case).