Skip to content

Remove median_absolute_error from METRICS_WITHOUT_SAMPLE_WEIGHT #30787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sklearn/metrics/_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,9 +907,9 @@ def median_absolute_error(
>>> median_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7])
0.85
"""
y_type, y_true, y_pred, multioutput = _check_reg_targets(
y_true, y_pred, multioutput
)
_, y_true, y_pred, multioutput = _check_reg_targets(y_true, y_pred, multioutput)
check_consistent_length(y_true, y_pred, sample_weight)

if sample_weight is None:
output_errors = np.median(np.abs(y_pred - y_true), axis=0)
else:
Expand Down
12 changes: 7 additions & 5 deletions sklearn/metrics/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,6 @@ def precision_recall_curve_padded_thresholds(*args, **kwargs):

# No Sample weight support
METRICS_WITHOUT_SAMPLE_WEIGHT = {
"median_absolute_error",
"max_error",
"ovo_roc_auc",
"weighted_ovo_roc_auc",
Expand Down Expand Up @@ -1445,9 +1444,10 @@ def test_averaging_multilabel_all_ones(name):
check_averaging(name, y_true, y_true_binarize, y_pred, y_pred_binarize, y_score)


def check_sample_weight_invariance(name, metric, y1, y2):
def check_sample_weight_invariance(name, metric, y1, y2, sample_weight=None):
rng = np.random.RandomState(0)
sample_weight = rng.randint(1, 10, size=len(y1))
if sample_weight is None:
sample_weight = rng.randint(1, 10, size=len(y1))

# top_k_accuracy_score always lead to a perfect score for k > 1 in the
# binary case
Expand Down Expand Up @@ -1550,13 +1550,15 @@ def check_sample_weight_invariance(name, metric, y1, y2):
),
)
def test_regression_sample_weight_invariance(name):
n_samples = 50
n_samples = 51
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cannot be an even number due to #17370, inspired by @ogrisel last paragraph in #17370 (comment), even though I agree it's a temporary fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since #29907 was merged, it should be possible to update median_absolute_error to handle even number of data points (with or without weights) as explained below.

random_state = check_random_state(0)
# regression
y_true = random_state.random_sample(size=(n_samples,))
y_pred = random_state.random_sample(size=(n_samples,))
sample_weight = np.arange(len(y_true))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as suggested in #30787 (comment). This works great.

I've amended check_sample_weight_invariance to optionally take sample_weight as I didn't want to change the sample_weight for all tests that use check_sample_weight_invariance, WDYT @glemaitre ?

metric = ALL_METRICS[name]
check_sample_weight_invariance(name, metric, y_true, y_pred)

check_sample_weight_invariance(name, metric, y_true, y_pred, sample_weight)


@pytest.mark.parametrize(
Expand Down
Loading