NumPy Fast Food Tutorial (2) - Advanced Multidimensional Array

NumPy Fast Food Tutorial (2) - Advanced Multidimensional Array

In the previous lecture, we introduced the shape change and generation method of ndarray. In this section, we continue to discuss the use of multidimensional arrays.

access element

NumPy uses [] square brackets to access elements. If it is a one-dimensional array, use a subscript number, such as a[1], if it is a multidimensional array, use a tuple in square brackets, such as a[(2,3,4)]

example:

In [1]: import numpy as np

In [2]: a20 = np.linspace(1,100,27)

In [3]: a20
Out[3]: 
array([   1.        ,    4.80769231,    8.61538462,   12.42307692,
         16.23076923,   20.03846154,   23.84615385,   27.65384615,
         31.46153846,   35.26923077,   39.07692308,   42.88461538,
         46.69230769,   50.5       ,   54.30769231,   58.11538462,
         61.92307692,   65.73076923,   69.53846154,   73.34615385,
         77.15384615,   80.96153846,   84.76923077,   88.57692308,
         92.38461538,   96.19230769,  100.        ])

In [4]: a21 = a20.reshape(3,3,3)

In [5]: a21
Out[5]: 
array([[[   1.        ,    4.80769231,    8.61538462],
        [  12.42307692,   16.23076923,   20.03846154],
        [  23.84615385,   27.65384615,   31.46153846]],

       [[  35.26923077,   39.07692308,   42.88461538],
        [  46.69230769,   50.5       ,   54.30769231],
        [  58.11538462,   61.92307692,   65.73076923]],

       [[  69.53846154,   73.34615385,   77.15384615],
        [  80.96153846,   84.76923077,   88.57692308],
        [  92.38461538,   96.19230769,  100.        ]]])

In [6]: print(a21[(1,1,1)])
50.5

slice

Use a value to refer to it with square brackets, and if you want to refer to multiple values, consider making a slice. For example, s[1:3] is a list consisting of s[1] and s[2]:
Example:

In [10]: a22 = np.linspace(1,10,5)

In [11]: a22
Out[11]: array([  1.  ,   3.25,   5.5 ,   7.75,  10.  ])

In [12]: print(a22[2:4])
[ 5.5   7.75]

The same is true for multi-dimensional slicing. For example, we cut a small 2x2x2 cube from a 3x3x3 cube:

In [5]: a21
Out[5]: 
array([[[   1.        ,    4.80769231,    8.61538462],
        [  12.42307692,   16.23076923,   20.03846154],
        [  23.84615385,   27.65384615,   31.46153846]],

       [[  35.26923077,   39.07692308,   42.88461538],
        [  46.69230769,   50.5       ,   54.30769231],
        [  58.11538462,   61.92307692,   65.73076923]],

       [[  69.53846154,   73.34615385,   77.15384615],
        [  80.96153846,   84.76923077,   88.57692308],
        [  92.38461538,   96.19230769,  100.        ]]])

In [8]: slice1 = a21[1:3,1:3,1:3]

In [9]: slice1
Out[9]: 
array([[[  50.5       ,   54.30769231],
        [  61.92307692,   65.73076923]],

       [[  84.76923077,   88.57692308],
        [  96.19230769,  100.        ]]])

Note that the syntax for slicing doesn't use tuples, just slice directly in square brackets.

In addition, slices can be subscripted with negative numbers, -1 is the first element from the right. Both the leftmost and the rightmost can be omitted, for example, from 1 to the rightmost, it can be written as a[1:]

example:

In [11]: a22
Out[11]: array([  1.  ,   3.25,   5.5 ,   7.75,  10.  ])

In [12]: print(a22[2:4])
[ 5.5   7.75]

In [13]: a22[1:]
Out[13]: array([  3.25,   5.5 ,   7.75,  10.  ])

In [14]: a22[1:-1]
Out[14]: array([ 3.25,  5.5 ,  7.75])

Data Types of Multidimensional Arrays

In the previous study, we did not care about the data type, and multidimensional arrays can also be used. However, with types, arrays can be manipulated more conveniently and faster.
The methods of generating arrays that we have learned earlier can actually take a dtype parameter by default.
Commonly used type values ​​are int32, int64, uint32, uint64, float32, float64, complex64, complex128, etc. Because NumPy is a math library, precise types are beneficial for speeding up computations.

example:

In [18]: a23 = np.logspace(1,10,5,base=2,dtype=np.float64)

In [19]: a23
Out[19]: 
array([    2.        ,     9.51365692,    45.254834  ,   215.2694823 ,
        1024.        ])

Calculate every element of the array

