[Numpy-discussion] 3D array slicing bug?

Michael Himes mhimes at knights.ucf.edu
Wed Mar 21 16:40:55 EDT 2018


Hi,


I have discovered what I believe is a bug with array slicing involving 3D (and higher) dimension arrays. When slicing a 3D array by a single value for axis 0, all values for axis 1, and a list to slice axis 2, the dimensionality of the resulting 2D array is flipped. However, slicing more than a single index for axis 0 or performing the slicing in two steps results in the correct dimensionality. Below is a quick example to demonstrate this behavior.


import numpy as np


arr = np.arange(54).reshape(2, 3, 9)

list = [0, 2, 4, 5, 8]

print(arr.shape)  # (2, 3, 9)

print(arr[0, :, list].shape) # (5, 3) -- but it should be (3, 5)?

print(arr[0][:, list].shape) # (3, 5), as expected

print(arr[0:1, :, list].shape) # (1, 3, 5), as expected


This behavior carries over to 4D arrays as well, where the axis sliced with a list becomes the 0th axis regardless of order. Below demonstrates that.


arr2 = np.arange(324).reshape(2, 3, 6, 9)

print(arr2[0, :, :, list].shape) # (5, 3, 6), but I expect (3, 6, 5)


arr3 = np.arange(324).reshape(2, 3, 9, 6)

print(arr3[0, :, list].shape) # (5, 3, 6), expected (3, 5, 6)

print(arr3[0, :, list, :].shape) # same as above


Can anyone explain this behavior, or is this a bug?


Best,

Michael

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180321/143bfed0/attachment.html>


More information about the NumPy-Discussion mailing list