0

I'm using Python 3.7.7. to work with Numpy.

I have absolutely no idea about Numpy array's indexing and I want to loop over the elements of a numpy array with shape (960, 2, 200, 200, 1). In words this array is:

960 pairs of images of 200x200x1.

I know how to get the first element in the pair, and the second element:

first_pair = dataset[:, 0, :]
second_pair = dataset[:, 1, :]

But now I want to loop all the elements in first_pair and I don't know how to do it.

first_pair and second_pair shapes are (960, 200, 200, 1).

I have this code to access an element in the first_pair:

img = dataset[:, 0, :][28][:, :, 0]

I want to get all the images (arrays with shape (200, 200, 1) in first_pair array. Something like:

for image in first_pair:
    # ...

But I'm confuse because I don't understand what [:, :, 0] means and I don't know if it is necessary to loop the elements.

How can I loop the elements (images) in first_pair?

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • I am not sure if I understand what you want to achieve. Currently I would says you need something like `for elem in first_pair: ...' because thats what a loop is. Can you specify more, what exactly you want to achieve? – MichaelJanz Aug 24 '20 at 07:46
  • @VansFannel you should learn more about numpy and slicing numpy arrays if you plan to use it more, this may help https://www.pythoninformer.com/python-libraries/numpy/index-and-slice/ – S4rt-H4K Aug 24 '20 at 07:49
  • @MichaelJanz I have updated the question with more details. – VansFannel Aug 24 '20 at 07:52
  • Read the manual section on basic slicing (https://numpy.org/doc/stable/reference/arrays.indexing.html#basic-slicing-and-indexing) or a similar tutorial from another website. Yeah it's boring but it'll answer your question about what `[:, :, 0]` means and more. – BatWannaBe Aug 24 '20 at 08:25
  • One more little thing: you've been calling basic slices of the main array "elements" but most people consider the scalar values as elements.You should call them views, which are numpy arrays looking at the same data as the main array but with different strides (https://stackoverflow.com/questions/53097952/how-to-understand-numpy-strides-for-layman). If you don't want to access the same data as the main array, then copy a view with `numpy.copy(some_view)`; this takes longer because you're actually copying elements to another block of memory. – BatWannaBe Aug 24 '20 at 08:50
  • @BatWannaBe Thanks but I have found a solution by myself. – VansFannel Aug 24 '20 at 13:10

1 Answers1

0

I've been testing the arrays to inspect their shapes:

ds is a numpy array with shape (960, 2, 200, 200, 1).

ds[:, 0, :] has shape (960, 200, 200, 1).

ds[:, 0, :][28][:, :, 0] has shape (200, 200) and

ds[:, 0, :][28] has shape (200, 200, 1).

So [:, :, 0] returns an array with one less dimension (or remove the last dimension).

To loop all the images in ds[:, 0, :] or in ds[:, 1, :] you have to do:

for x in ds[:, 0, :]:
    print(x.shape)
VansFannel
  • 45,055
  • 107
  • 359
  • 626