## Issue

If a is numpy array of shape (5,3), b is of shape (2,2) and c is of shape (2,2), what is the shape of a[b,c]?

Can anyone explain this to me with an example. I’ve read the docs but still I am not able to understand how it works.

## Solution

Just for the purpose of expounding the concept of advanced indexing, here is a contrived example:

```
# input arrays
In [22]: a
Out[22]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
In [23]: b
Out[23]:
array([[0, 1],
[2, 3]])
In [24]: c
Out[24]:
array([[0, 1],
[2, 2]])
# advanced indexing
In [25]: a[b, c]
Out[25]:
array([[ 0, 4],
[ 8, 11]])
```

By the expression `a[b, c]`

, we are using the arrays `b`

and `c`

to selectively pull out elements from the array `a`

.

To interpret the output of `a[b, c]`

:

```
# b # c # 2D indices
[[0, 1], [[0, 1] ---> (0,0) (1,1)
[2, 3]] [2, 2]] ---> (2,2) (3,2)
```

The 2D indices would simply be applied to the array `a`

and the corresponding elements would be returned as array in the result of `a[b, c]`

```
a[(0,0)] --> 0
a[(1,1)] --> 4
a[(2,2)] --> 8
a[(3,2)] --> 11
```

The above elements are returned as a 2D array since the arrays `b`

and `c`

are 2D arrays themselves.

Also, please note that advanced indexing always returns a copy.

```
In [27]: (a[b, c]).flags.owndata
Out[27]: True
```

However, an *assignment* operation using *advanced* indexing will alter the original array (in-place). But, this behaviour is also dependent on two factors:

- whether your indexing operation is pure (only
*advanced*indexing) or mixed (a combination of*advanced & simple*indexing) - in case of mixed indexing, the order in which they are applied.

See: Views and copies confusion with NumPy arrays when combining index operations

Answered By – kmario23

Answer Checked By – Jay B. (BugsFixing Admin)