This is the first day of my participation in Gwen Challenge
The current plan for this series is to update pandas, and some specific cases will be explained together. Pandas will then compare the two technologies: SQL and PANDAS.
In this section, however, let’s review the common numpy. We’ll use it later in the code.
1. Numpy generation
- The simplest way is to specify the value directly
import numpy as np
np.array([1.2.3])
Copy the code
Output: array([1, 2, 3])
-
Generate an arithmetic sequence
-
Contain termination
-
np.linspace(1.5.3) Start, end, array length
Copy the code
Array ([1., 3., 5.])
Note: these happen to be integers, but will be converted to floating point numbers.
-
- Does not contain termination items
np.arange(1.5.2) Start, end, array length
Copy the code
Output: array([1, 3])
2. Special matrix
-
Np. zeros generate all zero matrices (probably not used much)
-
Np.eye (3) generates the identity matrix with 3 rows and 3 columns
-
Fill the matrix
np.full((6.5), 10)
Copy the code
np.full((6.5),1.2.3])
Copy the code
Np. Full ((6,5), [1, 2, 3, 4, 5])Copy the code
To sum up: The np.full function populates the columns based on their values. If the two parameter columns are not equal, they will not be filled unless the second parameter column is 1
3. Array deformation and merge
- Array transpose
import numpy as np
arr = np.full((6, 5), [1, 2, 3, 4, 5])
print(arr)
arr.T
Copy the code
// The output is [[1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5]] array([[1, 1, 1, 1, 1], [2, 2, 2, 2, 1, 2, 1, 1, 1]) 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5]])Copy the code
- Array merge np.r_, np.c_
import numpy as np
print(np.r_[np.array([0.0]),np.zeros(2)])
print(np.c_[np.array([1.2]),np.zeros(2)])
print(np.r_[np.array([[0.0], [1.1]]),np.array([[2.2], [3.3]]])Copy the code
Look at the code above and the output is:
[0. 0 0. 0.] [[1. 0.] [2. 0.]] [[0, 0] [1, 1] [2] 2 3] [3]Copy the code
For one-dimensional arrays, the np.r_ and Np. c_ merge rules seem to be reversed. After repeated verification and analysis, it is found that numpy stores one-dimensional arrays as column vectors.