Index of numpy array

Posted by flunn on Wed, 09 Feb 2022 11:37:38 +0100

introduction

Array index refers to the use of square brackets ([]) to index a set of values. The most familiar indexing method is single element indexing. In addition, this paper will also introduce the index method of array slice, as well as index array, Boolean index group and structure index tool.

1. Single element index

Note: the starting position of the index is 0
When the array is a one-dimensional array:

>>> x = np.arange(10)	# array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x[2]
2
>>> x[-2]
8

When the array is multidimensional:
numpy arrays also support multidimensional indexes of multidimensional arrays.

>>> x.shape = (2,5) # Now x is a two-dimensional array
# array([[0, 1, 2, 3, 4],
#       [5, 6, 7, 8, 9]])
>>> x[1,3]	# The element corresponding to row 1 and column 3 of array x
8
>>> x[1,-1]	# The element corresponding to the first row and last column of array x
9
>>> x[0]	#If the index is a multidimensional array with fewer dimensions than the index, a sub dimensional array will be obtained
array([0, 1, 2, 3, 4])

2. Array slicing

Like lists and tuples, arrays can be sliced to index elements. An example is given below:

>>> x = np.arange(10)
>>> x[2:5]
array([2, 3, 4])
>>> x[:-7]
array([0, 1, 2])
>>> x[1:7:2]
array([1, 3, 5])
>>> y = np.arange(35).reshape(5,7)
>>> y[1:5:2,::3]
array([[ 7, 10, 13],
       [21, 24, 27]])

[] first: the first is the starting value of the index, the first is the ending value of the index, and the second is the step size of the index. Take x[1:7:2] as an example for analysis: the index start position is 1, the end position is 7 (excluding the elements corresponding to index 7), and the step size is 2. Therefore, the index result of x[1:7:2] is [1, 3, 5].
Take x[2:5] as an example for analysis: the start position of the index is 2 and the end position is 5 (excluding the elements corresponding to index 7). If the step size is omitted, it is 1 by default. Therefore, the index result of x[2:5] is [2, 3, 4].
Take y[1:5:2,::3] as an example: y is a multi-dimensional array. Before the comma is the index of the first dimension of the array, and after the comma is the index of the second dimension of the array. The index method of each dimension of the array is the same as that of one-dimensional array. Where, 1:5:2 means that the start index is 1, the end index is 5 (excluding the elements corresponding to index 5), and the step size is 2. The result of the fourth row of the [y: 2] index is the second row of the array. Namely

>>> y[1:5:2]
array([[ 7,  8,  9, 10, 11, 12, 13],
       [21, 22, 23, 24, 25, 26, 27]])

[:: 3] the step size representing the 2D index is 3, and there is no restriction on the starting and ending index positions. That is, take the first, fourth and seventh columns of y[1:5:2]. therefore

>>> y[1:5:2,::3]
array([[ 7, 10, 13],
       [21, 24, 27]])

3. Index array

numpy array also supports array index array, that is, [] can be an array. The following is an example:

>>> x = np.arange(10,1,-1)	# This time - 1 means the step size is - 1, i.e. in reverse order
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x[np.array([3, 3, 1, 8])]	# Index the 4th, 4th, 2nd and 9th elements of array x
array([7, 7, 9, 2])
>>> x[np.array([3,3,-3,8])]
array([7, 7, 4, 2])

NP in [] of x Array ([3, 3, 1, 8]), that is, the index array composed of values 3, 3, 1 and 8. Generally speaking, when using an indexed array, an array with the same shape as the indexed array is returned. Here is an example of using multidimensional index array:

>>> x[np.array([[1,1],[2,3]])]
array([[9, 9],
       [8, 7]])

4. Boolean index group

The shape of the Boolean array used as an index must be the same as the initial size of the array to be indexed. The Boolean index result is a one-dimensional array, which contains all the elements in the index array corresponding to all the real elements in the Boolean array. An example is given below for specific analysis:

>>> y = np.arange(35).reshape(5,7)
>>> b = y>20
>>> b
array([[False, False, False, False, False, False, False],
       [False, False, False, False, False, False, False],
       [False, False, False, False, False, False, False],
       [ True,  True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True,  True]], dtype=bool)
>>> y[b]
array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34])

Array y is an array with 5 rows and 7 columns, b is a Boolean array, the index position corresponding to Y > 20 is True, and others are False. Index array b is used as the index of group y and returns a one-dimensional array. Where the array element is the element of array y corresponding to the True index in index array b.

5. Structure index tool

np.newaxis is a commonly used numpy function in deep learning. NP. Can be used in array indexes Newaxis object to add a new dimension of size 1.

>>> y
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13],
       [14, 15, 16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25, 26, 27],
       [28, 29, 30, 31, 32, 33, 34]])
>>> y.shape
(5, 7)
>>> y[:,np.newaxis,:]
array([[[ 0,  1,  2,  3,  4,  5,  6]],

       [[ 7,  8,  9, 10, 11, 12, 13]],

       [[14, 15, 16, 17, 18, 19, 20]],

       [[21, 22, 23, 24, 25, 26, 27]],

       [[28, 29, 30, 31, 32, 33, 34]]])
>>> y[:,np.newaxis,:].shape
(5, 1, 7)
>>> y[np.newaxis,:,:]
array([[[ 0,  1,  2,  3,  4,  5,  6],
        [ 7,  8,  9, 10, 11, 12, 13],
        [14, 15, 16, 17, 18, 19, 20],
        [21, 22, 23, 24, 25, 26, 27],
        [28, 29, 30, 31, 32, 33, 34]]])
>>> y[np.newaxis,:,:].shape
(1, 5, 7)
>>> y[:,:,np.newaxis]
array([[[ 0],
        [ 1],
        [ 2],
        [ 3],
        [ 4],
        [ 5],
        [ 6]],

       [[ 7],
        [ 8],
        [ 9],
        [10],
        [11],
        [12],
        [13]],

       [[14],
        [15],
        [16],
        [17],
        [18],
        [19],
        [20]],

       [[21],
        [22],
        [23],
        [24],
        [25],
        [26],
        [27]],

       [[28],
        [29],
        [30],
        [31],
        [32],
        [33],
        [34]]])
>>> y[:,:,np.newaxis].shape
(5, 7, 1)

As shown above, there are no new elements in the array, but dimensions are added. But what's the use?
This makes it easy to combine two arrays, otherwise explicit reshaping will be required. For example:

>>> x = np.arange(5)
>>> x
array([0, 1, 2, 3, 4])
>>> x[:,np.newaxis]
array([[0],
       [1],
       [2],
       [3],
       [4]])
>>> x[np.newaxis,:]
array([[0, 1, 2, 3, 4]])
>>> x[:,np.newaxis] + x[np.newaxis,:]
array([[0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [4, 5, 6, 7, 8]])

Topics: Deep Learning numpy