Numpy.vectorize: Why So Slow?
Solution 1:
I'll just quote the the vectorize docstring: "The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop."
You want to make 1/(1 + abs(x)) fast. numpy has a function called numpy.abs (also called numpy.absolute--they are different names for the same object). It computes the absolute value of each element of its argument, and it does this in C code, so it is fast. Also, the Python built-in function abs knows how to dispatch arguments to objects that have the method __abs__, which numpy arrays do, so you can also use Python's abs() to compute the element-wise absolute value of a numpy array. In the following, though, I'll use np.abs.
Here's an example of the use of np.abs:
In [25]: x = np.array([-2, -1.5, 0, 5, 10])
In [26]: np.abs(x)
Out[26]: array([ 2. , 1.5, 0. , 5. , 10. ])
Here's a comparision of the performance of scipy.special.expit and 1.0/(1 + np.abs(x)) for a large array x:
In [34]: from scipy.special import expit
In [35]: x = np.random.randn(100000)
In [36]: %timeit expit(x)
1000 loops, best of 3: 771 µs per loop
In [37]: %timeit 1.0/(1 + np.abs(x))
1000 loops, best of 3: 279 µs per loop
So 1.0/(1 + np.abs(x)) is quite a bit faster than expit(x).
Post a Comment for "Numpy.vectorize: Why So Slow?"