Let’s review the relevant knowledge from the previous article and learn more about the APIs of the Numpy library
I. Numpy
II Introduction to NumPy
- NumPy’s homepage
- NumPy stands for “Numerical Python”, is a library specializing in processing, calculating vectors, matrices.
- NumPy is often used with SciPy and Matplotlib to replace the extremely expensive MatLab.
- Written in Python and C, so performance is good.
I.2 APIs
1. NumPy Arrays – NumPy Array
2. Access through index and cut Array – Array Indexing and Array Slicing
a. Indexing and Slicing
Indexing and slicing are similar to lists in Python.
There are lots of ways to do this, but I just show you the most understandable and practical ways.
You can refer to the details here
- Basic indexing
import numpy as np
2-dimensional arrays. We can think of a 2-dimensional matrix.
NumPy’s multi-dimensional matrix simulation is based on the use of the list inside the list (nested list) so accessing the elements in this array is exactly the same as we have done in Python.
1 2 3 4 | Input: number_2D = np.array([[4,5,6], [7, 8, 9]]) number_2D |
1 2 3 4 | Output: array([[4, 5, 6], [7, 8, 9]]) |
1 2 3 | Input: number_2D.shape |
1 2 3 | Output: (2, 3) |
Counter item
Access the first row of the matrix
1 2 3 | Input: number_2D[0] |
1 2 3 | Output: array([4, 5, 6]) |
Access the last row of the matrix
1 2 3 | Input: number_2D[-1] |
1 2 3 | Output: array([7, 8, 9]) |
Access column
Access the first column of the matrix: this is equivalent to accessing all rows and taking the first element.
1 2 3 | Input: number_2D[:, 0] # -> Dấu : biển hiện việc duyệt qua tất cả các hàng, qua mỗi hàng ta lấy phần tử đầu tiên |
1 2 3 | Output: array([4, 7]) |
Access the last column of the matrix
1 2 3 | Input: number_2D[:, -1] |
1 2 3 | Output: array([6, 9]) |
Access the middle column of the matrix
1 2 3 | Input: number_2D[:, 1] |
1 2 3 | Output: array([5, 8]) |
Access arrays in matrices
1 2 3 4 5 6 7 8 9 | Input: awesome = np.array([ [1, 2, 3, 4, 5, 6, 7], [8, 9, 4, 5, 8, 9, 0], [2, 9, 7, 8, 0, 4, 5], [9, 6, 4, 9, 3, 7, 2], ]) awesome.shape # -> Một ma trận 4 hàng 7 cột |
1 2 3 | Output: (4, 7) |
Steps to proceed
- Access row 2 and row 3 through indexes 1 and 2
- Access column 2 and column 3 of the above 2 rows through indexes 2 and 3
1 2 3 | Input: awesome[1:3, 2:4] |
1 2 3 4 | Output: array([[4, 5], [7, 8]]) |
Access element
Access element
Steps to take:
- Access row 1 via index is 0
- Accessing column element 2 row 1 through index is 1
1 2 3 | Input: awesome[0, 1] |
1 2 3 | Output: 2 |
1 2 3 | # Tương đương với awesome[0][1] |
Access the row element 2 column 3
Steps to take:
- Access row 2 via index is 1
- Access column element 3 of row 2 via index is 2
1 2 3 | Input: awesome[1, 2] |
1 2 3 | Output: 4 |
1 2 3 | # Tương đương với awesome[1][2] |
3. Spreading – Broadcasting
Watch carefully about Broadcasting
Boardcasting we can translate is viral, the way we simultaneously change the value of many elements of an array. More specifically, Numpy allows performing calculations between arrays with numbers and arrays with different dimensions.
Image source: Python Data Science Handbook – Jake VanderPlas
a. Spread the array with numbers
Addition of arrays with numbers
One-dimensional array
1 2 3 4 5 | Input: import numpy as np numbers_1D = np.array([1, 2, 3]) numbers_1D + 3 |
1 2 3 | Output: array([4, 5, 6]) |
As we saw above, 3 is added to all (broadcasting) elements of x.
A simple analogy like the first line of Figure 2, Numpy converts the number 3 into a one-dimensional array, [3, 3, 3] to perform this addition.
1 2 3 | # Tương đương với numbers_1D + np.array([3, 3, 3]) |
1 2 | array([4, 5, 6]) |
1 2 3 | # Tương tự với phép trừ numbers_1D + 3 - 10 |
1 2 | array([-6, -5, -4]) |
Two-dimensional array
1 2 3 4 5 6 7 | Input: numbers_2D = np.asarray([ [4, 5, 6], [7, 8, 9] ]) numbers_2D + 20 |
1 2 3 | array([[24, 25, 26], [27, 28, 29]]) |
Multiplication arrays with numbers
One-dimensional array
1 2 3 4 | Input: numbers_1D_range = np.arange(10) numbers_1D_range |
1 2 3 | Output: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
1 2 3 | Input: numbers_1D_range * 20 |
1 2 3 | Output: array([ 0, 20, 40, 60, 80, 100, 120, 140, 160, 180]) |
Two-dimensional array
1 2 3 4 | Input: numbers_2D_ones = np.ones((10, 10)) numbers_2D_ones |
1 2 3 4 5 6 7 8 9 10 11 12 | Output: array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]) |
1 2 3 4 | Input: numbers_2D_ones * 8 |
1 2 3 4 5 6 7 8 9 10 11 | array([[8., 8., 8., 8., 8., 8., 8., 8., 8., 8.], [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.], [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.], [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.], [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.], [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.], [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.], [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.], [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.], [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.]]) |
b. Spread the array with the array
Addition of 2 arrays of different dimensions
2-dimensional array with 1-dimensional array
1 2 3 4 5 6 7 8 9 10 11 12 13 | Input: x = np.array([ [6, 9, 10], [15, 18, 20], ]) y = np.array([ [2], [4], ]) x + y |
1 2 3 | array([[ 8, 11, 12], [19, 22, 24]]) |
There’s more …