gpt4 book ai didi

python - 如何有效地连接 numpy 中的许多 arange 调用?

转载 作者:太空狗 更新时间:2023-10-29 22:26:38 24 4
gpt4 key购买 nike

我想在 cnt 值的向量上对像 numpy.arange(0, cnt_i) 这样的调用进行向量化,并像下面的代码片段那样连接结果:

import numpy
cnts = [1,2,3]
numpy.concatenate([numpy.arange(cnt) for cnt in cnts])

array([0, 0, 1, 0, 1, 2])

不幸的是,由于临时数组和列表理解循环,上面的代码内存效率非常低。

有没有办法在 numpy 中更有效地做到这一点?

最佳答案

这是一个完全向量化的函数:

def multirange(counts):
counts = np.asarray(counts)
# Remove the following line if counts is always strictly positive.
counts = counts[counts != 0]

counts1 = counts[:-1]
reset_index = np.cumsum(counts1)

incr = np.ones(counts.sum(), dtype=int)
incr[0] = 0
incr[reset_index] = 1 - counts1

# Reuse the incr array for the final result.
incr.cumsum(out=incr)
return incr

这是@Developer 的答案的变体,它只调用一次 arange:

def multirange_loop(counts):
counts = np.asarray(counts)
ranges = np.empty(counts.sum(), dtype=int)
seq = np.arange(counts.max())
starts = np.zeros(len(counts), dtype=int)
starts[1:] = np.cumsum(counts[:-1])
for start, count in zip(starts, counts):
ranges[start:start + count] = seq[:count]
return ranges

这是原始版本,写成一个函数:

def multirange_original(counts):
ranges = np.concatenate([np.arange(count) for count in counts])
return ranges

演示:

In [296]: multirange_original([1,2,3])
Out[296]: array([0, 0, 1, 0, 1, 2])

In [297]: multirange_loop([1,2,3])
Out[297]: array([0, 0, 1, 0, 1, 2])

In [298]: multirange([1,2,3])
Out[298]: array([0, 0, 1, 0, 1, 2])

使用更大的计数数组比较时序:

In [299]: counts = np.random.randint(1, 50, size=50)

In [300]: %timeit multirange_original(counts)
10000 loops, best of 3: 114 µs per loop

In [301]: %timeit multirange_loop(counts)
10000 loops, best of 3: 76.2 µs per loop

In [302]: %timeit multirange(counts)
10000 loops, best of 3: 26.4 µs per loop

关于python - 如何有效地连接 numpy 中的许多 arange 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20027936/

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