"where" in numpy-1.13 ufuncs

I sometimes use the suggestion where

in numpy 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.

I recently upgraded to numpy 1.13. The following code 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 this is how the proposal was supposed to be used where

, but perhaps I was wrong. So I have two questions: first, what is wrong with this code; and secondly, what is the recommended way to achieve my goal?

+3


source to share


1 answer


For future reference: this turned out to be a bug in numpy. It has been fixed for the next release of numpy, presumably version 1.13.1.



Workaround for 1.13.0 is to explicitly specify the parameter out

for ufunc. The example above np.sqrt(a, where=a>0, out=np.zeros(a.shape))

works.

+2


source







All Articles