我如何在自身内部重复此numpy数组?

时间:2019-06-06 15:24:40

标签: python arrays numpy

我有一个z shape的数组(8,)

>>> z
array([-30000.        , -30000.        , -30000.        , -30000.        ,
       -27703.12304688, -27703.15429688, -27703.70703125, -27703.67382812])

我想在保持位置不变的情况下再复制7次值,以创建array zr中的shape (8,8),例如:

    >>> z
    array([-30000.        , -30000.        , -30000.        , -30000.        ,
           -27703.12304688, -27703.15429688, -27703.70703125, -27703.67382812],
          [-30000.        , -30000.        , -30000.        , -30000.        ,
           -27703.12304688, -27703.15429688, -27703.70703125, -27703.67382812]
            .........)

我已经尝试过np.repeat(),但这会创建一个shape (64,)数组,我想要(8,8)

>>> zr = np.repeat(z, 8)
>>> zr
array([-30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -27703.12304688, -27703.12304688, -27703.12304688, -27703.12304688,
       -27703.12304688, -27703.12304688, -27703.12304688, -27703.12304688,
       -27703.15429688, -27703.15429688, -27703.15429688, -27703.15429688,
       -27703.15429688, -27703.15429688, -27703.15429688, -27703.15429688,
       -27703.70703125, -27703.70703125, -27703.70703125, -27703.70703125,
       -27703.70703125, -27703.70703125, -27703.70703125, -27703.70703125,
       -27703.67382812, -27703.67382812, -27703.67382812, -27703.67382812,
       -27703.67382812, -27703.67382812, -27703.67382812, -27703.67382812])
>>> zr.shape
(64,)

我在做什么错了?

3 个答案:

答案 0 :(得分:1)

np.tile与列表一起使用以返回2D数组:

# tile improvement courtesy OP
np.tile(z, [8, 1])

如果您想要只读视图,np.broadcast_to相当快:

np.broadcast_to(z, (8,)+z.shape)

答案 1 :(得分:0)

您可以尝试

np.tile(z,8).reshape(8,8)

答案 2 :(得分:0)

In [278]: z = np.arange(4)   

repeat(无轴)仅按平面顺序复制每个元素

In [280]: np.repeat(z,4)                                                                               
Out[280]: array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3])

但是可以将其按摩成所需的数组:

In [281]: np.repeat(z,4).reshape(4,4)                                                                  
Out[281]: 
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3]])
In [282]: np.repeat(z,4).reshape(4,4).T                                                                
Out[282]: 
array([[0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3]])

如果z为(1,n),那么我们可以在第一个轴上重复:

In [283]: np.repeat(z[None,:],4,0)                                                                     
Out[283]: 
array([[0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3]])

np.tile可能更易于使用。内部使用repeat