To learn the basics of Python, see One day introduction Python serieshttps://blog.csdn.net/day_and_night_2017/category_8729957.html
numpy is a set of mathematical calculation library based on python. It has rich APIs related to matrix operation and provides convenient calculation tools for in-depth learning.
import numpy as np
Use import to import numpy and specify the alias np
Generate array (one-dimensional vector)
x = np.array([1, 2, 3])
numpy uses ndarray to represent arrays.
>>> x = np.array([1.0, 2.0, 3.0]) >>> print(x) [ 1. 2. 3.] >>> type(x) <class 'numpy.ndarray'>
tips: we generally call one-dimensional arrays vectors; A two-dimensional array is called a matrix; Arrays above three dimensions are called tensors.
Vector and basic operation of vector
Basic operations of corresponding position elements can be performed between vectors:
>>> x = np.array([1.0, 2.0, 3.0]) >>> y = np.array([2.0, 4.0, 6.0]) >>> x + y # Addition of corresponding elements array([ 3., 6., 9.]) >>> x - y array([ -1., -2., -3.]) >>> x * y # element-wise product array([ 2., 8., 18.]) >>> x / y array([ 0.5, 0.5, 0.5])
When the lengths of two vectors are the same, this operation can be performed. In case of inconsistency, an error will be reported.
Vector and scalar operations
When calculating vector and scalar, it is equivalent to calculating between each element of vector and scalar:
>>> x = np.array([1.0, 2.0, 3.0]) >>> x / 2.0 array([ 0.5, 1. , 1.5])
Generate multidimensional array
numpy can generate multidimensional arrays, for example:
>>> A = np.array([[1, 2], [3, 4]]) >>> print(A) [[1 2] [3 4]] >>> A.shape (2, 2) >>> A.dtype dtype('int64')
As long as multiple one-dimensional vectors are stacked together, a two-dimensional matrix is formed.
ndarray has two common attributes:
shape: get the dimension of the tensor.
dtype: get the type of data element
Operations between matrices
The operation between matrices also conforms to the principle of corresponding position calculation:
>>> B = np.array([[3, 0],[0, 6]]) >>> A + B array([[ 4, 2], [ 3, 10]]) >>> A * B array([[ 3, 0], [ 0, 24]])
Matrix and scalar computation
Matrix and constant calculations are still the result of each element and scalar calculation:
>>> print(A) [[1 2] [3 4]] >>> A * 10 array([[ 10, 20], [ 30, 40]])
Here, when calculating between one-dimensional vector or two-dimensional array and scalar, the method of "broadcast" is used.
Broadcast is used in scalar and vector computing. What is broadcast?
When calculating the two-dimensional matrix and scalar 10, it is equivalent to expanding the scalar into a matrix with the same dimension and scalar element values at the corresponding position of the matrix, and then calculating the corresponding position, which is called broadcast.
In fact, matrices and vectors are broadcast directly:
>>> A = np.array([[1, 2], [3, 4]]) >>> B = np.array([10, 20]) >>> A * B array([[ 10, 40], [ 30, 80]])
Here, it is equivalent to expanding the B vector into A corresponding matrix according to the format of A matrix, and then calculating the corresponding position.
Random reading and writing of vectors
The bottom layer of a vector is an array, so its reading and writing method is similar to that of an array:
>>> X = np.array([[51, 55], [14, 19], [0, 4]]) >>> print(X) [[51 55] [14 19] [ 0 4]] >>> X # Line 0 array([51, 55]) >>> X # Elements of (0,1) 55
The first index starts from 0. X represents a one-dimensional array (vector) composed of the first row elements of the matrix
We can also get each row in turn through the for loop:
>>> for row in X: ... print(row) ... [51 55] [14 19] [0 4]
Merge matrices into one-dimensional arrays
>>> X = X.flatten() # Convert X to a one-dimensional array >>> print(X) [51 55 14 19 0 4]
Using flatten, the matrices can be merged into one-dimensional matrices from top to bottom and from left to right.
We can specify the index to get elements in batch:
>>> X[np.array([0, 2, 4])] # Get element with index 0, 2, 4 array([51, 14, 0])
Matrix element judgment and filtering
The operation on the matrix is equivalent to the operation on each element:
>>> X > 15 array([ True, True, False, True, False, False], dtype=bool)
According to the judgment result, filter the elements at the corresponding position when the result is True
>>> X[X>15] array([51, 55, 19])