2

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?

3
  • 2
    np.dstack(arrays) Commented Dec 12, 2013 at 15:57
  • 2
    With numpy, it is usually a good practice to define a zeros array and fill it with content. Commented Dec 12, 2013 at 16:01
  • Wait, dstack orders the axes differently than I thought. You might need to reorder them with slicing or use something else. Commented Dec 12, 2013 at 16:11

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.