重复数组的每个值不同的时间 [英] Repeat each values of an array different times

查看:18
本文介绍了重复数组的每个值不同的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设 a = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]s = [3, 3, 9, 3, 6, 3].我正在寻找精确重复 a[i]s[i] 次的最佳方法,然后以 b = [0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, ... ].

我想尽快做这件事,因为我必须做很多次.我使用 Python 和 numpy,数组被定义为 numpy.ndarray.我四处搜索,发现了 repeattilecolumn_stack,它们可以很好地用于重复每个元素 n次,但我想重复他们每个人不同的时间.

一种方法是:

a = hsplit(a, 6)对于范围内的 i(len(a)):a[i] = 重复(a[i], s[i])a = a.flatten()

我想知道是否有更好的方法来做到这一点.

解决方案

这正是 numpy.repeat 确实:

<预><代码>>>>a = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6])>>>s = np.array([3, 3, 9, 3, 6, 3])>>>np.repeat(a, s)数组([ 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3,0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5,0.5, 0.5, 0.6, 0.6, 0.6])

在纯 Python 中,您可以执行以下操作:

<预><代码>>>>从 itertools 导入重复、链、imap>>>列表(chain.from_iterable(imap(repeat, a, s)))[0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, .5, 0.5, 0.5, 0.5, 0.5, 0.6, 0.6]

但当然它会比它的 NumPy 等价物慢得多:

<预><代码>>>>s = [3, 3, 9, 3, 6, 3]*1000>>>a = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]*1000>>>%timeit 列表(chain.from_iterable(imap(repeat, a, s)))1000 个循环,最好的 3 个:每个循环 1.21 毫秒>>>%timeit np.repeat(a_a, s_a) #a_a 和 s_a 是与 a 和 b 大小相同的 NumPy 数组10000 个循环,最好的 3 个:每个循环 202 µs

Suppose a = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] and s = [3, 3, 9, 3, 6, 3]. I'm looking for the best way to repeat a[i] exactly s[i] times and then have a flatten array in the form of b = [0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, ... ].

I want to do this as fast as possible since I have to do it many times. I'm using Python and numpy and the arrays are defined as numpy.ndarray. I searched around and find out about repeat, tile and column_stack which can be used nicely to repeat each element n times but I wanted to repeat each of them different times.

One way to do this is:

a = hsplit(a, 6)
for i in range(len(a)):
    a[i] = repeat(a[i], s[i])
a = a.flatten()

I am wondering if there is a better way to do it.

解决方案

That's exactly what numpy.repeat does:

>>> a = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6])
>>> s = np.array([3, 3, 9, 3, 6, 3])
>>> np.repeat(a, s)
array([ 0.1,  0.1,  0.1,  0.2,  0.2,  0.2,  0.3,  0.3,  0.3,  0.3,  0.3,
        0.3,  0.3,  0.3,  0.3,  0.4,  0.4,  0.4,  0.5,  0.5,  0.5,  0.5,
        0.5,  0.5,  0.6,  0.6,  0.6])

In pure Python you can do something like:

>>> from itertools import repeat, chain, imap
>>> list(chain.from_iterable(imap(repeat, a, s)))
[0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.6, 0.6, 0.6]

But of course it is going to be way slower than its NumPy equivalent:

>>> s = [3, 3, 9, 3, 6, 3]*1000
>>> a = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]*1000
>>> %timeit list(chain.from_iterable(imap(repeat, a, s)))
1000 loops, best of 3: 1.21 ms per loop
>>> %timeit np.repeat(a_a, s_a) #a_a and s_a are NumPy arrays of same size as a and b
10000 loops, best of 3: 202 µs per loop

这篇关于重复数组的每个值不同的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