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

前端 未结 3 1942
无人及你
无人及你 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:12

    cdist is fast because it is written in highly-optimized C code (as you already pointed out), and it only supports a small predefined set of metrics.

    Since you want to apply the operation generically, to any given foo function, you have no choice but to call that function na-times-nb times. That part is not likely to be further optimizable.

    What's left to optimize are the loops and the indexing. Some suggestions to try out:

    1. Use xrange instead of range (if in python2.x. in python3, range is already a generator-like)
    2. Use enumerate, instead of range + explicitly indexing
    3. Use a python speed "magic", such as cython or numba, to speed up the looping process.

    If you can make further assumptions about foo, it might be possible to speed it up further.

提交回复
热议问题