Vectorize Is Indeterminate
I'm trying to vectorize a simple function in numpy and getting inconsistent behavior. I expect my code to return 0 for values < 0.5 and the unchanged value otherwise. Strangel
Solution 1:
If this really is the problem you want to solve, then there's a much better solution:
A[A<=0.5] = 0.0
The problem with your code, however, is that if the condition passes, you are returning the integer 0, not the float 0.0. From the documentation:
The data type of the output of
vectorized
is determined by calling the function with the first element of the input. This can be avoided by specifying theotypes
argument.
So when the very first entry is <0.5
, it tries to create an integer, not float, array.
You should change return 0
to
return0.0
Alternately, if you don't want to touch my_func
, you can use
f = np.vectorize(my_func, otypes=[np.float])
Post a Comment for "Vectorize Is Indeterminate"