Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"where" clause in numpy-1.13 ufuncs

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?

like image 735
Kessel Avatar asked Oct 17 '22 10:10

Kessel


1 Answers

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.

like image 127
Kessel Avatar answered Oct 21 '22 06:10

Kessel