current position:Home>Python numpy module basic usage

Python numpy module basic usage

2022-02-02 14:13:50 Ice sky

Python Numpy

One 、 Array

1. establish

Numpy Including a variety of ways to create arrays

np.array(list)

Initialize an array by passing in a list :

import numpy as np
#  Declare an array 
arr = np.array([[1,2,3], [4,5,6]])
''' 1 2 3 4 5 6 '''

np.ones(shape)/np.zeros(shape)

All elements that generate the desired shape are 1(0) Array of :

ones = np.ones((2,2))
''' output 1 1 1 1 '''
zeros = np.zeros((3,3))
''' output 0 0 0 0 0 0 0 0 0 '''

np.arange(begin, end, stride)

Generate a one-dimensional array according to the given interval and step size , The default from the 0 Start , In steps of 1, among :

  • begin: Starting value , optional
  • end: Cutoff value , Required
  • stride: step , optional
range = np.arange(0, 10, 2)
''' output: 0 2 4 6 8 10 '''

np.linespace(begin, end, count)

Similarly, this function also specifies an interval , But here's the difference ,linspace The third parameter refers to : from begin Start to end Number of intermediate equal parts , That is, an array with the specified number of elements is returned , The step length is the average of the interval length for the number of specified elements , for example :

ls1 = np.linspace(0, 10, 2)
ls1.size()
''' output 0, 10 size: 2 '''
ls2 = np.linspace(0, 10, 6)
ls2.size()
''' output 0, 2, 4, 6, 8, 10 size: 6 '''
ls3 = np.linspace(0, 10, 11)
ls3.size()
''' output 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 size: 11 '''

np.random.radint(min, max, shape)

When we need to create an array with random element size , It can be generated by this random number group :

rand = np.random.radint(0, 10, 3)
''' output 4 2 0 3 8 1 9 0 1 '''

2. attribute

Numpy The array object contains several very useful properties that describe the characteristics of the array :

  • ndim: Dimension of array
  • size: Array size , That is, the number of elements included
  • shape: The shape of the array
  • dtype: The type of array

for example :

temp = np.ones((2,2))
''' temp.ndim: 2 temp.size: 4 temp.shape: (2,2) temp.dtype: float '''

3. Method

3.1 Shape transformation method :reshape(shape), flatten()

Shape change , You can adjust the shape of the array , An array commonly used to convert a range to a desired shape :

range = np.arange(0, 10, 3) # [0, 3, 6, 9], size: 4, shape: (1,4)
range.reshape((2,2))
''' output 0 3 6 9 '''
range.flatten()
''' output 0 3 6 9 '''

3.2 Statistical methods :min, max, ptp, median, mean, std, var, sum

A little … \Huge\color{BROWN} A little \dots A little

3.3 Set operations

#  example 
# a: 1,2,3,4,5,6
# b: 5,6,7,8,9

#  intersection 
numpy.intersect1d(a,b)
''' output 5, 6 '''

#  Difference set 
numpy.sestdiff1d(a,b)
''' output 1, 2, 3, 4, 7, 8, 9 '''

From the function name , obviously , The function is used to calculate the intersection between one-dimensional arrays , If the input array dimension is not one-dimensional , At this time, the function will first modify the array flatten operation , Unified into a one-dimensional array and then calculated . setdiff1d Empathy

3.4 Correlation coefficient calculation and mask array

The correlation coefficient is known :
ρ = C o v ( X , Y ) σ X , σ Y \rho=\frac{Cov(X,Y)}{\sigma_{X},\sigma_{Y}} ρ=σX,σYCov(X,Y)
also :
C o v ( X , Y ) = E ( X Y ) − E ( X ) E ( Y ) Cov(X,Y)=E(XY)-E(X)E(Y) Cov(X,Y)=E(XY)E(X)E(Y)
be :
ρ = E ( X Y ) − E ( X ) E ( Y ) σ X σ Y \rho=\frac{E(XY)-E(X)E(Y)}{\sigma_{X}\sigma_{Y}} ρ=σXσYE(XY)E(X)E(Y)
When the correlation coefficient is calculated by code , At this point, you can create a file containing X , Y X,Y X,Y Two arrays of x and y, be X Y XY XY by x * y, Then, according to the statistics member function of the array mentioned above, we can get :

# E(XY)
e_xy = (x * y).mean()
# E(X)E(Y)
e_x_y = x.mean() * y.mean()
# X  Standard deviation 
sigma_x = x.std()
# Y  Standard deviation 
sigma_y = y.std()

Finally, the correlation coefficient can be calculated :

corrcoef = (e_xy - e_x_y) / (sigma_x * sigma_y)

Now combine Numpy The correlation coefficient matrix calculation function provided :

np.corrcoef(x, y)
''' output 1 corr(x,y) corr(x,y) 1 '''

The corresponding correlation coefficient can be obtained at the corresponding row and column position , The diagonal is the self correlation coefficient , by 1 1 1

copyright notice
author[Ice sky],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202021413475327.html

Random recommended