Fastest way to populate a matrix with a function on pairs of elements in two numpy vectors?

前端 未结 3 1943
无人及你
无人及你 2021-01-14 14:52

I have two 1 dimensional numpy vectors va and vb which are being used to populate a matrix by passing all pair combinations to a function.

3条回答
  •  终归单人心
    2021-01-14 15:17

    One of the least known numpy functions for what the docs call functional programming routines is np.frompyfunc. This creates a numpy ufunc from a Python function. Not some other object that closely simulates a numpy ufunc, but a proper ufunc with all its bells and whistles. While the behavior is in many aspects very similar to np.vectorize, it has some distinct advantages, that hopefully the following code should highlight:

    In [2]: def f(a, b):
       ...:     return a + b
       ...:
    
    In [3]: f_vec = np.vectorize(f)
    
    In [4]: f_ufunc = np.frompyfunc(f, 2, 1)  # 2 inputs, 1 output
    
    In [5]: a = np.random.rand(1000)
    
    In [6]: b = np.random.rand(2000)
    
    In [7]: %timeit np.add.outer(a, b)  # a baseline for comparison
    100 loops, best of 3: 9.89 ms per loop
    
    In [8]: %timeit f_vec(a[:, None], b)  # 50x slower than np.add
    1 loops, best of 3: 488 ms per loop
    
    In [9]: %timeit f_ufunc(a[:, None], b)  # ~20% faster than np.vectorize...
    1 loops, best of 3: 425 ms per loop
    
    In [10]: %timeit f_ufunc.outer(a, b)  # ...and you get to use ufunc methods
    1 loops, best of 3: 427 ms per loop
    

    So while it is still clearly inferior to a properly vectorized implementation, it is a little faster (the looping is in C, but you still have the Python function call overhead).

提交回复
热议问题