I have a np.array data (28,8,20) form, and I only need some records from it, so I take a snippet:
In [41]: index = np.array([ 5, 6, 7, 8, 9, 10, 11, 17, 18, 19]) In [42]: extract = data[:,:,index] In [43]: extract.shape Out[43]: (28, 8, 10)
So far so good, everything is as it should be. But now I wand to see only the first two entries in the last index for the first row:
In [45]: extract[0,:,np.array([0,1])].shape Out[45]: (2, 8)
Wait, this should be (8.2). He switched the indexes around, although it was not the last time I sliced! In my opinion, the following should act the same:
In [46]: extract[0,:,:2].shape Out[46]: (8, 2)
... but that gives me exactly what I wanted! As long as I have a 3D array, both methods seem to be equivalent:
In [47]: extract[:,:,np.array([0,1])].shape Out[47]: (28, 8, 2) In [48]: extract[:,:,:2].shape Out[48]: (28, 8, 2)
So what should I do if I want not only the first two entries, but also the wrong list? I could, of course, transpose the matrix after the operation, but it seems very controversial. The best solution to my problem is this (although it may be more elegant):
In [64]: extract[0][:,[0,1]].shape Out[64]: (8, 2)
Which brings us to the actual
Question:
I wonder what is the reason for this behavior? Anyone who decided that this was how he should work probably knew more about programming than I did, and thought it was so consistent with what I was missing. And I will most likely be banging my head about it if I donβt have a way to figure it out.