“This is the seventh day of my participation in the First Challenge 2022.

preface

Following the previous numpy basic Indexing and Slicing installment, Numpy uses the standard Python X [obj] syntax for indexing.

According to different obJ object types, indexes are mainly divided into basic indexes and advanced indexes.

In single-element indexes, the index returns a copy of the original array for a one-dimensional array. For an n-dimensional array, the view of the original array is returned when there are fewer index objects than the number of dimensions of the array.

In the basic index, we can use start:stop:step to slice the operation. The basic operation logic and Python list, string, etc. The positive index and negative index, when ::, is the value of all array elements.

Also, during the slicing process, make sure that the size of the value set is consistent with the original array shape.

However, the start:stop:step slicing approach is not suitable for advanced indexes.

I believe you must be full of curiosity about advanced index, which next we will start to learn advanced index related concepts, Let’s go~~

1. Advanced indexes

  • What is an advanced index?

    The numpy module index is indexed using the Python standard x[obj].

    • Trigger advanced index conditions

      • When OBj is a non-tuple sequence
      • The data element type of Ndarray is Boolean
      • A tuple of data type integer or Boolean

    Advanced indexes always return a copy of the data, as opposed to the view returned by basic slicing.

2. Integer array index

2.1 What is an integer array index?

An integer array index is a query for multiple arbitrary data elements indexed by an NDARray object whose elements are of type integer. Like basic indexes and slicing, indexed arrays support both positive and negative indexes.Copy the code
  • The integer array index is an NDARray object

    (an NDARray (of data type INTEGER or bool)

    >>> import numpy as np
    >>> intarr = np.arange(12)
    >>> intarr
    array([ 0.1.2.3.4.5.6.7.8.9.10.11])
    >>> intarr[np.array([3.5.7.9])]
    array([3.5.7.9) > > >Copy the code

    If the object in the index is not Ndarray, TypeError is raised

    >>> intarr([3.5.7.9])
    Traceback (most recent call last):
      File "<stdin>", line 1.in <module>
    TypeError: 'numpy.ndarray' object is not callable
    Copy the code
  • Integer arrays are indexed to list objects

    a non-tuple sequence object

    >>> intarr[[3.5.7.9]]
    array([3.5.7.9])
    Copy the code
  • The integer array index is a tuple object containing a sequence

    >>> intarr[([3.5.7.9])]
    array([3.5.7.9])
    
    Copy the code

When the index is negative, it works the same way as slicing, starting with the unbit of the array

` ` ` python > > > intarr array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) > > > intarr [([1, 2, 3, 1])] array ([1, 2, 9, 11]) > > > ` ` `Copy the code

IndexError is reported when the index timeout range is set

```python
>>> intarr[([13])]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index 13 is out of bounds for axis 0 with size 12
>>>
```
Copy the code

2.2 Integer array index of multidimensional arrays

  • When there are as many multidimensional arrays as there are index dimensions, numpy broadcasts and iterates internally, as follows.

    >>> intarr2 = np.array([[2.3], [4.5], [8.9]])
    >>> intarr2
    array([[2.3],
           [4.5],
           [8.9]])
    >>> intarr2[[1, -1]]
    array([[4.5],
           [8.9]])
    Copy the code

Similarly, numpy indexError is raised when the indexed array value is greater than the original array value

>>> intarr2[[3.1]]
Traceback (most recent call last):
  File "<stdin>", line 1.in <module>
IndexError: index 3 is out of bounds for axis 0 with size 3
>>>

Copy the code

2.3 Application of integer array of multi-dimensional array

  • Multidimensional array integer index general operation

    The official iteration formula of advanced index is given

    result[i_1, ..., i_M] == x[ind_1[i_1, ..., i_M], ind_2[i_1, ..., i_M],
                              ..., ind_N[i_1, ..., i_M]]
    Copy the code

    When the shape of the indexed array is the same as that of the original array, within the legal range in each dimension, the result is the same as the shape of the indexed array. Intarr3 [np.array([1,2,3]),np.array([1,3,2])] = intarr3[1,1] intarr3[2,3] intarr3[3,2]

    >>> intarr3 = np.arange(20).reshape(5.4)
    >>> intarr3
    array([[ 0.1.2.3],
           [ 4.5.6.7],
           [ 8.9.10.11],
           [12.13.4.1 15],
           [16.17.18.19]])
    >>> intarr3[np.array([1.2.3]),np.array([1.3.2])]
    array([ 5.11.14) > > >Copy the code

When numpy broadcast rules are triggered, the output shape will be the same as the original array shape. Otherwise, the system will report an exception with inconsistent shape

Intarr3 [np.array([1,2,3]),np.array([1,3])] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (2,) >>> ```Copy the code
  • The NUMpy broadcast mechanism allows overlap with other indexes

    Intarr3 [np.array([1,2,3]),1]= intarr3[1,1]intarr3[2,1]intarr3[3,1]

    >>> intarr3
    array([[ 0.1.2.3],
           [ 4.5.6.7],
           [ 8.9.10.11],
           [12.13.14.15],
           [16.17.18.19]])
    >>> intarr3[np.array([1.2.3]),1]
    array([ 5.9.13) > > >Copy the code

Boolean index

The Numpy module also triggers advanced indexing when the index object is of a Boolean type.

A Boolean index is a Boolean operation to retrieve specified element data

The most common use of Boolean indexes is to filter the required element values

  >>> intarr3
array([[ 0.1.2.3],
     [ 4.5.6.7],
     [ 8.9.10.11],
     [12.13.14.15],
     [16.17.18.19]])
>>> intarr3[[intarr3>5]]
array([ 6.7.8.9.10.11.12.13.14.15.16.17.18.19) > > >Copy the code

4. Fancy indexes

Fancy index is an extension of integer array index, specifically for multiple index arrays, call Np.ix_ to achieve broadcast.

>>> intarr3
array([[ 0.1.2.3],
       [ 4.5.6.7],
       [ 8.9.10.11],
       [12.13.14.15],
       [16.17.18.19]])
>>> intarr3[[intarr3>5]]
array([ 6.7.8.9.10.11.12.13.14.15.16.17.18.19])
>>> intarr3[[3.4.4.1]]
array([[12.13.14.15],
       [16.17.18.19],
       [16.17.18.19],
       [ 4.5.6.7]])
>>> intarr3[[-3, -1, -1, -1]]
array([[ 8.9.10.11],
       [16.17.18.19],
       [16.17.18.19],
       [16.17.18.19]])
Copy the code

Similar to slicing, advanced indexes can call the np.ix_ method to implement the broadcast mechanism

>>> intarr3[np.ix_([-3, -1, -1, -1], [0.1.2.3])]
array([[ 8.9.10.11],
       [16.17.18.19],
       [16.17.18.19],
       [16.17.18.19]]) > > >Copy the code

conclusion

This issue, numpy module on the advanced index two types of integer array index and Boolean index related operations, learn.

Advanced indexes can help us implement more functions, such as iteration and broadcasting, than basic indexes

That’s the content of this episode. Please give us your thumbs up and comments. See you next time