2

我有一个大代码,需要一些时间才能运行。我已经找到了占用大部分时间的两条线路,我想知道是否有办法加快它们的速度。这是一个MWE:

import numpy as np

def setup(k=2, m=100, n=300):
    return np.random.randn(k,m), np.random.randn(k,n),np.random.randn(k,m)
# make some random points and weights
a, b, w = setup()

# Weighted euclidean distance between arrays a and b.
wdiff = (a[np.newaxis,...] - b[np.newaxis,...].T) / w[np.newaxis,...]

# This is the set of operations that need a performance boost:
dist_1 = np.exp(-0.5*(wdiff*wdiff)) / w
dist_2 = np.array([i[0]*i[1] for i in dist_1])

我来自这个问题顺便说一句,数组中点之间的快速加权欧几里得距离,其中ali_m提出了他的惊人答案,通过应用广播为我节省了很多时间(我对此一无所知,但至少)可以应用类似的东西用这些线?

4

1 回答 1

3

您的dist_2计算可以加快 10 倍左右:

>>> dist_1.shape
(300, 2, 100)
>>> %timeit dist_2 = np.array([i[0]*i[1] for i in dist_1])
1000 loops, best of 3: 1.35 ms per loop
>>> %timeit dist_2 = dist_1.prod(axis=1)
10000 loops, best of 3: 116 µs per loop
>>> np.allclose(np.array([i[0]*i[1] for i in dist_1]), dist_1.prod(axis=1))
True

dist_1由于大部分时间都花在求幂上,因此我无法对您做太多事情:

>>> %timeit (-0.5*(wdiff*wdiff)) / w
1000 loops, best of 3: 467 µs per loop
>>> %timeit np.exp((-0.5*(wdiff*wdiff)))/w
100 loops, best of 3: 3.3 ms per loop
于 2013-10-12T02:17:07.510 回答