我有一个大代码,需要一些时间才能运行。我已经找到了占用大部分时间的两条线路,我想知道是否有办法加快它们的速度。这是一个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提出了他的惊人答案,通过应用广播为我节省了很多时间(我对此一无所知,但至少)可以应用类似的东西用这些线?