[SciPy-user] Generating a 4D array from a set of 3D arrays

Anne Archibald peridot.faceted@gmail....
Wed May 21 12:58:28 CDT 2008


2008/5/21 Dharhas Pothina <Dharhas.Pothina@twdb.state.tx.us>:

> I'm trying to read 3 dimensional time series data from a file and store it in a numpy array so I can analyze the data. I'm having problems working out how to append the 3D array from each timestep to make a 4D array. I worked out that I could make a list of 3D arrays but if I do that I'm having issues slicing it the way I need to.

As a general rule, growing arrays should be avoided; it's not a very
efficient process.

If you know the final shape of the 4D array, the best thing to do is
to allocate it all at once, then assign to it:

big = np.zeros((a,b,c,d))/0.

for i in range(a):
    big[i,...] = read_3D_chunk()

If you don't know the final shape, it's probably best to load the lot
into a python list (which support efficient appending), then convert
it to an array. array() should normally do this:

big_list = []
for i in range(a):
    big.append(read_3D_chunk())
big = np.array(big_list)

But array() is sometimes too clever for its own good, so I would be
tempted to use concatenate along a new axis:

big_list = []
for i in range(a):
    big.append(read_3D_chunk()[np.newaxis,...])
big = np.concatenate(tuple(big_list))

Good luck,
Anne


More information about the SciPy-user mailing list