8000 MAINT: cleanups to quantile by eric-wieser · Pull Request #16274 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: cleanups to quantile #16274

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 2 commits into from
May 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions numpy/lib/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3943,42 +3943,29 @@ def _quantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False,
if np.issubdtype(a.dtype, np.inexact):
indices_above = concatenate((indices_above, [-1]))

weights_above = indices - indices_below
weights_below = 1 - weights_above

weights_shape = [1, ] * ap.ndim
weights_shape[axis] = len(indices)
weights_below.shape = weights_shape
weights_above.shape = weights_shape

ap.partition(concatenate((indices_below, indices_above)), axis=axis)

# ensure axis with q-th is first
ap = np.moveaxis(ap, axis, 0)
weights_below = np.moveaxis(weights_below, axis, 0)
weights_above = np.moveaxis(weights_above, axis, 0)
axis = 0

weights_shape = [1] * ap.ndim
weights_shape[axis] = len(indices)
weights_above = (indices - indices_below).reshape(weights_shape)

# Check if the array contains any nan's
if np.issubdtype(a.dtype, np.inexact):
indices_above = indices_above[:-1]
n = np.isnan(ap[-1:, ...])

x1 = take(ap, indices_below, axis=axis) * weights_below
x1 = take(ap, indices_below, axis=axis) * (1 - weights_above)
x2 = take(ap, indices_above, axis=axis) * weights_above

# ensure axis with q-th is first
x1 = np.moveaxis(x1, axis, 0)
x2 = np.moveaxis(x2, axis, 0)

if zerod:
x1 = x1.squeeze(0)
x2 = x2.squeeze(0)

if out is not None:
r = add(x1, x2, out=out)
else:
r = add(x1, x2)
r = add(x1, x2, out=out)

if np.any(n):
if zerod:
Expand Down
0