NumPy数组看起来非常慢;我做错什么了吗



对于执行相同计算的这两个函数,为什么第二个函数的运行时间大约是它的八倍?

def random_walk_2(n):
"""Return coordinates after 'n' step random walk."""
x, y = 0, 0
for _ in range(n):
(dx, dy) = random.choice([(0, 1), (0, -1), (1, 0), (-1, 0)])
x += dx
y += dy
return (x, y)     # 6.48 seconds for 20,000
def random_walk_3(n):
"""Return coordinates after 'n' step random walk."""
location = np.array([0, 0])
for _ in range(n):
move = np.array(random.choice([[0, 1], [0, -1], [1, 0], [-1, 0]]))
location += move
return location     # 54.8 seconds for 20,000

TIA,

标记

要充分利用numpy,请一次生成所有移动。在这里,我使用numpy's'版本的choice一次拾取1000个移动。然后将它们相加:

In [138]: arr = np.array([[0, 1], [0, -1], [1, 0], [-1, 0]])
In [139]: moves = np.random.choice(4, 1000)
In [140]: np.sum(arr[moves,:], axis=0)
Out[140]: array([  9, -51])

另一个";运行":

In [141]: moves = np.random.choice(4, 1000)
In [142]: np.sum(arr[moves,:], axis=0)
Out[142]: array([30, 34])

a定时:

In [144]: timeit np.sum(arr[np.random.choice(4, 20000),:],0)
952 µs ± 190 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
def random_walk(steps_per_walk):
possible_steps = np.array([[0, 1], [0, -1], [1, 0], [-1, 0]])
moves = possible_steps[np.random.choice(len(possible_steps), (walks, steps_per_walk))]
ending_locations = np.sum(moves, 1)
ending_distances = np.sum(np.abs(ending_locations), 1)
return ending_distances     # 0.50 seconds for 20,000 walks

然后调用者中的np.where会得到我想要的,小于某个值的距离的百分比。多亏了@hpaulj,这比我第一次尝试NumPy快了100倍。

相关内容

最新更新