1

Can anyone exmplain me how this type of indexing is work with numpy arrays?

n=10
l=[0,1,1,1,1,0,0,1,0,1]
array=np.zeros((n,2))
array[np.arange(n),l] = k

I know what is the result. But I want to know how the last line of code operates(explanation)?

TassosK
  • 293
  • 3
  • 16
  • 4
    You can index with tuples of arrays of the same length, and it will take the indexes elementwise from those index arrays. For e.g., `a[(1, 2, 3, 4), (5, 6, 7, 8)]` will index the array `a` at (1, 5), (2, 6), (3, 7), and (4, 8). See the numpy [indexing documentation](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html). – alkasm Mar 11 '19 at 17:16
  • 2
    it is invalid code, because you have 2 columns [0,1] and you are indexing columns with invalid index 2 – mujjiga Mar 11 '19 at 17:17
  • @mujjiga yes, you're right I fixed it. Thanks for the response. – TassosK Mar 11 '19 at 17:31
  • @AlexanderReynolds thanks for the quick explanation, I got it. – TassosK Mar 11 '19 at 17:33

0 Answers0