重复二维数组的行

时间:2019-02-05 23:08:53

标签: python numpy

我有一个numpy数组,我想重复n次,同时保留行的原始顺序:

>>>a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

所需输出(n = 2):

>>>a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

我发现了一个np.repeat函数,但是它不保留列的原始顺序。还有其他内置函数或技巧可以在保留顺序的同时重复数组吗?

6 个答案:

答案 0 :(得分:2)

使用np.repeat,然后使用np.concatenate

np.concatenate(np.repeat(a[None, :], n, axis=0), axis=0)

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

另一种选择是使用np.broadcast_to

np.broadcast_to(a, (n, *a.shape)).reshape(-1, a.shape[1])

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

答案 1 :(得分:2)

这是另一种方式。我还添加了一些时间与@coldspeed的解决方案进行比较

n = 2
a_new = np.tile(a.flatten(), n) 
a_new.reshape((n*a.shape[0], a.shape[1]))
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11],
#        [ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11]])

与Coldspeed解决方案的性能比较

我的方法,n = 10000

a = np.array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
n = 10000

def tile_flatten(a, n):
    a_new = np.tile(a.flatten(), n).reshape((n*a.shape[0], a.shape[1])) 
    return a_new

%timeit tile_flatten(a,n)
# 149 µs ± 20.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)   

冷速解决方案1 ​​,n = 10000

a = np.array([[ 0,  1,  2,  3],
   [ 4,  5,  6,  7],
   [ 8,  9, 10, 11]])
n = 10000

def concatenate_repeat(a, n):
    a_new =  np.concatenate(np.repeat(a[None, :], n, axis=0), axis=0)
    return a_new

%timeit concatenate_repeat(a,n)
# 7.61 ms ± 1.37 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

冷速解决方案2 ,n = 10000

a = np.array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
n = 10000

def broadcast_reshape(a, n):
    a_new =  np.broadcast_to(a, (n, *a.shape)).reshape(-1, a.shape[1])
    return a_new

%timeit broadcast_reshape(a,n)
# 162 µs ± 29.8 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

@ user2357112的解决方案

def tile_only(a, n):
    a_new = np.tile(a, (n, 1))
    return a_new

%timeit tile_only(a,n)
# 142 µs ± 21.8 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

答案 2 :(得分:2)

numpy.repeat用于逐个元素重复。要重复整个数组,您需要numpy.tile

numpy.tile(a, (2, 1))

元组是每个轴上的重复次数。您想要第一个为2,第二个为1,所以元组为(2, 1)

答案 3 :(得分:1)

在这种情况下,n的填充模式很有用:

m

def whiten(self, x): shape = x.shape x = K.batch_flatten(x) mn = K.mean(x, 0) std = K.std(x, 0) + K.epsilon() r = (x - mn) / std r = K.reshape(x, (-1,shape[1],shape[2],shape[3])) return r # 方法是不同的。)

答案 4 :(得分:0)

您可以尝试numpy.tile()

这是在保存原始订单的同时使用 numpy.tile 重复阵列的方法:

import numpy as np

a = np.array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

n = 5
b = np.tile(a, (n,1))
print b

输出:

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]] 

答案 5 :(得分:0)

您也可以尝试

b=np.append(a,a).reshape(np.shape(a)[0]*2,np.shape(a)[1])

输出

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])