简体   繁体   中英

Proper way to “append” multi-dimensional numpy arrays?

I have a 4d array x , for which I want to loop through the first axis, modify that 3d array, and add this modified array to a new 4d array y .

I am currently doing something like:

xmod = modify(x[0, :, :, :])
y = xmod.reshape(1, x.shape[1], x.shape[2], x.shape[3])
for i in range(1, x.shape[0]):
    xmod = modify(x[i, :, :, :])
    y = np.vstack((y, xmod))

I am guessing there is amuch cleaner to do this. How?

If you must act on x one submatrix at a time you could do:

y = np.zeros_like(x)
for i in range(x.shape[0]):
    y[i,...] = modify(x[i,...])

eg

In [595]: x=np.arange(24).reshape(4,3,2)
In [596]: y=np.zeros_like(x)
In [597]: for i in range(x.shape[0]):
   .....:     y[i,...]=x[i,...]*2
   .....:     
In [598]: y
Out[598]: 
array([[[ 0,  2],
        [ 4,  6],
        ...
        [40, 42],
        [44, 46]]])

appending to lists is generally better than repeatedly 'appending' to arrays:

In [599]: y=[]
In [600]: for row in x:
   .....:     y.append(row*2)
   .....: 
In [601]: y=np.array(y)

for very large cases you could see if vstack (or concatenate axis=0) is faster. But you have to explicitly add a beginning dimension to the arrays.

In [615]: y=[]
In [616]: for row in x:
    y.append((row*2)[None,:])
   .....:     
In [617]: np.vstack(y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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