Indexing of ndarrays
Array indexing refers to the way of accessing a particular array element or elements. In NumPy, all ndarray indices are zero-based—that is, the first item of an array has index 0
. Negative indices are understood as counting from the end of the array.
Direct access to an ndarray's element
Direct access to a single ndarray's element is one of the most used forms of access.
The following code builds a 3 x 3 random-valued ndarray for our use:
arr = np.random.randn(3,3); arr
The arr
ndarray has the following elements:
array([[-0.04113926, -0.273338  , -1.05294723],        [ 1.65004669, -0.09589629,  0.15586867],        [ 0.39533427,  1.47193681,  0.32148741]])
We can index the first element with integer index 0
, as follows:
arr[0]
This gives us the first row of the arr
ndarray, as follows:
array([-0.04113926, ...