user3676943
user3676943

Reputation: 923

NumPy indexing using List?

SOF,

I noticed an interesting NumPy demo in this URL:

http://cs231n.github.io/python-numpy-tutorial/

I see this:

import numpy as np

a = np.array([[1,2], [3, 4], [5, 6]])

# An example of integer array indexing.
# The returned array will have shape (3,) and 

print( a[[0, 1, 2], [0, 1, 0]] )
# Prints "[1 4 5]"

I understand using integers as index arguments:

a[1,1]

and this syntax:

a[0:2,:]

Generally, If I use a list as index syntax, what does that mean?

Specifically, I do not understand why:

print( a[[0, 1, 2], [0, 1, 0]] )
# Prints "[1 4 5]"

Upvotes: 2

Views: 2637

Answers (1)

Freek Wiekmeijer
Freek Wiekmeijer

Reputation: 4940

The last statement will print (in matrix notation) a(0,0), a(1,1) and a(2,0). In python notation that's a[0][0], a[1][1] and a[2][0].

The first index list contains the indices for the first axis (matrix notation: row index), the second list contains the indices for the second axis (column index).

Upvotes: 2

Related Questions