Skip to content

[MRG+2] Add max_error to the existing set of metrics for regression #12232

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

Merged
merged 23 commits into from
Oct 11, 2018
Merged

[MRG+2] Add max_error to the existing set of metrics for regression #12232

merged 23 commits into from
Oct 11, 2018

Conversation

whiletruelearn
Copy link
Contributor

Reference Issues/PRs

Fixes #12231

What does this implement/fix? Explain your changes.

Added a new metric for regression which is max_error. It is the worst case error between the predicted value and the true value.

 >>> from sklearn.metrics import max_error
 >>> y_true = [3.1, 2.4, 7.6, 1.9]
 >>> y_pred = [4.1, 2.3, 7.4, 1.7]
 >>> max_error(y_true, y_pred)
    1.0

Any other comments?

Copy link
Contributor

@eamanu eamanu left a comment

Choose a reason for hiding this comment

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

Please add some test

@eamanu
Copy link
Contributor

eamanu commented Oct 1, 2018

Add [WIP] to the PR name

@whiletruelearn whiletruelearn changed the title Add max_error to the existing set of metrics for regression [WIP] Add max_error to the existing set of metrics for regression Oct 1, 2018
@qinhanmin2014
Copy link
Member

Is this metric widely used? Do you have some more references about the metric? Personally I'm not persuaded to add it.
If you add a metric, you also need a scorer.

@whiletruelearn
Copy link
Contributor Author

@qinhanmin2014 I have updated scorer and It is cited as as a metric here
Thought it will be nice to have in sklearn as well.

@qinhanmin2014
Copy link
Member

@qinhanmin2014 I have updated scorer and It is cited as as a metric here
Thought it will be nice to have in sklearn as well.

I've read that in your issue but I don't think it's persuasive. Some papers/books/a wiki entry might be better from my side, or you can wait to see if other core devs will support you. We already have many metrics and I'm personally a bit selective for new metrics.

@whiletruelearn
Copy link
Contributor Author

whiletruelearn commented Oct 1, 2018

@qinhanmin2014 Thanks for the review. I am closing this PR.

Wanted to get your thoughts on this. I see bunch of other metrics related to regression.

  • MPE
  • MAPE
  • adjusted R2
  • AIC
  • BIC

Is there any historical reason why these metrics are not considered. Or would it be nice to have them implemented?. I see that Statsmodel currently prints out these metrics when we call summary on a model.

@eamanu
Copy link
Contributor

eamanu commented Oct 1, 2018

I sincerely, heard about this very little. But, I will prefer to have more and not less.

@qinhanmin2014
Copy link
Member

I sincerely, heard about this very little. But, I will prefer to have more and not less.

@eamanu I'm not opposed to more metrics, my point here is that we should ensure that the metrics we included are well-defined and widely accepted, but none of you have provided sufficient evidence to show that this metric is widely accepted.

@qinhanmin2014
Copy link
Member

@qinhanmin2014 Thanks for the review. I am closing this PR.

@whiletruelearn Thanks for the PR. If you want more feedback from others, feel free to reopen it :)

Wanted to get your thoughts on this. I see bunch of other metrics related to regression.

