Article directory
- Numpy array processing library
- array broadcast
- Features of the array type
- Type conversion (emphasis)
- array slice
Numpy array processing library
Import the numpy library and alias it as np
import numpy as np
See what is provided in the numpy library
dir(np)或help(np)
Supplementary description of a function of jupyer
Put the cursor on the function and press shift+tab
to call out the function description:
Use numpy's random function to randomly generate arrays
numpy.random.rand()
numpy.random.rand(d0,d1,…,dn)
- The rand function generates data between [0,1) according to the given dimension, including 0 and excluding 1
- dn indicates dimension
- The return value is an array of the specified dimension
example:
numpy.random.randn() - important
numpy.random.randn(d0,d1,…,dn)
-
The randn function returns a sample or set of samples, with a standard normal distribution.
-
dn indicates dimension
-
The return value is an array of the specified dimension
-
.shape
The construction of the resulting array can be viewed via -
By
.ndim
looking at the dimension of the array (also can be understood as the number of "axes") -
By
.size
looking at the number of elements contained in the array
-
.argmax
: Returns the index (subscript) of the maximum value for the specified axis. In a two-dimensional array, the parameter "0" represents the column, and the parameter "1" represents the row. After
explaining why it is used.argmax(axis=0)
, the output array([1, 1, 0]) ?
.argmax
returns the maximum value of the specified axis索引(下标)
.二维数组中,参数“0”代表列,参数“1”代表行。
Use.argmax(0)
fetch列的最大值的索引
:
The first column of the generated three-row three-column array arr3 is: 0.78650488, 1.30572907, -0.49828544, among which 1.30572907 is the largest, and its index is 1.
The second column of the generated three-row three-column array arr3 is: 0.38190384, 1.60970205, -2.00096618, among which 1.60970205 is the largest, and its index is 1.
The third column of the generated three-row three-column array arr3 is: 1.145984, -0.2094228, -0.85838297, among which 1.145984 is the largest, and its index is 0.
In the same way, use .argmax(axis=1)
fetch 行的最大值的索引
:
numpy.random.randint()
numpy.random.randint(low, high=None, size=None, dtype=numpy.int32)
- Return a random integer , the range is [low, high), including low, excluding high
- Parameters: low is the minimum value, high is the maximum value, size is the size of the array dimension, dtype is the data type, and the default data type is np.int
- When high is not filled in, the default range of random number generation is [0, low)
Generate a floating point number between [0,1)
- numpy.random.random_sample(size=None)
- numpy.random.random(size=None)
- numpy.random.ranf(size=None)
- numpy.random.sample(size=None)
numpy.random.choice()
numpy.random.choice(a, size=None, replace=True, p=None)
- Generate random numbers from a given 1D array
- Parameters: a is a one-dimensional array similar to data or an integer; size is the dimension of the array; p is the probability of the data in the array appearing
- When a is an integer, the corresponding one-dimensional array is np.arange(a)
- The length of parameter p needs to be consistent with the length of parameter a;
- The parameter p is the probability, and the sum of the data in p should be 1
numpy.random.seed()
- The role of np.random.seed(): Make random data predictable.
- When we set the same seed, the random number generated each time is the same. If no seed is set, a different random number will be generated each time
numpy.zeros()
It is used to generate an array of all 0s, and the variable type can be specified with the dtype parameter
Example: Generate an array of all zeros with three rows and four columns, and the type is float32
array multiplication numpy.dot()
numpy.arange() - important
Use the arange function in the numpy library to create a range of values and return an ndarray (n-dimensional array, n-dimensional array) object. The format of the function is as follows:
numpy.arange(start, stop, step, dtype)
Generate an ndarray according to the range specified by start and stop and the step size set by step.
array broadcast
Multiplying an array by 10 will multiply all the numbers in it by 10
Add a number to the array, each element will add this number
If arr2 is an array of 4 rows and 6 columns, and arr3 is a matrix of 1 row and 6 columns, arr2 + arr3 is each row of arr2 plus the corresponding element of arr3
Features of the array type
- has its own shape (shape)
- There are data types (dtype), such as: dtype=numpy.int32; dtype=numpy.float32
- You can modify the value of an element in an array
Modify array shape numpy.reshape() - very important
- Preliminary work, first generate an array object:
arr5 = np.arange(0, 24, dtype=np.int32)
arr5
- Convert the array to an array of three rows and eight columns:
arr5.reshape(3, 8)
- Convert the array to an array of four rows and six columns:
arr5.reshape(4, 6)
-
Find the first element of the array whose shape has been modified by indexing (note the difference from list indexing, array indexing is a bit simpler and less parentheses):
-
Indexed elements in supplementary lists:
-
Modify elements in an array:
注意,使用reshape函数时,不会创建副本。例如,这里修改了arr6的元素,但是也修改了arr5的元素,arr5和arr6指向同一个元素,没有创建副本,如下图:
使用copy函数,创建副本
arr6 = arr5.copy()
Use the function list() to force an array to be converted to a list
Use the function list() to force an array of three rows and four columns to be converted to a list, where each element is still an array type:
Use .ndim to view the dimensions of an array
It can also be understood as the number of axes in the array.
Example: arr6 is an array of three rows and four columns
Use .size to view the number of elements in the array
Type conversion (emphasis)
Use numpy.array() to convert a list to an array
Pay attention to the comments, involving the use of type() function and dtype attribute
numpy.astype()
By looking at the function description, we can see that astype() can create a copy of the original array and change the type of elements in the array
Example:
After using the astype() function, check whether it is a copy of arr7 through the elements in arr8:
array slice
数组切片是原始数据的视图,也就是不会创造副本。注意这与列表切片的区别。
High-dimensional array slices:
Example 1:
Example 2: