I occasionally use the where
clause in numpy's ufuncs. For example, the following:
import numpy as np
a = np.linspace(-1, 1, 10)
np.sqrt(a, where=a>0) * (a>0)
In Numpy 1.12 and earlier, this used to give me square root values where possible and zero otherwise.
Recently, though, I upgraded to numpy 1.13. The code above now gives me the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Automatic allocation was requested for an iterator operand, and it was flagged as readable, but buffering without delayed allocation was enabled
I thought that this was exactly how the where
clause was supposed to be used, but perhaps I was wrong. So I have two questions: first, what's wrong with this code; and second, what is the recommended way of achieving my goal?
For future reference: this turned out to be a bug in numpy. It has been fixed for the next numpy release, presumably version 1.13.1.
A workaround fix for 1.13.0 is to explicitly provide an out
parameter to the ufunc. In the example above, np.sqrt(a, where=a>0, out=np.zeros(a.shape))
works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With