Home > database >  prefered method to get an element of numpy array
prefered method to get an element of numpy array

Time:09-01

Looking at a numpy array, i find two methods to get an element from the answers given to this question: How to call an element in a numpy array?.

There are the two methods: a[i][j] or a[i,j]

Here is a minimum reproducable example:

import numpy as np
a = np.arange(15).reshape(3, 5)
'''
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
'''


print(a[1][1])  # returns 6
print(a[1,1])   # return 6

Both appear to give the same result, so is one method better than the other ?

CodePudding user response:

You have the answer in the docs

Note that if one indexes a multidimensional array with fewer indices than dimensions, one gets a subdimensional array. For example:

x[0]
array([0, 1, 2, 3, 4])

That is, each index specified selects the array corresponding to the rest of the dimensions selected. In the above example, choosing 0 means that the remaining dimension of length 5 is being left unspecified, and that what is returned is an array of that dimensionality and size. It must be noted that the returned array is a view, i.e., it is not a copy of the original, but points to the same values in memory as does the original array. In this case, the 1-D array at the first position (0) is returned. So using a single index on the returned array, results in a single element being returned. That is:

x[0][2]
2

So note that x[0, 2] == x[0][2] though the second case is more inefficient as a new temporary array is created after the first index that is subsequently indexed by 2.

CodePudding user response:

Single element indexing

Single element indexing works exactly like that for other standard Python sequences. It is 0-based, and accepts negative indices for indexing from the end of the array.

x = np.arange(10)
x[2]
2
x[-2]
8

It is not necessary to separate each dimension’s index into its own set of square brackets.

x.shape = (2, 5)  # now x is 2-dimensional
x[1, 3]
8
x[1, -1]
9

Note that if one indexes a multidimensional array with fewer indices than dimensions, one gets a subdimensional array. For example:

x[0]
array([0, 1, 2, 3, 4])

That is, each index specified selects the array corresponding to the rest of the dimensions selected. In the above example, choosing 0 means that the remaining dimension of length 5 is being left unspecified, and that what is returned is an array of that dimensionality and size. It must be noted that the returned array is a view, i.e., it is not a copy of the original, but points to the same values in memory as does the original array. In this case, the 1-D array at the first position (0) is returned. So using a single index on the returned array, results in a single element being returned. That is:

x[0][2]
2

So note that x[0, 2] == x[0][2] though the second case is more inefficient as a new temporary array is created after the first index that is subsequently indexed by 2.

Note

NumPy uses C-order indexing. That means that the last index usually represents the most rapidly changing memory location, unlike Fortran or IDL, where the first index represents the most rapidly changing location in memory. This difference represents a great potential for confusion.

source

CodePudding user response:

a[1][1]

Returns the slice a[1] and then indexes that slice at [1]

a[1] -> array([5, 6, 7, 8, 9])
array([5, 6, 7, 8, 9])[1] -> 6

Whereas

a[1, 1]

Does the same end-result calculation but all within the numpy API in C, so is faster:

a[1][1]
360 ns ± 28.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

a[1, 1]
191 ns ± 2.76 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
  • Related