Skip to content

Fix r2_score behavior with a single sample (#11435)#12855

Merged
jnothman merged 15 commits intoscikit-learn:masterfrom
psendyk:r2_score
Jan 8, 2019
Merged

Fix r2_score behavior with a single sample (#11435)#12855
jnothman merged 15 commits intoscikit-learn:masterfrom
psendyk:r2_score

Conversation

@psendyk
Copy link
Copy Markdown
Contributor

@psendyk psendyk commented Dec 22, 2018

Reference Issues/PRs

#11435 partially (see other comments).

What does this implement/fix? Explain your changes.

It fixes the behavior of r2_score when passed single samples. As explained in #11435, it raises a warning and returns a NaN value.

Any other comments?

There are more metrics which are degenerate with a single sample and need that fix.

check_consistent_length(y_true, y_pred, sample_weight)

if _num_samples(y_pred) < 2:
msg = "Found an array of {0} sample(s), while the minimum number is 2."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or maybe "R^2 score is not well-defined with less than two samples!", not sure though.

@adrinjalali
Copy link
Copy Markdown
Member

Please also add a test and check if the correct warning is raised and the expected nan value is returned.

warning_msg = "not well-defined with less than two samples."

# Check all metrics which are degenerate when passed a single sample.
for metric in [r2_score]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you can give this to the function as a parameter using pytest parameterize.


# Check all metrics which are degenerate when passed a single sample.
for metric in [r2_score]:
with warnings.catch_warnings(record=True) as w:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

with pytest.warns(....)


from sklearn.metrics.regression import _check_reg_targets

from ...exceptions import DataDimensionalityWarning
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wouldn't UndefinedMetricWarning be more apt? Or even a simple UserWarning. What do you think @jnothman ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right, UndefinedMetricWarning makes more sense with that warning message and is more consistent with other warnings. Let's see what @jnothman has to say.

@adrinjalali
Copy link
Copy Markdown
Member

Please add an entry to the change log at doc/whats_new/v*.rst. Like the other entries there, please reference this pull request with :issue: and credit yourself (and other contributors if applicable) with :user:.

@jnothman jnothman merged commit 4c2eb3a into scikit-learn:master Jan 8, 2019
@jnothman
Copy link
Copy Markdown
Member

jnothman commented Jan 8, 2019

Thanks, @psendyk!

xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants