Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ufunc.reduce(..., where=) #8342

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

Closed
anntzer opened this issue Dec 5, 2016 · 6 comments
Closed

ufunc.reduce(..., where=) #8342

anntzer opened this issue Dec 5, 2016 · 6 comments

Comments

@anntzer
Copy link
Contributor

anntzer commented Dec 5, 2016

It would be nice if ufunc.reduce gained a where=... kwarg (taking either a boolean mask, or a list of indices). This would avoid having to construct an intermediate array in expressions such as np.max(t[t > 0]).

In particular I wonder whether this would cover most of the difference in the performance of nansum (and friends) with bottleneck.

@eric-wieser
Copy link
Member

eric-wieser commented Dec 11, 2016

Presumably ufunc.accumulate should acquire this too?

This would be usable in masked arrays as well, right?

@anntzer
Copy link
Contributor Author

anntzer commented Dec 11, 2016

Sounds reasonable, although we'd need to figure it how to handle the case where axis is passed and the mask has inconsistent sizes (leading to a ragged array).
Maaked arrays could be rewritten to use this mechanism, yes.

@eric-wieser
Copy link
Member

eric-wieser commented Dec 11, 2016

I'd argue that for consistency with other uses of where, the resulting array should not be ragged, but be holey in the same way that np.add([1, 2, 3], [4, 5, 6], where=[1, 0, 1]) -> [5, 0, 9]. So np.add.accumuate(arr, where=m)[m] would be the same as np.add.accumulate(arr[m]), and the other entries would presumably be zero.

@eric-wieser
Copy link
Member

eric-wieser commented May 8, 2017

Hmm, the semantics here are ambiguous to me.

ufunc(a, b, out=c, where=w) means c[w] = ufunc(a[w], b[w])

Would ufunc.reduce(a, out=c, where=w) (a 2d, w 1d) mean:

  • c[w.any()] = ufunc.reduce(a[w,:]) - only reduce the normalized rows where we requested an output
  • c[w] = ufunc.reduce(a[:,w]) - only reduce the normalized rows where we requested an output
  • c[:] = ufunc.reduce(a[w,:]) - always write to all outputs, but write the identity for empty rows

I think you were proposing the last one of these, but the semantics of where for normal ufuncs would lead to one of the first ones.

@anntzer
Copy link
Contributor Author

anntzer commented May 8, 2017

To be honest I had not thought about >1d semantics at all.

@mhvk
Copy link
Contributor

mhvk commented Jan 5, 2019

reduce now has a where argument (see #12644), so closing (in principle, accumulate and reduceat could gain it too, but let those get there own issues).

@mhvk mhvk closed this as completed Jan 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants