Python Data Analysis - NumPy -6. NumPy Module


Preface

Many modules in NumPy are inherited from its predecessor, Numeric. Some of these modules also have corresponding parts in SciPy, and their functions may be more diverse. The ny.dual module contains functions defined in both NumPy and SciPy.


1. Linear Algebra

1.1. Calculate the inverse matrix (np. final. inv())

In linear algebra, multiplying a matrix by its inverse matrix yields an identity matrix. The inv() function in the numpy.linalg module can calculate the inverse matrix.

import numpy as np
#apply mat function creation example matrix 
A = np.mat("0 1 2;1 0 3;4 -3 8")
# B = np.mat(([0, 1, 2], [1, 0, 3], [4, -3, 8]))
#output matrix A
print("A:n", A)
#apply inv calculate the inverse matrix of the () function 
inverse = np.linalg.inv(A)
print("inverse matrix :n", inverse)
#check the result of multiplying the original matrix by the obtained inverse matrix 
check = A * inverse
print("multiplying the original matrix by the inverse matrix (identity matrix) :n", check)  #the result is indeed an identity matrix 

1.2. Solving linear equation systems (np. final. solve(), np. dot())

Matrix can perform linear transformation on vectors, which corresponds to a system of linear equations in mathematics. The solve() function in the ny. linalg module can solve problems in the form of Ax= A linear system of equations for b, where A is a matrix, b is a one-dimensional or two-dimensional array, and x is an unknown variable. Meanwhile, use the dot() function to calculate the dot product of two floating-point arrays.

import numpy as np
#apply mat function creation example matrix 
A = np.mat(([1, -2, 1], [0, 2, -8], [-4, 5, 9]))
b = np.array([0, 8, -9])
#output matrix A、b
print("A:n", A)
print("b:n", b)
#apply solve solving linear equations with () function 
x = np.linalg.solve(A,b)
print("Solution:n", x)
#apply dot the solution obtained by checking the () function 
B = np.dot(A,x)
print("multiplying the original matrix by the inverse matrix (identity matrix) :n", B)  #results and b equally 
import numpy as np
#apply mat function creation example matrix 
A = np.mat(([1, -2, 1], [0, 2, -8], [-4, 5, 9]))
b = np.mat([0, 8, -9]).T
#output matrix A、b
print("A:n", A)
print("b:n", b)
#apply solve solving linear equations with () function 
x = np.linalg.solve(A, b)
print("Solution X:n", x)
#apply dot the solution obtained by checking the () function 
B = np.dot(A, x)
print("multiplying the original matrix by the inverse matrix (identity matrix) :n", B)  #results and b equally 
print(B.shape)
print(b.shape)
equal = np.array_equal(B, b)  #check if two arrays have the same shape and elements 
print("Arrays equal?", equal)

1.3 Eigenvalues and eigenvectors (np. final. eigvals(), np. final. eig())

Eigenvalue is the equation Ax= The root of ax is a scalar. Among them, A is a two-dimensional matrix, and x is a one-dimensional vector. Eigenvector is a vector about eigenvalues. The eigvals() function in the np. final module can calculate the eigenvalues of a matrix, while the eig() function in the np. final module can return a tuple containing the eigenvalues and corresponding eigenvectors.

import numpy as np
#apply mat function creation example matrix 
A = np.mat(([1, -2, 1], [0, 2, -8], [-4, 5, 9]))
#output matrix A
print("A:n", A)
#apply eigvals () function for solving eigenvalues 
eigenvalue = np.linalg.eigvals(A)
print("Eigenvalue a:n", eigenvalue)
#apply eig the () function solves for eigenvalues and eigenvectors. this function will return a tuple 
#the first column is the eigenvalues, and the second column is the eigenvectors. 
eigenvalues, eigenvectors = np.linalg.eig(A)
print("First tuple of eig", eigenvalues)
print("Second tuple of eign", eigenvectors)
print()
#apply dot () function validation : calculate equation Ax = ax is the left and right halves of equal 
for i in range(len(eigenvalues)):
print("Left", np.dot(A, eigenvectors[:, i]))
print("Right", eigenvalues[i] * eigenvectors[:, i])
print()

1.4 Singular Value Decomposition (np. linalg. svd(), np. diag())

SVD (Singular Value Decomposition) is a factorization operation that decomposes a matrix into the product of three matrices. The svd() function in the numpy.final module can perform singular value decomposition on matrices. This function returns three matrices: U, Sigma, and V, where U and V are orthogonal matrices, and Sigma contains the singular values of the input matrix.

import numpy as np
#apply mat function creation example matrix 
A = np.mat(([4, 11, 14], [8, 7, 2], [-4, 5, 9]),)
#output matrix A、b
print("A:n", A)
#apply svd () function decomposition matrix 
U, Sigma, V = np.linalg.svd(A, full_matrices=False)
#two orthogonal matrices obtained at the left and right ends U and V , as well as the singular value matrix in the middle Sigma
print("U:n", U)
print("Sigma:n", Sigma)
print("V:n", V)
#there is no true singular value matrix (values on the corner line), using diag the () function generates a complete singular value matrix 
Sigma = np.diag(Sigma)
print(Sigma)
#the one-dimensional array result is a matrix with a one-dimensional array as diagonal elements, and the two-dimensional matrix result outputs the diagonal elements of the matrix 
#multiplying the three decomposed matrices 
produce = U * Sigma * V
print("Productn",produce)

1.5. Generalized inverse matrix (np. final. pinv())

The Moore Penrose pseudoinverse matrix can be solved using the pinv() function in the numpy.linalg module. Calculating the generalized inverse matrix requires the use of singular value decomposition. The inv() function only accepts a square matrix as the input matrix, while the pinv() function does not have this limitation.

import numpy as np
#apply mat function creation example matrix 
A = np.mat(([4, 11, 14], [8, 7, 2], [-4, 5, 9]),)
# A = np.mat("4 11 14;8 7 -2")
#output matrix A、b
print("A:n", A)
#apply pinv calculate the generalized inverse matrix using the () function 
pseudoinv = np.linalg.pinv(A)
print("Pseudoinvn", pseudoinv)
#multiplying the original matrix by the obtained generalized inverse matrix 
check = A * pseudoinv
print("Checkn", check)  #the obtained result is not a strictly defined identity matrix, but it is very approximate 

1.6. Determinant (np. final. det())

A determinant is a scalar value related to a matrix and is widely used in mathematics. For an n × The determinant of a real matrix of n describes a linear transformation pair; Directed volume; The impact caused. A positive value of the determinant indicates that the spatial orientation is maintained (clockwise or counterclockwise), while a negative value indicates that the spatial orientation is reversed. The det() function in the numpy.final module can calculate the determinant of a matrix.

import numpy as np
#apply mat function creation example matrix 
#A = np.mat(([4, 11, 14], [8, 7, 2], [-4, 5, 9]),dtype="int")
A = np.mat("3 4;5 6")
#output matrix A、b
print("A:n", A)
#apply det () function calculation determinant 
determinant = np.linalg.det(A)
print("Determinantn", determinant)

2. Fast Fourier Transform (np. fft. fft())

FFT (Fast Fourier Transform) is an efficient algorithm for computing Discrete Fourier Transform (DFT). DFT has applications in signal processing, image processing, and solving partial differential equations. The fft module in NumPy provides the functionality of fast Fourier transform. In this module, many functions exist in pairs (corresponding inverse operation functions).

import numpy as np
from matplotlib.pyplot import plot, show
#create a cosine wave signal containing 30 points 
x = np.linspace(0, 2 * np.pi, 30)
wave = np.cos(x)
#apply fft the () function performs a fourier transform on a cosine wave signal. 
transformed1 = np.fft.fft(wave)
#application ifft the () function restores the signal 
transformed2 = np.fft.ifft(transformed1)
plot(transformed1)
plot(transformed2)
show()

3. P-shift (np. fft. fftshift())

The fftshift() function in the numpy.linalg module can move the DC component in the FFT output to the center of the spectrum. The ifftshift() function is its inverse operation.

import numpy as np
from matplotlib.pyplot import plot, show
#create a cosine wave signal containing 30 points 
x = np.linspace(0, 2 * np.pi, 30)
wave = np.cos(x)
#apply fft function performs fourier transform on cosine wave signals 
transformed = np.fft.fft(wave)
#apply fftshift perform frequency shift operation on the function 
shifted1 = np.fft.fftshift(transformed)
#use ifftshift perform inverse operation on the function, which will restore the signal before the frequency shift operation 
shifted2 = np.fft.ifftshift(shifted)
# plot(transformed, lw=3)
plot(shifted1, lw=2)
plot(shifted2, lw=1)
show()

4. Random numbers

Random numbers have many applications in Monte Carlo methods, random integrals, and other fields. The generation of true random numbers is very difficult, so pseudorandom numbers are usually used in practical applications. In most application scenarios, pseudo-random numbers are already sufficiently random. The functions related to random numbers can be found in the random module of NumPy
The core algorithm of the random number generator is based on the Mersenne Twister Algorithm. Random numbers can be generated from discrete or continuous distributions. The distribution function has an optional parameter size to specify the number of random numbers to be generated. This parameter allows setting to an integer or tuple, and the generated random number will fill the array of the specified shape. The supported discrete distributions include geometric distributions, hypergeometric distributions, and binomial distributions.

4.1 Binomial distribution (np. random. binomial())

A binomial distribution is a discrete probability distribution of n independent replicates of the number of successful/non successful trials, which are fixed and invariant and independent of the experimental results.

#apply NumPy random in the module binomial the () function simulates random walks 
import numpy as np
from matplotlib.pyplot import plot, show
#initialize an all zero array to store the remaining capital. called with parameter 10000 binomial function 
#it means playing a 10000 round coin gambling game 
cash = np.zeros(10000)
cash[0] = 1000
outcome = np.random.binomial(9, 0.5, size=len(cash))
for i in range(1, len(cash)):
if outcome[i] < 5:
  cash[i] = cash[i - 1] - 1
elif outcome[i] < 10:
  cash[i] = cash[i - 1] + 1
else:
  raise AssertionError("Unexpected outcome " + outcome)
print(outcome.min(), outcome.max())
plot(np.arange(len(cash)), cash)
show()
outcome = np.random.binomial(9, 0.5, size=100)
print(outcome)

4.2. Hypergeometric Distribution (np. random. hypergeometric())

Hypergeometric distribution is a discrete probability distribution that describes the process of extracting a specified number of objects from a jar without putting them back, and then extracting the number of objects of a specified type. The hypergeometric() function in the NumPy random module can simulate this distribution.

import numpy as np
from matplotlib.pyplot import plot, show
#apply hypergeometric the () function initializes the result matrix of the game 
#the first parameter of this function is the number of ordinary balls in the tank, 
#the second parameter is"unlucky ball"the quantity, 
#the third parameter is the number of samples taken each time (touching the ball) 
points = np.zeros(100)
outcomes = np.random.hypergeometric(25, 1, 3, size=len(points))
#calculate the corresponding score based on the game results generated in the previous step 
for i in range(len(points)):
if outcomes[i] == 3:
  points[i] = points[i - 1] + 1
elif outcomes[i] == 2:
  points[i] = points[i - 1] - 6
else:
  print(outcomes[i])
plot(np.arange(len(points)), points)
show()

4.3. Continuous distribution (np. random. normal())

Continuous distribution can be described using PDF (Probability Density Function). The probability of a random variable falling within a certain interval is equal to the area of the probability density function below the curve of that interval. There are a series of continuously distributed functions in the random module of NumPy: beta, chisquare, exponential, f, gamma, gumbel, laplace, lognormal, logistic, multivariate_Normal, noncentral_Chisquare, noncentral_f. Normal, etc.

#random numbers can be generated from a normal distribution, and their histograms can intuitively depict the normal distribution 
#generate a mean of 1 . 75, 10000 normal distribution data with a standard deviation of 1 
import matplotlib.pyplot as plt
#generate uniformly distributed random numbers 
x = np.random.normal(1.75, 1, 10000)
#draw a picture to see the distribution status 
plt.figure(figsize=(20, 10), dpi=100)
#draw a histogram 
plt.hist(x, 100)
plt.show()

Summary

Introduced the knowledge of the NumPy module, covering topics such as linear algebra, fast Fourier transform, continuous and discrete distributions, and random numbers.