You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Metrics which are degenerate with a single sample should raise an error if passed a single sample #11435
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?
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.
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
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
While I appreciate that these are about central tendency, I expect users
would apply MSE, MAE, etc in the context of leave-one-out cross validation,
to just measure squared or absolute error. It would be inappropriate to
break their usage for the sake of theory, so please let's be more careful
about this.
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.
Activity
adrinjalali commentedon Dec 13, 2018
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.
amueller commentedon Dec 13, 2018
+1 for nan & warning.
psendyk commentedon Dec 22, 2018
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.
FIX r2_score behavior with a single sample (#11435) (#12855)
FIX r2_score behavior with a single sample (scikit-learn#11435) (scik…
Revert "FIX r2_score behavior with a single sample (scikit-learn#11435)…
Revert "FIX r2_score behavior with a single sample (scikit-learn#11435)…
FIX r2_score behavior with a single sample (scikit-learn#11435) (scik…
amy12xx commentedon Jun 20, 2020
@psendyk @adrinjalali Do you know of other metrics that still need this fix? metrics.explained_variance_score maybe?
adrinjalali commentedon Jun 22, 2020
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
amy12xx commentedon Jun 23, 2020
I'll take a look at it
amy12xx commentedon Jun 28, 2020
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:
jnothman commentedon Jun 28, 2020
amy12xx commentedon Jul 7, 2020
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.
kurchi1205 commentedon Aug 20, 2021
I want to make my first contribution , Is there any other metric which needs to be fixed ?
lorentzenchr commentedon Mar 16, 2026
Metrics (ignoring curves) that need more than a single sample are (no guarantee for completeness).
Classification:
auccohen_kappa_scored2_brier_scored2_log_loss_scorejaccard_score(not 100% sure)matthews_corrcoef(not 100% sure)roc_auc_scoreMultilabel:
dcg_scorendcg_scorecoverage_errorlabel_ranking_average_precision_scorelabel_ranking_lossRegression:
d2_absolute_error_scored2_pinball_scored2_tweedie_scoreexplained_variance_scorer2_scoreClustering:
TODO