Data is only valuable if it can be calculated. We learned how to generate arrays, access arrays, and the next step is how to perform calculations on arrays.
NumPy provides a large number of functions that operate on arrays. For example, X is an array, and np.sin(X) can perform a sin operation on each element in the array.
example:

In [20]: a24 = np.linspace(0, np.pi/2, 10, dtype=np.float64)

In [21]: a24
Out[21]: 
array([ 0.        ,  0.17453293,  0.34906585,  0.52359878,  0.6981317 ,
        0.87266463,  1.04719755,  1.22173048,  1.3962634 ,  1.57079633])

In [22]: a25 = np.sin(a24)

In [23]: a25
Out[23]: 
array([ 0.        ,  0.17364818,  0.34202014,  0.5       ,  0.64278761,
        0.76604444,  0.8660254 ,  0.93969262,  0.98480775,  1.        ])

This is a one-line, multi-line also works, let's look at an example:

In [24]: a26 = np.linspace(0, np.pi*2, 16, dtype=np.float32)

In [25]: a26
Out[25]: 
array([ 0.        ,  0.41887903,  0.83775806,  1.2566371 ,  1.67551613,
        2.09439516,  2.51327419,  2.93215322,  3.35103226,  3.76991129,
        4.18879032,  4.60766935,  5.02654839,  5.44542742,  5.86430645,
        6.28318548], dtype=float32)

In [27]: a27 = np.sin(a26.reshape(4,4))

In [28]: a27
Out[28]: 
array([[  0.00000000e+00,   4.06736642e-01,   7.43144870e-01,
          9.51056540e-01],
       [  9.94521916e-01,   8.66025388e-01,   5.87785184e-01,
          2.07911611e-01],
       [ -2.07911789e-01,  -5.87785363e-01,  -8.66025448e-01,
         -9.94521916e-01],
       [ -9.51056480e-01,  -7.43144751e-01,  -4.06736493e-01,
          1.74845553e-07]], dtype=float32)

Addition, subtraction, multiplication and division, exponentiation, and remainder are supported between arrays.

Example: multiply each element of an array by 2

In [31]: a28 = np.array([1,2,3,4]).reshape(2,-1)

In [32]: a28
Out[32]: 
array([[1, 2],
       [3, 4]])

In [33]: a28*2
Out[33]: 
array([[2, 4],
       [6, 8]])

Add between two arrays:

In [35]: a29 = np.ones((2,2))

In [36]: a29
Out[36]: 
array([[ 1.,  1.],
       [ 1.,  1.]])

In [37]: a28+a29
Out[37]: 
array([[ 2.,  3.],
       [ 4.,  5.]])

Not only arithmetic operations can be performed, but also size comparison operations can be performed on the entire array.

example:

In [38]: a29>a28
Out[38]: 
array([[False, False],
       [False, False]], dtype=bool)

Operations on summary classes

In addition to calculating each element, we can also aggregate these elements, such as summing sum, averaging mean, etc.

example:

In [40]: np.sum(a28)
Out[40]: 10

In [41]: np.mean(a28)
Out[41]: 2.5

matrix

In addition to the multidimensional arrays mentioned above, NumPy also provides the matrix class matrix. The default operations of matrix are matrix operations.
example:

In [45]: a30 = np.matrix(np.linspace(1,10,9,dtype=np.float64).reshape(3,-1))

In [46]: a30
Out[46]: 
matrix([[  1.   ,   2.125,   3.25 ],
        [  4.375,   5.5  ,   6.625],
        [  7.75 ,   8.875,  10.   ]])
In [48]: a31 = np.matrix(np.ones((3,3)))

In [49]: a31
Out[49]: 
matrix([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])

In [50]: np.dot(a30,a31)
Out[50]: 
matrix([[  6.375,   6.375,   6.375],
        [ 16.5  ,  16.5  ,  16.5  ],
        [ 26.625,  26.625,  26.625]])

The inverse of the matrix can be directly represented by X**-1.

example:

In [52]: a30 ** -1
Out[52]: 
matrix([[  9.38565300e+14,  -1.87713060e+15,   9.38565300e+14],
        [ -1.87713060e+15,   3.75426120e+15,  -1.87713060e+15],
        [  9.38565300e+14,  -1.87713060e+15,   9.38565300e+14]])

In [53]: a30
Out[53]: 
matrix([[  1.   ,   2.125,   3.25 ],
        [  4.375,   5.5  ,   6.625],
        [  7.75 ,   8.875,  10.   ]])

In [54]: a30 * (a30 ** -1)
Out[54]: 
matrix([[ 0.8125 , -0.125  ,  0.     ],
        [ 0.15625, -1.0625 ,  1.     ],
        [ 0.     ,  0.     ,  2.     ]])

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325696813&siteId=291194637
Recommended