Generate numpy.ndarray
from iterable with automatic memory pre-allocation
#14479
Labels
33 - Question
Question about NumPy usage or development
Colab
I think a general use case of numpy.ndarrays is loading data from several files into one array, these could be pickles, images, or anything that can easily be loaded as a
numpy.ndarray
. In this case all these files would have the sameshape
anddtype
, and would have normal strides. Is there an elegant way to load such files, avoiding duplicate code and memory overhead?E.g. files created like this:
One way to open the files would be like this, but it would need twice the memory that is actually needed to first store the list and then create the array.
Alternatively:
But this has two places where the data is loaded, so duplicate code, which makes it non-transparent and easily introduces bugs.
Would it make sense for the numpy api to have a
fromiter_nd
for example:Existing functions fail in the following manor:
numpy.fromiter
Assumes numbers and creates something 1D,numpy.stack
,numpy.concatenate
, do not inspect the first item and use probably known generator length information to preallocate memory, and hence need twice the memory at peak.The text was updated successfully, but these errors were encountered: