Skip to content

Metrics which are degenerate with a single sample should raise an error if passed a single sample #11435

Open
0 of 1 issue completed

Description

@jnothman

A metric such as r2_score requires comparing the distributions of the true and predicted values. With a single sample it either returns 1 for equality or 0 for inequality (which already is a bit weird given that it's a metric over continuous variables).

Instead, we should return NaN or issue a warning or raise an error. We should identify other metrics where this would also be appropriate behaviour.

Do others think this is the correct course of action?

Sub-issues

Sub-issues

0 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. adrinjalali commented on Dec 13, 2018

    @adrinjalali
    Member

    I'd say to return NaN and raise a warning. I wouldn't raise an error, just in case only one of the folds turns out to have only one sample and others are fine.

    The NaN + warning should give the user enough info on how to proceed I suppose.

  2. amueller commented on Dec 13, 2018

    @amueller
    Member

    +1 for nan & warning.

  3. added
    Easy
    on Dec 13, 2018
  4. psendyk commented on Dec 22, 2018

    @psendyk
    Contributor

    I opened a PR as you can see. If that's the desired behavior I can go over other metrics that need the same fix.

  5. amy12xx commented on Jun 20, 2020

    @amy12xx
    Contributor

    @psendyk @adrinjalali Do you know of other metrics that still need this fix? metrics.explained_variance_score maybe?

  6. adrinjalali commented on Jun 22, 2020

    @adrinjalali
    Member

    Not off the top of my head, but I'd say figuring out which metrics would need it would be a nice place to start and contribute on this issue if you're up for it @amy12xx

  7. amy12xx commented on Jun 23, 2020

    @amy12xx
    Contributor

    I'll take a look at it

  8. amy12xx commented on Jun 28, 2020

    @amy12xx
    Contributor

    I looked the regression metrics, and it looks like all of them need this fix, since all are based on central tendency measures (mean / median / variance). metrics.max_error could potentially be a case that might not need this.

    Here's the current behavior on an example:

    >>> from sklearn.metrics import *
    >>> y_true = [3]
    >>> y_pred = [2.5]
    >>> r2_score(y_true, y_pred)
    \scikit-learn\sklearn\metrics\_regression.py:607: UndefinedMetricWarning: R^2 score is not well-defined with less than two samples.
      warnings.warn(msg, UndefinedMetricWarning)
    nan
    >>> explained_variance_score(y_true, y_pred)
    1.0
    >>> max_error(y_true, y_pred)
    0.5
    >>> mean_absolute_error(y_true, y_pred)
    0.5
    >>> mean_squared_error(y_true, y_pred)
    0.25
    >>> mean_squared_error(y_true, y_pred, squared=True)
    0.25
    >>> mean_squared_error(y_true, y_pred, squared=False)
    0.5
    >>> mean_squared_log_error(y_true, y_pred)
    0.017830632816244387
    >>> median_absolute_error(y_true, y_pred)
    0.5
    >>> mean_poisson_deviance(y_true, y_pred)
    0.09392934076372761
    >>> mean_gamma_deviance(y_true, y_pred)
    0.03535688641209056
    
  9. jnothman commented on Jun 28, 2020

    @jnothman
    MemberAuthor
  10. amy12xx commented on Jul 7, 2020

    @amy12xx
    Contributor

    Ok, that makes sense. I did not pay attention to the LOO part. I think I'll leave it alone for now, since I don't really understand the criteria to fix.

  11. kurchi1205 commented on Aug 20, 2021

    @kurchi1205

    I want to make my first contribution , Is there any other metric which needs to be fixed ?

  12. added and removed
    Easy
    on Jan 17, 2022
  13. lorentzenchr commented on Mar 16, 2026

    @lorentzenchr
    Member

    Metrics (ignoring curves) that need more than a single sample are (no guarantee for completeness).
    Classification:

    • auc
    • cohen_kappa_score
    • d2_brier_score
    • d2_log_loss_score
    • jaccard_score (not 100% sure)
    • matthews_corrcoef (not 100% sure)
    • roc_auc_score

    Multilabel:

    • dcg_score
    • ndcg_score
    • coverage_error
    • label_ranking_average_precision_score
    • label_ranking_loss

    Regression:

    • d2_absolute_error_score
    • d2_pinball_score
    • d2_tweedie_score
    • explained_variance_score
    • r2_score

    Clustering:
    TODO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions