Skip to content
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

[MRG] Add drop_intermediate kwarg to metrics.precision_recall_curve #24668

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dberenbaum
Copy link

@dberenbaum dberenbaum commented Oct 15, 2022

Reference Issues/PRs

Fixes #21825

What does this implement/fix? Explain your changes.

Adds a drop_intermediate kwarg to metrics.precision_recall_curve similar to the one that already exists for metrics.roc_curve. This removes unnecessary points on the curve to reduce its size.

# with the same tps value have the same recall and thus x coordinate.
# They appear as a vertical line on the plot.
optimal_idxs = np.where(
np.r_[True, np.logical_or(np.diff(tps[:-1]), np.diff(tps[1:])), True]
Copy link
Contributor

@betatim betatim Oct 17, 2022

Choose a reason for hiding this comment

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

For my education: why does taking the "second derivative" in roc_curve work, but here it doesn't?

Copy link
Contributor

@betatim betatim Oct 17, 2022

Choose a reason for hiding this comment

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

More precisely, why does using the second derivative work for roc_curve? What we are looking for is two (or more) points where there is no change, so the first derivative seems like the natural thing to use :-/

Copy link
Contributor

@betatim betatim Oct 17, 2022

Choose a reason for hiding this comment

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

Thinking about this some more, could we use np.r_[True, np.diff(tps, 2), True] instead?

For tps = [1, 2, 3, 3, 3, 5, 6] we'd get [1, 3, 3, 5, 6] (the two gets dropped because its is on the line between 1 and 3. For tps = [1,2.1,3,3,3,5,6] we get [1., 2.1, 3., 3., 5., 6.].

I guess for plotting purposes it is fine to remove the 2?! Is there a reason to have different behaviour regarding the removal of points in roc_curve and this (with np.logical_or(np.diff(tps[:-1]), np.diff(tps[1:])) the 2 is kept)?

Copy link
Author

@dberenbaum dberenbaum Oct 17, 2022

Choose a reason for hiding this comment

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

The difference is that both axes of an ROC curve have constant denominators:

  • fpr = fps / fps[-1] (linearly correlated with fps)
  • tpr = tps / tps[-1] (linearly correlated with tps)

By contrast, precision has a non-constant denominator (note that recall = tpr):

  • precision = tps / (tps + fps) (not linearly correlated with either tps or fps)

If you extend your example by one to tps = [1, 2, 3, 3, 3, 5, 6, 7], then you will get:

tps = [1, 3, 3, 3, 5, 6, 7]
fps = [0, 0, 1, 2, 2, 2, 2]
tpr = [1/7, 3/7, 3/7, 3/7, 5/7, 6/7, 7/7]
fpr = [0/2, 0/2, 1/2, 2/2, 2/2, 2/2, 2/2]
precision = [1/1, 3/3, 3/4, 3/5, 5/7, 6/8, 7/9]

np.r_[True, np.logical_or(np.diff(tps[:-1]), np.diff(tps[1:])), True] results in [1, 3, 3, 5, 6, 7].

np.r_[True, np.diff(tps, 2), True] results in [1, 3, 3, 5, 7].

The second method incorrectly drops the 6, which is not actually on a line in the precision-recall curve:

image

Copy link
Contributor

@betatim betatim Oct 18, 2022

Choose a reason for hiding this comment

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

Today I learnt! Thanks for taking the time to explain it

Copy link
Contributor

@betatim betatim left a comment

Looks good to me.

Does this need an entry in "what's new"?

@dberenbaum
Copy link
Author

dberenbaum commented Oct 17, 2022

Looks good to me.

Does this need an entry in "what's new"?

Not sure if this question is to me? I'm not sure what justifies a "what's new" entry but happy to provide one if needed.

@betatim
Copy link
Contributor

betatim commented Oct 18, 2022

Not sure if this question is to me? I'm not sure what justifies a "what's new" entry but happy to provide one if needed.

It was aimed at someone "in the know", because I also don't know the inclusion criteria.

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

Successfully merging this pull request may close these issues.

Add drop_intermediate to precision_recall_curve
2 participants