[PYTHON] numpy non-basic techniques

numpy's Quick Start Tutorial has a Less basic (not very basic) technique, so I thought it would be interesting, so I will introduce it while understanding.

Array index using an array

The index of an array (the part i in a [i]) typically has a scalar value, but you can put an array here as well.

import numpy as np

a = np.arange(12)**2                       
i = np.array([1, 1, 3, 8, 5])             
a[i] #array([ 1,  1,  9, 64, 25], dtype=int32)

What is happening is as shown in the figure.

npLB1.png

Is it an image that the element of array i becomes an index and is extracted from the array of a using it?

The index can also be applied to a two-dimensional array. In that case, the output will also be two-dimensional.

j = np.array([[3, 6, 7], [5, 9, 7]])      
a[j]                                       
#array([[ 9, 36, 49],
#       [25, 81, 49]], dtype=int32)

In the tutorial, RGB is given as an application example, but it seems that it can also be used for one-hot expression used in machine learning.

one_hot = np.array([[0, 0, 0], 
                    [1, 0, 0], 
                    [0, 1, 0],      
                    [0, 0, 1]])
number = np.array([[0, 1, 2, 0], 
                  [0, 3, 2, 0]])
one_hot[number]
#array([[[0, 0, 0],
#        [1, 0, 0],
#        [0, 1, 0],
#        [0, 0, 0]],
#
#       [[0, 0, 0],
#        [0, 0, 1],
#        [0, 1, 0],
#        [0, 0, 0]]])

By the way, note that even if you set ``` number [one_hot [number]]` ``, it will not be restored.

You can also specify multiple arrays for the index.

a = np.arange(12).reshape(3,4)
#array([[ 0,  1,  2,  3],
#       [ 4,  5,  6,  7],
#       [ 8,  9, 10, 11]])
i = np.array([[0, 1],                     
              [1, 2]])
j = np.array([[2, 1],                     
              [3, 3]])
a[i, j] 
#array([[ 2,  5],
#      [ 7, 11]])

It is difficult to interpret how this is handled, but it is as follows.

npLB3.png

You can also specify a list as the index of the array.

a = np.arange(3,8)
a
#array([3, 4, 5, 6, 7])
a[[1,3,4]] = 0
a
#array([3, 0, 5, 0, 0])

Again, each element of the list is treated as an index of a.

You can use a list to assign (assign) all at once, but if the list has the same number, the assignment is repeated and the last value is assigned.

a = np.arange(3,8)
a
#array([3, 4, 5, 6, 7])
a[[1,1,4]] = [1,2,3]
a
#array([3, 2, 5, 6, 3])

Indexing using a Boolean array

You can create a Boolean array by giving the array a logical operator.

By using a Boolean array as an index, a one-dimensional array with the elements that become False is removed (note the shape of the array).

a = np.arange(-3,9).reshape(3,4)
a
#array([[-3, -2, -1,  0],
#       [ 1,  2,  3,  4],
#       [ 5,  6,  7,  8]])
b = a > 0
b                                   
#array([[False, False, False, False],
#       [ True,  True,  True,  True],
#       [ True,  True,  True,  True]])
a[b]                                       
#array([1, 2, 3, 4, 5, 6, 7, 8])

By assigning a Boolean array to an array as an index, you can assign to the elements that meet the conditions at once.

a[a<0] = 0                                  
a
#array([[0, 0, 0, 0],
#       [1, 2, 3, 4],
#       [5, 6, 7, 8]])

More complex extractions are possible by using the same Boolean array as the dimension (axis).

a = np.arange(12).reshape(3,4)
b1 = np.array([False,True,True])             
b2 = np.array([True,False,True,False])     

a[b1,:] #a[b1]But yes
#array([[ 4,  5,  6,  7],
#       [ 8,  9, 10, 11]])

a[:,b2] #a[b2]But yes
#array([[ 0,  2],
#       [ 4,  6],
#       [ 8, 10]])

a[b1,b2]                                 
#array([ 4, 10])

Looking at the figure, it looks like this.

npLB4.png

I wonder why a [b1, b2] is [4,10] instead of [[4,6], [8,10]]. The documentation also says a weird thing to do, so I wonder if I have to remember that.

Summary

So far, we have introduced array indexing using arrays and indexing using Boolean arrays. It's a tricky technique, but it should be useful if you master it.

The tutorial with the link at the beginning has other techniques (I omitted it because I could not understand it well), so if you can afford it, please read it.

Recommended Posts

numpy non-basic techniques
numpy practice 1
Numpy [Basic]
numpy part 1
NumPy basics
Numpy Memorandum_Matrix
numpy tips
About numpy
NumPy axis
Use Numpy
numpy part 2