Numpy is trying to tell us that we can't just feed an array to a function that operates on single values. The function has to be "vectorized." Say:
We didn't get a ValueError, but the result is not correct. What happened? According to the
Data-type of output of vectorized is determined by calling the function with the first element of the input.
The problem is that although the array contains floats, we specified 0 as the value to return for n < 0.3. The first n evaluated was 0.0 which is less than 0.3 and so returns the integer 0. That leads to trouble, since the vectorized function now returns all the rest of the values as ints. Or so the docs imply. (Also see here). There are a couple more issues. First:
The output above is obtained if it is run in the same script as the first part. That is, the data type to be returned is only evaluated once, when the function is first called. If the first call is commented out, then we get this:
One way to fix the whole problem is to return a float. The following example also uses
Continuing in the docs:
This can be avoided by specifying the otypes argument as either a string of typecode characters or a list of data-types specifiers.
We're supposed to do something like:
But, in my tests, neither of these can compensate for returning the integer 0. The real reason for the otypes argument seems to be that there is a large number of numpy dtypes.