This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together
Data science included in the main use of Numpy knowledge points, Numpy learning this is enough
1 Numpy Indicates the data type
- dtype
The keyword | instructions |
---|---|
inti | Int32 or int64, an integer whose precision depends on the platform |
int8 int16 int32 int64 | Signed integer |
uint8 uint16 uint32 uint64 | Unsigned integer |
float16 float32 float64 | The plural |
complex64 complex128 | The plural |
2 numpy array
2.1 Creating an Array
2.1.1 Creating the one-dimensional array Arange
- Format: np. Arange (start, end, step, dtype)
np.arange(10) > > [0 1 2 3 4 5 6 7 8 9]
np.arange(2.10) > > [2 3 4 5 6 7 8 9]
np.arange(2.10.3) > > [2 5 8]
np.arange(2.10.3,dtype='float64') > > [2. 5. 8.]
Copy the code
2.1.2 Creating a Multidimensional Array array
np.array([1.5) > > [1 5]
np.array([[1.5], [6.10[[]]) > >1 5]
[ 6 10]]
Copy the code
2.2 Changing array dimensions
The command | instructions |
---|---|
reshape() | Do not modify on the original array, return the changed result |
shape | Get or set dimensions (modify the original array) |
resize() | On the original array |
2.2.1 reshape ()
a = np.arange(12)
b = a.reshape(3.4)
print(a)
print(b)
>> [ 0 1 2 3 4 5 6 7 8 9 10 11[[] > >0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Copy the code
2.2.2 shape
a = np.arange(12)
a_shape = a.shape
print(a_shape)
>> (12,)
a.shape = (3.4)
print(a)
>> [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Copy the code
2.2.3 the resize ()
a = np.arange(12)
b = a.resize(3.4)
print(b)
>> None
print(a)
>> [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Copy the code
2.3 Flattening of multi-dimensional groups
Command:
- ravel()
- flatten()
Description:
- Do not change the original array
a = np.array([[1.2], [3.4]])
a_ravel = a.ravel()
a_flatten = a.flatten()
print(a_ravel)
>> [1 2 3 4]
print(a_flatten)
>> [1 2 3 4]
Copy the code
Section 2.4
a = np.array([[1.2], [3.4]])
print(a)
>> [[1 2]
[3 4]]
print(a[0.0) > >1
>
print(a[:,0) > > [1 3]
Copy the code
Where “:” indicates any, and a[:,0] indicates the index 0 column of any row
2.5 Array Combination
2.5.1 Horizontally Combining the HStack
a = np.array([[1.2], [3.4]])
b = np.array([[5.6], [7.8]])
print(np.hstack((a,b)))
>> [[1 2 5 6]
[3 4 7 8]]
Copy the code
2.5.2 Vertically Combining the VStack
a = np.array([[1.2], [3.4]])
b = np.array([[5.6], [7.8]])
print(np.vstack((a,b)))
>> [[1 2]
[3 4]
[5 6]
[7 8]]
Copy the code
2.5.3 Array Combination DStack
a = np.array([[1.2], [3.4]])
b = np.array([[5.6], [7.8]])
print(np.dstack((a,b)))
>> [[[1 5]
[2 6]]
[[3 7]
[4 8]]]
Copy the code
2.5.4 Column Combination column_STACK
a = np.array([[1.2], [3.4]])
b = np.array([[5.6], [7.8]])
print(np.column_stack((a,b)))
>> [[1 2 5 6]
[3 4 7 8]]
Copy the code
2.5.5 Combining Rows row_STACK
a = np.array([[1.2], [3.4]])
b = np.array([[5.6], [7.8]])
print(np.row_stack((a,b)))
>> [[1 2]
[3 4]
[5 6]
[7 8]]
Copy the code
2.6 Array Splitting
a = np.array([[1.2], [3.4]])
print(a[0.0) > >1
print(a[:,0) > > [1 3]
Copy the code
2.7 Conversion of Numpy arrays to Python lists
The command | instructions |
---|---|
tolist() | Converted to an array |
Astype (types) | Specifies the type of the conversion array |
a = np.arange(5)
print(a.tolist())
>> [0.1.2.3.4]
print(a.astype(float)) > > [0. 1. 2. 3. 4.]
Copy the code
2.8 Other Common Commands
function | The command |
---|---|
Get data type | dtype |
Number of bytes occupied by memory | itemsize |
The number of dimensions or axes in an array | ndim |
real | Returns the real part of the complex number |
imag | Returns the imaginary part of the complex number |
flat | Returns a flat iterator |
3 matrix
3.1 Creating the identity matrix eye
eye = np.eye(3)
print(eye)
>> [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Copy the code
3.2 Zero vectors and zero matrices
The command | instructions |
---|---|
zero() | Create a zero vector or matrix |
zero_like() | The zero vector or matrix of the same dimension as the target |
# 1. Create a zero row vector
x_v = np.zeros(5)
print(x_v)
>> [0. 0. 0. 0. 0.]
# 2. Create a zero matrix
x_m = np.zeros([2.4])
print(x_m)
>> [[0. 0. 0. 0.]
[0. 0. 0. 0.]]
# 3. Create a vector or matrix with the same shape as the target
x_v = np.array([1.2.3.4])
x_m = np.array([[1.2.3], [4.5.6]])
print(np.zeros_like(x_v))
>> [0 0 0 0]
print(np.zeros_like(x_m))
>> [[0 0 0]
[0 0 0]]
Copy the code
3.3 One vector or matrix
The command | instructions |
---|---|
ones() | Creates a vector or matrix with a value of 1 |
ones_like() | Creates a vector or matrix with the value 1 of the same dimension as the target |
print(np.ones(5)) > > [1. 1. 1. 1. 1.]
print(np.ones([2.3[[]]) > >1. 1. 1.]
[1. 1. 1.]]
x_m = np.array([[1.2.3], [4.5.6]])
print(np.ones_like(x_m))
>> [[1 1 1]
[1 1 1]]
Copy the code
3.4 The value of MAT matrix and array matrix
- Mat Value: [m,n]
- Array Value: [m][n]
x_v_m = np.mat('1 2 3; 4, 5 6 ')
x_v_a = np.array([[1.2.3], [4.5.6]])
# take some element
print(x_v_m[1.2) > >6
print(x_v_a[1] [2) > >6
If mat matrix does not take elements, the result is a matrix. If array matrix takes a row, the result is a row vector
print(x_v_m[0, :) > > [[1 2 3]]
print(x_v_a[0) > > [1 2 3]
# take a list
print(x_v_m[:,0[[]] > >1]
[4]]
Copy the code
3.5 Conversion of Mat matrix and array matrix
- .A converts MAT to array
- Np.mat () converts mat to array
x_m = np.mat('1 2 3; 4, 5 6 ')
x_a = x_m.A
print(type(x_a))
>> <class 'numpy.ndarray'>
>
x_m = np.mat(x_a)
print(type(x_m)) > > <class 'numpy.matrix'>
Copy the code
3.6 Transpose the matrix T
x_m = np.mat('1 2 3; 4, 5 6 ')
print(x_m)
>> [[1 2 3]
[4 5 6]]
print(x_m.T)
>> [[1 4]
[2 5]
[3 6]]
Copy the code
3.7 Matrix inversion
- .I If the matrix is irreversible, an exception is reported
x_m = np.mat('1 2 3; 4 5 6 ; 0 0 1')
print(x_m.I)
>> [[-1.66666667 0.66666667 1. ]
[ 1.33333333 -0.33333333 -2. ]
[ 0. 0. 1. ]]
Copy the code
3.8 Merging the block matrix BMAT
- Space: horizontal merge
- Semicolon: vertical merge
x_m = np.mat('1 2; 3 4 ')
# block matrix combination, semicolon indicates next line
print(np.bmat('x_m x_m')) > > [[1 2 1 2]
[3 4 3 4]]
print(np.bmat('x_m; x_m')) > > [[1 2]
[3 4]
[1 2]
[3 4]]
Copy the code
3.9 deductions for
3.9.1 Constant operation
a = np.mat('1 2 ; 3 4')
print(a + 2)
print(a - 2)
print(a / 2)
Copy the code
3.9.2 Matrix addition and subtraction
a = np.mat('1 2 ; 3 4')
b = np.mat('5 6; 7, 8 ')
print(a + b)
>> [[ 6 8]
[10 12]]
print(a - b)
>> [[-4 -4]
[-4 -4]]
Copy the code
3.9.3 divide divide
The command | instructions |
---|---|
/ and divide | General division |
/ / and floor_divide | Integer part reserved |
x_v1 = np.array([1.2.3])
x_v2 = np.array([3.5.6])
print(np.divide(x_v1,x_v2))
>> [0.33333333 0.4 0.5 ]
print(np.floor_divide(x_v1,x_v2))
>> [0 0 0]
Copy the code
3.9.4 Add, multiply, subtract
The command | instructions |
---|---|
add | add |
multiply | The multiplication |
subtract | subtraction |
- Reduce returns the result directly
- Accumulate preserves the intermediate process
Take addition:
- If it’s a row vector, return the sum
- If it’s a matrix, add the column units to get a 1 by n row vector
x_v = np.array([1.2.3])
x_m = np.mat('1 2 3; 4 5 6; 7 8 9 ')
print(np.add.reduce(x_v))
>> 6
print(np.add.reduce(x_m))
>> [[12 15 18]]
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
print(np.add.accumulate(x_v))
>> [1 3 6]
print(np.add.accumulate(x_m))
>> [[ 1 2 3]
[ 5 7 9]
[12 15 18]]
Copy the code
3.9.5 Flattening sum
- If it’s a matrix, it’s the same thing as flattening and summing
x_v = np.array([1.2.3.4])
x_m = np.array([[1.2.3], [4.5.6]])
# sum is equal to the sum after a draw
print(np.sum(x_v))
>> 10
print(np.sum(x_m))
>> 21
Copy the code
3.10 the multiplication
3.10.1 Multiplication of constants
a = np.mat('1 2 ; 3 4')
print(a * 2) > > [[2 4]
[6 8]]
Copy the code
3.10.2 Matrix Multiplication
type | * |
---|---|
array | Multiplication of corresponding elements |
mat | Matrix multiplication |
The command | instructions |
---|---|
np.dot() | Matrix multiplication |
np.multiply() | Multiplication of corresponding elements |
x_array_m = np.array([[1.2.3], [4.5.6]])
print(x_array_m * x_array_m)
>> [[ 1 4 9]
[16 25 36]]
Copy the code
x_mat_m = np.mat('1 2 3; 4, 5 6 ')
print(x_mat_m * x_mat_m.T)
>> [[14 32]
[32 77]]
Copy the code
3.10.3 Vector multiplication
type | * | dot | multiply |
---|---|---|---|
array | Multiplication of corresponding elements | Inner product | Multiplication of corresponding elements |
x_v = np.array([1.2.3.4])
print(x_v * x_v)
>> [ 1 4 9 16]
print(np.dot(x_v,x_v))
>> 30
print(np.multiply(x_v,x_v))
>> [ 1 4 9 16]
Copy the code
3.10.4 factorial
The command | instructions |
---|---|
prod | Return the maximum value |
comprod | So let’s figure out the factorial |
x_v = np.array([1.2.3.4])
x_m = np.array([[1.2.3], [6.7.8]])
# 1. Factorial prod, for matrices, is equivalent to flattening
print(np.prod(x_v))
>> 24
print(np.prod(x_m))
>> 2016
# 2. Cumprod
print(np.cumprod(x_v))
>> [ 1 2 6 24]
print(np.cumprod(x_m))
>> [ 1 2 6 36 252 2016]
Copy the code
4 Common Functions
4.1 Reading and Writing Files
- Write file: savetxt
- Read file: loadtxt
- Delimiter: Column delimiter, default is space
- Usecols: which columns to load, default all (tuple representation)
- Unpack: indicates whether to transpose
matrix = np.mat('1 2 3 ; 4 5 6; 7 8 9')
# 1. Write the file, savetxt
np.savetxt('matrix.csv',matrix)
# 2. Read the file
content = np.loadtxt('matrix.csv',delimiter=' ',usecols=(0.1.2),unpack=True)
print(content)
Copy the code
4.2 Arithmetic mean
- If it’s a matrix, you take the whole matrix and you average it
Create a row vector and a matrix
x_v = np.array([1.2.3.4])
x_m = np.mat('1 2 3; 4, 5 6 ')
print(np.mean(x_v))
>> 2.5
print(np.mean(x_m))
>> 3.5
Copy the code
4.3 Maximum Value Minimum value Max min
Create a row vector and a matrix
x_v = np.array([1.2.3.4])
x_m = np.mat('1 2 3; 4, 5 6 ')
# maximum, Max
print(np.max(x_v))
print(np.max(x_m))
# Minimum, min
print(np.min(x_v))
print(np.min(x_m))
Copy the code
4.4 Value Range PTP
- The difference between maximum and minimum
x_v = np.array([1.2.3.4])
# difference between minimum and maximum, PTP
print(np.ptp(x_v))
>> 3
Copy the code
4.5 Median
# median, median, np.median(...) The output median is not necessarily present in the original data
x_v = np.array([1.2.3.4])
print(np.median(x_v))
>> 2.5
Copy the code
4.6 sorting msort
x_v = np.array([1.2.5.1.2.6.2])
# From small to big
print(np.msort(x_v))
>> [1 1 2 2 2 5 6]
Copy the code
4.7 the variance var
x_v = np.array([1.2.3.4])
x_m = np.array([[1.2.3], [4.5.6]])
Var is variance, not pseudo-variance
print(np.var(x_v))
>> 1.25
print(np.var(x_m))
>> 2.9166666666666665
Copy the code
4.8 Standard Deviation STD
- The matrix is actually the same thing as the standard deviation after flattening out
x_v = np.array([1.2.3.4])
x_m = np.array([[1.2.3], [4.5.6]])
print(np.std(x_v))
>> 1.118033988749895
print(np.std(x_m)
>> 1.707825127659933
Copy the code
4.9 Diff difference between adjacent elements
- The difference between adjacent elements, the matrix is equivalent to computing the result of multiple row vectors
x_v = np.array([1.2.3.4])
x_m = np.array([[1.2.3], [4.5.6]])
print(np.diff(x_v))
>> [1 1 1]
print(np.diff(x_m))
>> [[1 1]
[1 1]]
Copy the code
4.10 Arithmetic square root SQRT
x_v = np.array([1.2.3.4])
x_m = np.array([[1.2.3], [4.5.6]])
print(np.sqrt(x_v))
>> [1. 1.41421356 1.73205081 2. ]
print(np.sqrt(x_m))
>> [[1. 1.41421356 1.73205081]
[2. 2.23606798 2.44948974]]
Copy the code
4.11 Index where conditions are Met
- For row vectors, the index that satisfies the WHERE condition is returned
- For a matrix, the first is the row index and the second is the column index
x_v = np.array([1.2.3.4])
x_m = np.array([[1.2.3], [4.5.6]])
For row vectors, the index that satisfies the WHERE condition is returned
print(np.where(x_v > 2))
>> (array([2.3], dtype=int64),)
For a matrix, the first is the row index and the second is the column index
print(np.where(x_m > 2))
>> (array([0.1.1.1], dtype=int64), array([2.0.1.2], dtype=int64))
ret = np.where(x_m > 2)
for i in range(len(ret[0])):
row_index = ret[0][i]
column_index = ret[1][i]
print(x_m[row_index][column_index])
>> 3
4
5
6
Copy the code
4.12 Get the element take by index
- Vector, return by index
- Matrix, which is the equivalent of flattening the matrix and returning it by index
x_v = np.array([1.2.3.4])
x_m = np.array([[1.2.3], [4.5.6]])
print(np.take(x_v,[0.1.2)) > > [1 2 3]
print(np.take(x_m,[4.5)) > > [5 6]
Copy the code
4.13 Comparison of maximum values
- Maximum Compares the maximum value of two objects and returns the largest object
- Minimum Compares the smallest of two objects and returns the smallest object
x_v1 = np.array([0.2.3.4])
x_v2 = np.array([1.1.1.5])
# Compare the maximum value of two objects and return the largest object
print(np.maximum(x_v1,x_v2))
>> [1 2 3 5]
# Compare the minimum value of two objects and return the smallest object
print(np.minimum(x_v1,x_v2))
>> [0 1 1 4]
Copy the code
4.14 E to the exponent e
x_v = np.array([1.2.3.4])
x_m = np.array([[1.2.3], [4.5.6]])
print(np.exp(x_v))
>> [ 2.71828183 7.3890561 20.08553692 54.59815003]
print(np.exp(x_m))
>> [[ 2.71828183 7.3890561 20.08553692]
[ 54.59815003 148.4131591 403.42879349]]
Copy the code
4.15 linespace
linspace(start,end,num)
- Start the start
- End terminates, including end
- The format of the num point, equivalent to 1/step
x_v1 = np.linspace(0.10.10)
print(x_v1)
>> [ 0. 1.11111111 2.22222222 3.33333333 4.44444444 5.55555556
6.66666667 7.77777778 8.88888889 10. ]
Copy the code
4.16 Define convenient clip
- If it’s greater than the boundary, take the maximum of the boundary
- Less than the boundary and take the minimum of the boundary
- Belongs to the boundary range, the original value output
x_v = np.array([1.2.3.4])
print(x_v)
print(np.clip(x_v,2.3)) > > [1 2 3 4]
[2 2 3 3]
x_m = np.array([[1.2.3], [4.5.6]])
print(x_m)
print(np.clip(x_m,2.4)) > > [[1 2 3]
[4 5 6[[]] > >2 2 3]
[4 4 4]]
Copy the code
4.17 Element COMPRESS that meets the conditions
- Can only be vector-based operations that return elements that meet the criteria, not indexes
x_v = np.array([1.2.3.4])
print(x_v.compress(x_v > 2)) > > [3 4]
Copy the code
4.18 Trigonometric functions
- sin
- cos
- tan
x_m = np.random.rand(10)
# sin()
print(np.sin(x_m))
# cos()
print(np.cos(x_m))
# tan()
print(np.tan(x_m))
Copy the code
4.19 Maximum index
- Argmax and argmin, return the largest | small value index (matrix flattening)
x_v = np.array([1.2.3.4])
print(np.argmax(x_v))
>> 3
print(np.argmin(x_v))
>> 0
The matrix operation is equivalent to flattening jj
x_m = np.array([[1.2.3], [4.5.6]])
print(np.argmax(x_m))
>> 5
print(np.argmin(x_m))
>> 0
Copy the code
5 Portable Functions
5.1 Covariance COV
x_v1 = np.array([1.2.3.4])
x_v2 = np.array([4.5.6.7])
print(np.cov(x_v1,x_v2))
>> [[1.66666667 1.66666667]
[1.66666667 1.66666667]]
Copy the code
5.2 Diagonal element diagonal
- Diagonal returns a vector of diagonal elements
x_m = np.array([[1.2.3.4], [2.3.4.5]])
print(x_m)
>> [[1 2 3 4]
[2 3 4 5]]
print(np.diagonal(x_m))
>> [1 3]
Copy the code
5.3 Trace trace of matrix
- A trace is the sum of diagonal elements
x_m = np.array([[1.2.3.4], [2.3.4.5]])
print(x_m)
>> [[1 2 3 4]
[2 3 4 5]]
print(np.trace(x_m))
>> 4
Copy the code
5.4 Correlation coefficient CorrCOEF
x_v1 = np.array([1.2.3.4])
x_v2 = np.array([2.3.4.5])
# Correlation coefficient, Corrcoef
print(np.corrcoef(x_v1,x_v2))
>> [[1. 1.]
[1. 1.]]
Copy the code
5.5 Absolute value ABS
x_m = np.array([[-1, -2, -3], [1.2.3]])
print(np.abs(x_m))
>> [[1 2 3]
[1 2 3]]
Copy the code
5.6 Polynomial fitting
import numpy as np
# Generate samples randomly
x = np.random.rand(50) * 50
y = -(np.sin(x)*2 + 0.5 * x ** 2)
Polyfit is the fit parameter equivalent to AX^2 + BX + C. The second parameter is the degree of freedom, which is used to fit the sample output with several functions
fit = np.polyfit(x,y,2)
# polyfit, predict, use existing FIT to predict the output of sample X
fit_y = np.polyval(fit,x)
from matplotlib import pyplot as plt
Display the original sample
plt.plot(x,y,'b^',label='sample')
# Show the prediction for the sample
plt.plot(x,fit_y,'r.',label='predict')
Find the roots of the polynomial
print(np.roots(fit))
Find the derivative of the fitting function
fit_der = np.polyder(fit)
print('The original coefficient',fit)
print('The derivative coefficient'.,fit_der)
# Draw the prediction curve
x_line = np.linspace(np.min(x),np.max(x),100)
y_line = np.polyval(fit,x_line)
plt.plot(x_line,y_line,color='y')
# Draw the derivative of the prediction curve
y_line_fit_der = np.polyval(fit_der,x_line)
plt.plot(x_line,y_line_fit_der,color='b')
# Label the x and y axes
plt.xlabel('sample')
plt.ylabel('label')
Draw a grid
plt.grid(True)
# according to the label
plt.legend(loc=0)
# show
plt.show()
Copy the code
5.7 Sign function sign
- < 0 return 1
- > 0 returns 1
- = 0 0 is returned
x_v = np.array([[1.2.3]])2
x_m = np.array([[1.2.3], [4.5.6]])2
print(np.sign(x_v))
>> [[-1 0 1]]
print(np.sign(x_m))
>> [[-1 0 1]
[ 1 1 1]]
Copy the code
5.8 denoising piecewise
- The first parameter: the object to be modified
- Second argument: list write condition
- Third argument: What value is assigned to the element for each condition
x_v = np.array([1.2.3.4]) - 1
x_m = np.array([[1.2.3], [4.5.6]])
print(x_v)
print(np.piecewise(x_v,[x_v < 1,x_v > 2], [...100.100)) > > [0 1 2 3[-] > >100 0 0 100 ]
print(x_m)
print(np.piecewise(x_m,[x_m < 3,x_m > 5], [...100.100[[]]) > >1 2 3]
[ 4 5 6[[]] > > -100 -100 0]
[ 0 0 100]]
Copy the code
5.9 Checking whether isreal is the real number
x_v = np.array([1+2j])
print(np.isreal(x_v))
>> [False]
Copy the code