Skip to content

ENH: Add np.quantile - #9213

Closed
eric-wieser wants to merge 4 commits into
numpy:masterfrom
eric-wieser:quantile
Closed

eric-wieser wants to merge 4 commits into
numpy:masterfrom
eric-wieser:quantile

Conversation

@eric-wieser

Copy link
Copy Markdown
Member

Breaks off from #9211.

quantile is arguably a cleaner interface than percentile, as it removes the need to multiply by 100

Comment thread numpy/lib/function_base.py Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy was only needed for the in-place divide by 100

Comment thread numpy/lib/function_base.py Outdated

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.

Needs argument alignment.

Comment thread numpy/lib/function_base.py Outdated

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.

Alignment.

Comment thread numpy/lib/function_base.py Outdated

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.

Error message looks wrong.

Comment thread numpy/lib/function_base.py Outdated

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.

Error message looks wrong.

@eric-wieser eric-wieser Jun 3, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Error message is shared between percentile and quantile. Should it mention both, or should we somehow taylor tailor it?

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.

I'd taylor it. At the least, something like

raise ValueError("Quantiles must be in the interval [0.0, 1.0]")

But it is a private function, so it shouldn't error. I'd move the check and error message out into the calling code.

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.

Maybe an oldfashioned python assert if you want it for debugging.

Comment thread numpy/lib/function_base.py Outdated

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.

Needs .. versionadded::,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

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.

I don't think you should cheat on the documentation like that, just make the usual docstring. I don't see a need to mention percentile here, just having it in the See Also section should be enough.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The percentile docstring is awfully long, and that risks it being fixed in one place and not the other.

I guess I could templatize it, and interpolate relevent parts for quantile and percentile?

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.

Just copy it with modifications, I don't think docstring length is something to worry about, but If anything, I'd document quantile and have percentile refer to it. In any case, "eaxctly the same` is not true.

@charris

charris commented Jun 3, 2017

Copy link
Copy Markdown
Member

Needs tests. There is something bad going on with Python2, but not obvious to me what. I suspect integer division or somesuch.

@eric-wieser

Copy link
Copy Markdown
Member Author

Assuming I address the review comments, do you think this is a worthwhile addition?

@charris

charris commented Jun 3, 2017

Copy link
Copy Markdown
Member

do you think this is a worthwhile addition?

I'm not the person to ask, I don't do much statistics and haven't even used percentile. Maybe @josef-pkt might have some input.

@charris

charris commented Jun 3, 2017

Copy link
Copy Markdown
Member

Also @rkern .

@josef-pkt

Copy link
Copy Markdown

statsmodels is using np.percentile quite a bit.
The last time I used it in an example I had forgotten about "percent", and needed to add *100 after the results didn't make much sense. (but it's an obvious error)

So, for me/us quantile would be the more obvious choice than percentile. I'm not sure it's worth two functions, but I think statsmodels would eventually completely switch over to quantile.
(My impression: using percent is for the type of users that use Excel. The median is at 0.5, i.e half, and not 50)

@shoyer

shoyer commented Jun 5, 2017

Copy link
Copy Markdown
Member

All things being equal, I certainly would prefer quantile to percentile, but I'm not sure it's really worth adding a second function.

@eric-wieser

Copy link
Copy Markdown
Member Author

@charris: I've updated with better error messages.

I've also been a little more brutal with avoiding repetition of details in the docs. Now only np.quantile describes the effect of each parameter, and np.nanquantile, np.percentile, and np.nanpercentile all refer to those. Otherwise it's just too easy to forget to update one of them.

@rgommers

Copy link
Copy Markdown
Member

Still needs that message to the mailing list, but for the record: I'm -1 on deprecating percentile (too disruptive), and neutral on adding quantile.

@chunweiyuan

Copy link
Copy Markdown

I'll write an email to the mailing list. My vote is on keeping percentile and adding quantile. Everyone I know, in fact, prefers to deal with parameters in probability space (q=0.5, not 50).

@madphysicist

Copy link
Copy Markdown
Contributor

I am all for a quantile that adds the feature that I consider most necessary here: weights. It is something I have had on the back burner since I discovered that histogram can not use auto binwidth with weighted inputs. Adding weights will make it as justifiable as having average in addition to mean.

Comment thread doc/release/1.14.0-notes.rst Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use something less subjective than "better". "Updated" for example.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

@chunweiyuan

Copy link
Copy Markdown

Looks like the merge conflicts here are rather minor. Could we fix it and merge it in? I could then update #9211 accordingly.

@eric-wieser

Copy link
Copy Markdown
Member Author

Conflicts solved, and on the mailing list both @charris and @josef-pkt seem to now be in favor. Perhaps wait a few more days for any more responses, and then merge?

@eric-wieser

Copy link
Copy Markdown
Member Author

Rebased to fix conflicts and the previous rebase mistakes, but none of the other things changed.

@chunweiyuan, can I recommend you rebase on this to fix the releasenote problems in your PR?

As an aside, you should be able to submit a PR directly against my branch at eric-wieser/numpy, but what you did here makes more sense anyway.

@chunweiyuan

Copy link
Copy Markdown

@eric-wieser since I made the mistake of doing all the changes in 1 commit, I think I'm going to: 1.) pull your branch again, 2.) make changes only to function_base.py without mucking with any release notes, and 3.) PR back.

I might try (3) against your branch, though I'm still bewildered why I wasn't able to do that beforehand.

@eric-wieser

Copy link
Copy Markdown
Member Author

You should just be able to cherry pick /rebase your single commit, and solve the small merge conflicts

chunweiyuan pushed a commit to chunweiyuan/numpy that referenced this pull request Dec 10, 2017
eric-wieser added a commit to eric-wieser/numpy that referenced this pull request Dec 10, 2017
@chunweiyuan chunweiyuan mentioned this pull request Dec 11, 2017
mhvk added a commit that referenced this pull request Dec 11, 2017
DOC: Fixup percentile docstring, from review in gh-9213
@eric-wieser

Copy link
Copy Markdown
Member Author

Closing in favor of the more recent #10199, since other parts of this PR are already merged

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.

7 participants