Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python array indexed with list but array dimensions are permuted

I try to index an array (has five dimensions) using a list. However, under certain situation, the array is permuted.

Say, a has the shape of (3,4,5,6,7), i.e.,

>>> a = np.zeros((3,4,5,6,7))
>>> a.shape
(3, 4, 5, 6, 7)

Using a list to index this array on the third dimension, it looks normal:

>>> a[:,:,[0,3],:,:].shape
(3, 4, 2, 6, 7)

However, if the array were indexed under the following situation, the third dimension is permuted to the leftmost:

>>> a[0,:,[0,1],:,:].shape
(2, 4, 6, 7)

Can anyone shed some light on it?

like image 637
Liang Guo Avatar asked Nov 07 '22 20:11

Liang Guo


1 Answers

Basic Slicing:-

Basic Slicing occurs when a slice object is used.Usually a slice object is constructed as array[(start:stop:step)]. Ellipsis and newaxis also comes under this.

Example:- 1D array

>>x=np.arange(10)    
>>x[2:10:3]
 array([2, 5, 8])

Example:- 2D array

>>>x = np.array([[1,2,3], [4,5,6]])
>>>x[1:2]
array([[4, 5, 6]])

Example:- 3D array

>>>x = np.array([[[1],[2],[3]], [[4],[5],[6]]])
>>> x[0:1]
array([[[1],
        [2],
        [3]]])

In the above example the number of slices(obj) given is less than that of the total number of dimension of the array. If the number of objects in the selection tuple is less than N, then it is assumed for any subsequent dimensions.

Advanced Slicing:-

Advanced indexing is triggered when the selection object, obj,

  1. is a non-tuple sequence object,
  2. an ndarray (of data type integer or bool),
  3. a tuple with at least one sequence object or ndarray (of data type integer or bool).

There are two types of advanced indexing: Integer and Boolean.

Integer Indexing:-

Integer array indexing allows selection of arbitrary items in the array based on their N-dimensional index. Each integer array represents a number of indexes into that dimension.

When the index consists of as many integer arrays as the array being indexed has dimensions, the indexing is straight forward, but different from slicing.

Example:-

>>a = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>a[[0,1,2],[0,1,1]]
array([1, 5, 8])

Array Visualization

The above example prints: a[0,0],a[1,0],a[2,1]

Remember:- So Integer Indexing maps between two indexes.

Now to your question:-

>>>a=np.array([3,4,5])
>>>a[0,:,[0,1]]

First Case:-

This is of the form x[arr1,:,arr2]. arr1 and arr2 are advanced indexes.We consider 0 also to be an advanced index.

If the advanced indexes are separated by a slice, Ellipsis or newaxis then the dimensions resulting from the advanced indexing operation come first in the result array, and the subspace dimensions after that.

This essentially means that the dimension of [0,1] comes first in the array. I am leaving off 0 as it has no dimension.

>>>a[0,:,[0,1]].shape
(2,4)

Second case:-

This is of the form x[:,:,arr1]. Here only arr1 is advanced index.

If the advanced indexes are all next to each other then the dimensions from the advanced indexing operations are inserted into the result array at the same spot as they were in the initial array.

This essentially means that the dimension of [0,1] comes at its respective position specified in the index of the array.

>>>a[0:1,:,[0,1]].shape
(1,4,2)

[0,1] has shape(2,) and since it occurs at third index it is inserted into 3rd index of the result array.

Any suggestions and improvements are Welcome.

Reference:-

  1. Numpy_Docs
like image 170
Justice_Lords Avatar answered Nov 14 '22 21:11

Justice_Lords