'Reading 3D numpy array

I have a dataset which comprises of the binary data of pixelated 50x50 images. The array shape is (50, 50, 90245). I want to reach 50x50 pixels of each of the 90245 images. How can I slice the array?



Solution 1:[1]

If data is the variable storing the image data, and i is the index of the image you want to access, then you can do:

data[:,:,i]

to get the desired image data.

Solution 2:[2]

If data is the variable storing the image data, and i is the index of the image you want to access, then you can do as @BrokenBenchmark suggested. In case you want a (50,50,1) 3D array as the output, you could do:

data[:,:,i:i+1]

to get the image as a 3D array.

Edit1: If you reshaped your data matrix to be of shape (90245,50,50), you can get the ith image by doing data[i,:,:] or just data[i] to get a (50,50) image. Similarly, to get a (1,50,50) image, you could do data[i:i+1,:,:] or just data[i:i+1].

Edit2: To reshape the array, you could use the swapaxes() function in numpy.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BrokenBenchmark
Solution 2