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

Dharhas Pothina Dharhas.Pothina@twdb.state.tx...
Wed May 21 13:14:56 CDT 2008


Thanks Anne, I don't know the final size so appending to a list is probably best. The array(big_list) seems to be working but for my future reference, what does the following do?

big.append(read_3D_chunk()[np.newaxis,...])

- dharhas

>>> "Anne Archibald" <peridot.faceted@gmail.com> 5/21/2008 12:58 PM >>>
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
_______________________________________________
SciPy-user mailing list
SciPy-user@scipy.org 
http://projects.scipy.org/mailman/listinfo/scipy-user



More information about the SciPy-user mailing list