简体   繁体   中英

What is happening in this numpy problem where a numpy array is multiplied with different array?

import numpy as np

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

y = x [[0,1,2], [0,1,0]]   #:i did not understand this step,what is happening here?

print y

OUTPUT: [1 4 5]

When you're doing

x[a, b]

With a and b being arrays, you're specifying a series of indices to use. For instance, here you are saying "pick the 0th row, then the 1st, then the 2nd" and "pick the 0th column, the 1st and the 0th".

So, your resulting array is [x[0,0], x[1,1], x[2,0]]

This operation provides the sequence of the values with indexes taken from [0,1,2], [0,1,0] . First list relates to the first dimension, second one to the second one. So the coordinates will be (0, 0) , (1, 1) , (2, 0) and its values are 1, 4, 5 in the array x .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM