Repository: scikit-learn/scikit-learn
File: sklearn/metrics/tests/test_common.py (Python)

Code before:
        a_np=y_true_np,
        b_np=y_pred_np,
        sample_weight=sample_weight,
        **metric_kwargs,
    )


def check_array_api_multiclass_classification_metric(
    metric, array_namespace, device, dtype_name
):
    y_true_np = np.array([0, 1, 2, 3])
    y_pred_np = np.array([0, 1, 0, 2])

    if metric.__name__ == "average_precision_score":
        # we need y_pred_nd to be of shape (n_samples, n_classes
        y_pred_np = np.array(
            [
                [0.7, 0.2, 0.05, 0.05],
                [0.1, 0.8, 0.05, 0.05],
                [0.1, 0.1, 0.7, 0.1],
                [0.05, 0.05, 0.1, 0.8],
            ],
            dtype=dtype_name,
        )

    additional_params = {
        "average": ("micro", "macro", "weighted"),
        "beta": (0.2, 0.5, 0.8),
        "adjusted": (False, True),
    }
    metric_kwargs_combinations = _get_metric_kwargs_for_array_api_testing(
        metric=metric,
        params=additional_params,
    )
    for metric_kwargs in metric_kwargs_combinations:
        check_array_api_metric(
            metric,
            array_namespace,
            device,
            dtype_name,
            a_np=y_true_np,
            b_np=y_pred_np,
            sample_weight=None,
            **metric_kwargs,
        )

        sample_weight = np.array([0.0, 0.1, 2.0, 1.0], dtype=dtype_name)

        check_array_api_metric(
            metric,
            array_namespace,

Review comment (suggestion):
This case handling needs to be added, because `average_precision_score` handels multiclass inputs by binarising it. As a consequence, y_score needs to be of shape `(n_samples, n_classes)`. I have made a PR with a fix here: #32912.

Code after:
        dtype_name,
        a_np=y_true_np,
        b_np=y_pred_np,
        sample_weight=sample_weight,
        **metric_kwargs,
    )


def check_array_api_multiclass_classification_metric(
    metric, array_namespace, device, dtype_name
):
    y_true_np = np.array([0, 1, 2, 3])
    y_pred_np = np.array([0, 1, 0, 2])

    if metric.__name__ == "average_precision_score":
        # we need y_pred_nd to be of shape (n_samples, n_classes)
        y_pred_np = np.array(
            [
                [0.7, 0.2, 0.05, 0.05],
                [0.1, 0.8, 0.05, 0.05],
                [0.1, 0.1, 0.7, 0.1],
                [0.05, 0.05, 0.1, 0.8],
            ],
            dtype=dtype_name,
        )

    additional_params = {
        "average": ("micro", "macro", "weighted"),
        "beta": (0.2, 0.5, 0.8),
        "adjusted": (False, True),
    }
    metric_kwargs_combinations = _get_metric_kwargs_for_array_api_testing(
        metric=metric,
        params=additional_params,
    )
    for metric_kwargs in metric_kwargs_combinations:
        check_array_api_metric(
            metric,
            array_namespace,
            device,
            dtype_name,
            a_np=y_true_np,
            b_np=y_pred_np,
            sample_weight=None,
            **metric_kwargs,
        )

        sample_weight = np.array([0.0, 0.1, 2.0, 1.0], dtype=dtype_name)

        check_array_api_metric(
            metric,