简体   繁体   English

如何使用Numpy重复这一对2d对?

[英]How can I repeat this array of 2d pairs using Numpy?

I have an array that I want to repeat. 我有一个我想重复的数组。

test = numpy.array([(1, 11,), (2, 22), (3, 33)])

Now 现在

numpy.repeat(test, 2, 0)
numpy.repeat(test, 2, 1)

results in 结果是

array([[ 1, 11],
       [ 1, 11],
       [ 2, 22],
       [ 2, 22],
       [ 3, 33],
       [ 3, 33]])
array([[ 1,  1, 11, 11],
       [ 2,  2, 22, 22],
       [ 3,  3, 33, 33]]).

While

numpy.tile(test, 2)

results in 结果是

array([[ 1, 11,  1, 11],
       [ 2, 22,  2, 22],
       [ 3, 33,  3, 33]]).

How can I get this result instead? 我怎样才能得到这个结果呢?

array([[ 1, 11],
       [ 2, 22],
       [ 3, 33],
       [ 1, 11],
       [ 2, 22],
       [ 3, 33]])

Alternatively, for my use case I only use the repeated values once. 或者,对于我的用例,我只使用重复值一次。 To avoid the memory allocations, is there a way to have a generator of the repeated series instead somehow? 为了避免内存分配,有没有办法让某个重复序列的生成器以某种方式?

np.tile lets you specify repeats for each axis (as a tuple) np.tile允许您为每个轴指定重复(作为元组)

In [370]: np.tile(test,(2,1))
Out[370]: 
array([[ 1, 11],
       [ 2, 22],
       [ 3, 33],
       [ 1, 11],
       [ 2, 22],
       [ 3, 33]])

Here is one option: 这是一个选项:

In [15]: test = np.array([(1, 11,), (2, 22), (3, 33)])

In [16]: np.tile(test.T, 2).T
Out[16]: 
array([[ 1, 11],
       [ 2, 22],
       [ 3, 33],
       [ 1, 11],
       [ 2, 22],
       [ 3, 33]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM