Jorge Zapata
Jorge Zapata

Reputation: 2336

Import array files with numpy

I have a directory with multiple .npy files (numpy arrays), each file has a 2 dimensional array (same width and height). I need to read all files and generate a 3 dimensional array containing all arrays in directory, the result shape should be something like (# of files, width, height).

My code so far:

import os
import numpy

for file in os.listdir(os.getcwd()):
   result = numpy.load(file) #Obviously this doen't work

But I just simply don't know how to generate the result array. Should I first create a zeros array and then fill it? Can I make this on the fly? Can you help me please?

Upvotes: 2

Views: 677

Answers (1)

Bence
Bence

Reputation: 313

If you know how many there are and what the size is, create an empty array first. (An empty array is faster, because you don't have to zero all elements.) Something like this:

# Allocate empty array.
bigarray = numpy.empty([width, height, len(filenames)]);
# Load files.
for i in range(len(filenames)):
    bigarray[:,:,i] = numpy.load(filenames[i]);

If you do not know the dimensions in advance, use numpy.append. This is fairly slow, because it has to allocate a new chunck of memory and copy data in each iteration. Try this:

# Load first array.
bigarray = numpy.load(filenames[0]);
# Add a new axis to make it 3D.
bigarray = bigarray[numpy.newaxis,...];
# Load rest of arrays.
for i in range(1,len(filenames)):
    bigarray = numpy.append(bigarray, numpy.load(filenames[i])[numpy.newaxis,...], axis=0);

Upvotes: 2

Related Questions