# python - Indexing numpy array with index array of lower dim yields array of higher dim than both

```
a = np.zeros((5,4,3))
v = np.ones((5, 4), dtype=int)
data = a[v]
shp = data.shape
```

This code gives `shp==(5,4,4,3)`

I don't understand why. How can a larger array be output? makes no sense to me and would love an explanation.

## Gabriel-

2019-11-14

This is known as advanced indexing. Advanced indexing allows you to select arbitrary elements in the input array based on an N-dimensional index.

Let's use another example to make it clearer:

Say in this case

`a`

is:By indexing with an array of

`np.ones`

:You will simply be indexing

`a`

with`1`

along the first axis as many times as`v`

. Putting it in another way, when you do:You're indexing along the first axis, as no indexing is specified along the additional axes. It is the same as doing

`a[1, ...]`

, i.e taking a full slice along the remaining axes. Hence by indexing with a`2D`

array of ones, you will have the above`2D`

array`(5, 4)`

times stacked together, resulting in an ndarray of shape`(5, 4, 4, 3)`

. Or in other words,`a[1]`

, of shape`(4,3)`

, stacked`5*4=20`

times.Hence, in this case you'd be getting:

## Gavin-

2019-11-14

a is an array with 2 dimensions v is an array with 3 dimensions data is an array with 4 dimensions

As you access the 3 dimensional array with a 2 dimensional array you add one dimension to it.

Imagine for each point in 3d space you apply a 2d space, it would add another dimension to the 3d space. Creating a larger 4d space.

Thats just my guess tho, I'm really the opposite of expert so take my answer with a grain of salt haha!

## George-

2019-11-14

the value of

`v`

is:every single

`1`

indexes a complete "row" in`a`

, but every "element" in said "row" is a matrix. so every "row" in`v`

indexes a "row" of "matrix"es in`a`

. (does this make any sense to you..?)so you get 5 * 4

`1`

s, each is a 4*3 "matrix".if instead of

`zeroes`

you define`a`

as`a = np.arange(5*4*3).reshape((5, 4, 3))`

it might be easier to understand, because you get to see which parts of`a`

are being chosen:(output is pretty long, I don't want to paste it here)