gpt4 book ai didi

python - 提高数组处理的性能

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:59 24 4
gpt4 key购买 nike

我有一个很大的代码,需要一些时间来运行。我已经找到了占用大部分时间的两条线,我想知道是否有办法加快它们的速度。这是一个 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])

我来自这个问题 BTW Fast weighted euclidean distance between points in arrays ali_m 提出了他惊人的答案,通过应用广播(我对此一无所知,但至少)为我节省了很多时间线条?

最佳答案

您的 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

关于python - 提高数组处理的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com