forked from numpy/numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix percentile and quantile #46
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4237,7 +4237,9 @@ def percentile(a, | |
| if a.dtype.kind == "c": | ||
| raise TypeError("a must be an array of real numbers") | ||
|
|
||
| q = np.true_divide(q, 100) | ||
| # Use dtype of array if possible (e.g., if q is a python int or float) | ||
| # by making the divisor have the dtype of the data array. | ||
| q = np.true_divide(q, a.dtype.type(100) if a.dtype.kind == "f" else 100) | ||
| q = asanyarray(q) # undo any decay that the ufunc performed (see gh-13105) | ||
| if not _quantile_is_valid(q): | ||
| raise ValueError("Percentiles must be in the range [0, 100]") | ||
|
|
@@ -4498,7 +4500,12 @@ def quantile(a, | |
| if a.dtype.kind == "c": | ||
| raise TypeError("a must be an array of real numbers") | ||
|
|
||
| q = np.asanyarray(q) | ||
| # Use dtype of array if possible (e.g., if q is a python int or float). | ||
| if isinstance(q, (int, float)) and a.dtype.kind == "f": | ||
| q = np.asanyarray(q, dtype=np.result_type(a, q)) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also just use this stanza for Note that this would not work for user types either given the check for |
||
| else: | ||
| q = np.asanyarray(q) | ||
|
|
||
| if not _quantile_is_valid(q): | ||
| raise ValueError("Quantiles must be in the range [0, 1]") | ||
| return _quantile_unchecked( | ||
|
|
@@ -4596,7 +4603,9 @@ def _get_gamma(virtual_indexes, previous_indexes, method): | |
| """ | ||
| gamma = np.asanyarray(virtual_indexes - previous_indexes) | ||
| gamma = method["fix_gamma"](gamma, virtual_indexes) | ||
| return np.asanyarray(gamma) | ||
| # Ensure both that we have an array, and that we keep the dtype | ||
| # (which may have been matched to the input array). | ||
| return np.asanyarray(gamma, dtype=virtual_indexes.dtype) | ||
|
|
||
|
|
||
| def _lerp(a, b, t, out=None): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, feels a bit brittle, but I have to sleep on it probably (also doesn't generalize easily to user DTypes, but OK).
Maybe in this case it is actually easier to just track
was_pyscalar = type(q) in (int, float, complex)and then applyif was_pyscalar: pos = float(pos)at the end of the calculation.Beyond figuring this out, one poitn I am annoying about it is, that I think this is an important problem to get eyes on, because
percentilecan't be the only function in NumPy (and even more so downstream!) to run into this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with
was_pyscalaris that I think the expectation should be that the result keeps the array inputdtype, but only if it isfloat.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, by
posthere, I don't mean the end-result, but the interpolation point value that is used for the last bit of the calculation. I.e. the intermediate result just before mixing it with the values froma.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. Indeed, that could work too. I think I slightly prefer to trying to deal with things up front - I'm trying to think of these functions as
gufuncsand following the same logic...