MAPE is likely to be included in 0.21 (see #10711)
For other metrics, I'm unable to recall (and find) related issue/PR so if you have enough references to show that they are well-defined and widely accepted, I think contributions are welcomed.

@jnothman
Copy link
Member

jnothman commented Oct 3, 2018 via email

@whiletruelearn
Copy link
Contributor Author

@jnothman can i reopen this PR and work on this ?

@qinhanmin2014 qinhanmin2014 reopened this Oct 3, 2018
@qinhanmin2014
Copy link
Member

I wouldn't object to providing it with appropriate motivation in the user guide.

This is also what I want to know about the metric.

@whiletruelearn
Copy link
Contributor Author

whiletruelearn commented Oct 3, 2018

I get why this might be useful as a diagnostic measure. I think its meaning
is unambiguous for single output regression.

@jnothman I feel the same way. It is a simple diagnostic measure that will tell how good a fit the model is. It is straightforward to understand for everyone.

@qinhanmin2014 Sharing a bunch of links where people have discussed about this metric. Having dived a little deeper, i feel this metric can be useful.

[1] https://stats.stackexchange.com/questions/197642/linear-fit-how-to-minimize-maximum-error-rather-than-average-error
[2] https://math.stackexchange.com/questions/47944/linear-regression-for-minimizing-the-maximum-of-the-residuals
[3] https://lib.dr.iastate.edu/cgi/viewcontent.cgi?referer=https://www.google.co.in/&httpsredir=1&article=7550&context=rtd
[4] https://pdfs.semanticscholar.org/7971/868bf856e611aca547e01c29d6146f117450.pdf

Please let me know if i need something else to be changed

@whiletruelearn whiletruelearn changed the title [WIP] Add max_error to the existing set of metrics for regression [MRG] Add max_error to the existing set of metrics for regression Oct 3, 2018
@qinhanmin2014
Copy link
Member

@jnothman I feel the same way. It is a simple diagnostic measure that will tell how good a fit the model is. It is straightforward to understand for everyone.

In my previous comments, I'm not arguing that this metric is not well-defined. I'm worrying about whether it's widely accepted.

@qinhanmin2014 Sharing a bunch of links where people have discussed about this metric. Having dived a little deeper, i feel this metric can be useful.

Thanks for these materials, but they are about a regression method which minimize maximum error, right? (apologies if I missed something in the 100+ page reference). I'd rather see something defining such a metric, though these seems enough to keep me +0 instead of -1 for it.

@@ -573,3 +574,37 @@ def r2_score(y_true, y_pred, sample_weight=None,
avg_weights = multioutput

return np.average(output_scores, weights=avg_weights)


def max_error(y_true, y_pred):
Copy link
Member

Choose a reason for hiding this comment

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

I guess we can support sample_weight here, right?

Copy link
Contributor Author

@whiletruelearn whiletruelearn Oct 4, 2018

Choose a reason for hiding this comment

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

Thought about this for some time . Does it make sense to add? I see that median_absolute_error also doesn't accept sample weight. We are calculating the max residual error right, i couldn't fully understand the purpose sample_weight would provide here.

Copy link
Member

Choose a reason for hiding this comment

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

See #3450 and #6217

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I have made the change. Can you please review.

@@ -60,6 +60,7 @@
from .regression import mean_squared_log_error
from .regression import median_absolute_error
from .regression import r2_score
from .regression import max_error
Copy link
Member

Choose a reason for hiding this comment

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

alphabet order

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated.

1.0
"""
y_type, y_true, y_pred, _ = _check_reg_targets(y_true, y_pred,
'uniform_average')
Copy link
Member

Choose a reason for hiding this comment

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

This seems awkward. If we decide to include the metric, we might add a default value to multioutput

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed it to None. Is that the right approach?

'uniform_average')
if y_type == 'continuous-multioutput':
raise ValueError("Multioutput not supported in max_error")
max_error = np.around(np.max(np.abs(y_true - y_pred)), decimals=3)
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @qinhanmin2014. Or why not create a parameter to set the decimal number?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed.

@whiletruelearn
Copy link
Contributor Author

@qinhanmin2014 sorry for not stating clearly earlier.
In [4] https://pdfs.semanticscholar.org/7971/868bf856e611aca547e01c29d6146f117450.pdf

section 2.3 speaks about this metric.

image

Copy link
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

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

Tbh I don't know much about regression evaluation either, but this intuitively seems reasonable to me. The question of whether it's inappropriate to maintain this along with all the others is hard to decide, when someone may indeed question sample_weight support, or multi-output regression...

Copy link
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

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

I'll follow Joel, @whiletruelearn please add something to the user guide.

@qinhanmin2014
Copy link
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:.

@qinhanmin2014
Copy link
Member

At least it's straightforward and easy to maintain :)

@eamanu
Copy link
Contributor

eamanu commented Oct 6, 2018

When you add the entry to the change log (@qinhanmin2014 comment), it will ready for me.

@whiletruelearn
Copy link
Contributor Author

Thanks @jnothman @qinhanmin2014 @eamanu .

I have updated the change log as well.

@qinhanmin2014
Copy link
Member

please add something to the user guide.

@whiletruelearn
Copy link
Contributor Author

@qinhanmin2014 do you mean over metrics.rst ?
I don't see any other metric for regression mentioned over there.

@qinhanmin2014
Copy link
Member

See http://scikit-learn.org/dev/modules/model_evaluation.html#regression-metrics

@whiletruelearn
Copy link
Contributor Author

@qinhanmin2014 I have updated the user guide. Can you please let me know if this if fine ?

@whiletruelearn
Copy link
Contributor Author

Thanks @jnothman . I have updated the docs based on your suggestion.

Max error
-------------------

The :func:`max_error` function computes the maximum `residual error <https://en.wikipedia.org/wiki/Errors_and_residuals`_,
Copy link
Member

Choose a reason for hiding this comment

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

please generally keep under 80 chars per line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

@whiletruelearn
Copy link
Contributor Author

@qinhanmin2014 @jnothman I have made the changes as per review comments. Can you please let me know if there are any other changes to make?

Copy link
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

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

Some formatting issues. It would be better if you check your PR carefully again :)

Copy link
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @whiletruelearn

@qinhanmin2014 qinhanmin2014 changed the title [MRG] Add max_error to the existing set of metrics for regression [MRG+2] Add max_error to the existing set of metrics for regression Oct 11, 2018
:mod:`sklearn.metrics`
......................

- |Feature| A new regression metric: :class:`metrics.max_error`: a
Copy link
Member

Choose a reason for hiding this comment

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

this should be a sentence and you need to mention the scorer
see e.g., Added the metrics.balanced_accuracy_score metric and a corresponding 'balanced_accuracy' scorer for binary and multiclass classification. #8066 by @xyguo and Aman Dalmia, and #10587 by Joel Nothman.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. Also changed it from :class: to :func:

Copy link
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @whiletruelearn

@qinhanmin2014 qinhanmin2014 merged commit 831c760 into scikit-learn:master Oct 11, 2018
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.

Add max error metric for regression
4 participants