According to docs numpy's default behaviour is to index arrays first by rows then by columns:
a = numpy.arange(6).reshape(3,2)
[[0 1]
[2 3]
[4 5]]
print a[0][1] # is 1
I want to index the array using the geometrically oriented-convention a[x][y]
, as in x-axis and y-axis. How can I change the indexing order without modifying the array's shape so that a[0][1]
would return 2?
Indexing a Two-dimensional Array To access elements in this array, use two indices. One for the row and the other for the column. Note that both the column and the row indices start with 0. So if I need to access the value '10,' use the index '3' for the row and index '1' for the column.
Two-dimensional (2D) arrays are indexed by two subscripts, one for the row and one for the column. Each element in the 2D array must by the same type, either a primitive type or object type.
Indexing can be done in numpy by using an array as an index. In case of slice, a view or shallow copy of the array is returned but in index array a copy of the original array is returned. Numpy arrays can be indexed with other arrays or any other sequence with the exception of tuples.
Indexing in NumPy is a reasonably fast operation.
You can write a.T[0,1]
to use indices of the transpose of the array, which are the other way around in 2D.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With