numpy"延伸/追加" ndarray

时间:2017-12-20 09:08:49

标签: python arrays numpy append

问题:

我有一个numpy数组

tf = numpy.full((500, 4, 1, 2), [500, 1])

tf : 
array([[[[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]]],

   ..., 
   [[[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]]]])


tf.shape :
(500, 4, 1, 2)

考虑第一组: tf[0][0]这是:array([[ 500., 1.]])

我需要能够附加(到位)其他值[[100, 0.33], [1, 0.34], [15, 0.33]],以便最终结果看起来像(对每个元素执行此操作):

tf : 
array([[[[ 500.,  1.], [100.,  0.33], [1.,  0.34], [15.,   0.33]],

    [[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]]],

   ..., 
   [[[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]]]])

我尝试了numpy.concatenate((tf[0][0], [[100, 0.33]]), axis = 0)这会返回一个新的附加ndarray,但我无法将其分配回tf[0][0],因为它失败并出现以下错误。 ValueError: could not broadcast input array from shape (2,2) into shape (1,2)

有没有其他方法可以实现我想要的numpy?

=============================================== ===========

这样做的效率低list

# initialization
tf = [[[]] for i in xrange(500)]
for i in xrange(500):
    tf[i] = [[] for a in xrange(4)]
    for j in xrange(4):
        tf[i][j].append([500, 1.0])

# usage: (for any 0 < i < 500; 0 < j < 4 )

tf[i][j].append([100, 0.33])

但效率低(考虑到我需要这样做超过一百万次)

1 个答案:

答案 0 :(得分:1)

您的方法中的问题是每个元素的形状各不相同,因此您不能拥有固定的形状。但是,您可以将每个元素定义为类型object并实现您要执行的操作。

import numpy as np

tf = np.empty((500, 4, 1), dtype= object)

将产生

array([[[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]],

       ...,
       [[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]]], dtype=object)

现在将常量初始元素作为列表添加到每个数组元素中。你可能想在这里使用fill(),但是为每个数组元素分配一个对象,修改单个数组元素将改变整个数组。要进行初始化,您无法避免遍历整个数组。

for i,v in enumerate(tf):
    for j,w in enumerate(v):
        tf[i][j][0] = [[500.0,1.0]]

将产生

array([[[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       ...,
       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]]], dtype=object) 

现在您可以单独访问每个元素。根据您的喜好使用追加或扩展。

 tf[0][0][0].append([100,0.33])

将给出

array([[[list([[500.0, 1.0], [100, 0.33]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       ...,
       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]]], dtype=object)

只有初始化才需要遍历数组。