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

Code before:
            except TypeError:
                if delete_labels:
                    del score_params["labels"]
                if delete_pos_label:
                    del score_params["pos_label"]
                # TODO: How about issuing a warning?
                # The call to scoring will fail if y_test does not contain all
                # class labels.
        elif is_binary and "labels" in sig and "pos_label" in sig:
            # This seems like a hack and indeed it is one.
            scoring = make_scorer(
                scoring._score_func,
                greater_is_better=True if scoring._sign == 1 else False,
                response_method=scoring._response_method,
                labels=classes,
                pos_label=classes[-1],  # see _logistic_regression_path
                **getattr(scoring, "_kwargs", {}),
            )
        elif len(classes) >= 3 and "labels" in sig:
            scoring = make_scorer(
                scoring._score_func,
                greater_is_better=True if scoring._sign == 1 else False,
                response_method=scoring._response_method,
                labels=classes,
                **getattr(scoring, "_kwargs", {}),
            )

        def calc_score(log_reg):
            return scoring(log_reg, X_test, y_test, **score_params)

    for w, C in zip(coefs, Cs):
        log_reg.C = C
        if fit_intercept:
            log_reg.coef_ = w[..., :-1]
            log_reg.intercept_ = w[..., -1]
        else:
            log_reg.coef_ = w
            log_reg.intercept_ = 0.0

        scores.append(calc_score(log_reg))

    return coefs, Cs, np.array(scores), n_iter


class LogisticRegression(LinearClassifierMixin, SparseCoefMixin, BaseEstimator):
    """
    Logistic Regression (aka logit, MaxEnt) classifier.

    This class implements regularized logistic regression using a set of available
    solvers. **Note that regularization is applied by default**. It can handle both
    dense and sparse input `X`. Use C-ordered arrays or CSR matrices containing 64-bit

Review comment (suggestion):
```suggestion
        elif (
            (is_binary and "labels" in sig and "pos_label" in sig)
            or (len(classes) >= 3 and "labels" in sig):
        ):
            pos_label_kwarg = {}
            if is_binary:
                # see _logistic_regression_path
                pos_label_kwarg["pos_label"] = classes[-1]
            scoring = make_scorer(
                scoring._score_func,
                greater_is_better=True if scoring._sign == 1 else False,
                response_method=scoring._response_method,
                labels=classes,
                **pos_label_kwarg,
                **getattr(scoring, "_kwargs", {}),
            )
```

Code after:

    scores = list()

    # Prepare the call to get the score per fold: calc_score
    scoring = get_scorer(scoring)
    if scoring is None:

        def calc_score(log_reg):
            return log_reg.score(X_test, y_test, sample_weight=sw_test)

    else:
        is_binary = len(classes) <= 2
        score_params = score_params or {}
        score_params = _check_method_params(X=X, params=score_params, indices=test)
        # We need to pass the classes as "labels" argument to scorers that support
        # it, e.g. scoring = "neg_brier_score", because y_test may not contain all
        # class labels.
        # There are at least 2 possibilities:
        # 1. Metadata routing is enabled: A try except clause is possible with
        #   adding labels to score_params. We could then pass the already instantiated
        #   log_reg instance to scoring.
        # 2. We reconstruct the scorer and pass labels as kwargs explicitly.
        # We implement the 2nd option even if it seems a bit hacky because it works
        # with and without metadata routing.
        if hasattr(scoring, "_score_func"):
            sig = inspect.signature(scoring._score_func).parameters
        else:
            sig = []

        if (is_binary and "labels" in sig and "pos_label" in sig) or (
            len(classes) >= 3 and "labels" in sig
        ):
            pos_label_kwarg = {}
            if is_binary:
                # see _logistic_regression_path
                pos_label_kwarg["pos_label"] = classes[-1]
            scoring = make_scorer(
                scoring._score_func,
                greater_is_better=True if scoring._sign == 1 else False,
                response_method=scoring._response_method,
                labels=classes,
                **pos_label_kwarg,
                **getattr(scoring, "_kwargs", {}),
            )

        def calc_score(log_reg):
            return scoring(log_reg, X_test, y_test, **score_params)

    for w, C in zip(coefs, Cs):
        log_reg.C = C
        if fit_intercept: