Data analysis --- numpy basics (two)

​Continued from the previous article, continue to update the use of some common functions under numpy, here are mostly matrix operations, creating matrices, identity matrices, solving inverse matrices, etc. and performing one-hot encoding, linear matrix eigenvectors, features Calculation of values, singular values, determinants.

1、np.eye()

np.eye ():  used to return a two-dimensional diagonal array to create an identity matrix

numpy.eye(N,M=None,k=0,dtype=<class 'float'>,order='C)

  • N : int type, which means the number of output lines

  • M : int type, optional, the number of output columns, if not, the default is N

  • k : int type, optional, subscript of the diagonal, the default is 0 means the main diagonal, a negative number means the low diagonal, and a positive number means the high diagonal.

  • dtype : data type, optional, data type of the returned data

  • order : {'C','F'}, optional, that is, the format of the output array is stored in the memory according to the row-first'C' of the C language or the column-first'F' of the Fortran form

Common usage:

# 创建单位矩阵
A = np.eye(2) 
print("A:",A)
A1 = np.eye(3, k=1)
print("A1:",A1)
A2 = np.eye(3, k=-1)
print("A2:",A2)
A3 = np.eye(3, k=-3)
print("A3",A3)
​
"""
A: [[1. 0.]
 [0. 1.]]
A1: [[0. 1. 0.]
 [0. 0. 1.]
 [0. 0. 0.]]
A2: [[0. 0. 0.]
 [1. 0. 0.]
 [0. 1. 0.]]
A3: [[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
"""

Usage in deep learning:

One-hot encoding:

    When building a classification algorithm, tags are usually required to be one_hot encoding. In fact, the tags may be integers, so we all need to convert integers to one_hot encoding.

    One-Hot encoding, also known as one-bit effective encoding, mainly uses N-bit status registers to encode N states. Each state has its own independent register bit, and only one bit is valid at any time.

    One-Hot coding is the representation of categorical variables as binary vectors. This first requires mapping classification values ​​to integer values. Then, each integer value is represented as a binary vector, except for the index of the integer, which is all zero, and it is marked as 1.

Example 1:

#设置类别的数量
num_classes = 10
#需要转换的整数
arr = [1,3,4,5,9]
#将整数转为一个10位的one hot编码
print(np.eye(10)[arr])
"""
运行结果:
[[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
"""

Example 2:

labels=np.array([[1],[2],[0],[1]])
print("labels的大小:",labels.shape,"\n")
#因为我们的类别是从0-2,所以这里是3个类
a=np.eye(3)[1]
print("如果对应的类别号是1,那么转成one-hot的形式",a,"\n")
a=np.eye(3)[2]
print("如果对应的类别号是2,那么转成one-hot的形式",a,"\n")
a=np.eye(3)[1,0]
print("1转成one-hot的数组的第一个数字是:",a,"\n")
#这里和上面的结果的区别,注意!!!
a=np.eye(3)[[1,2,0,1]]
print("如果对应的类别号是1,2,0,1,那么转成one-hot的形式\n",a)
res=np.eye(3)[labels.reshape(-1)]
print("labels转成one-hot形式的结果:\n",res,"\n")
print("labels转化成one-hot后的大小:",res.shape)
"""
运行结果:
labels的大小: (4, 1) 
如果对应的类别号是1,那么转成one-hot的形式 [0. 1. 0.] 
如果对应的类别号是2,那么转成one-hot的形式 [0. 0. 1.] 
1转成one-hot的数组的第一个数字是: 0.0 
如果对应的类别号是1,2,0,1,那么转成one-hot的形式
 [[0. 1. 0.][0. 0. 1.][1. 0. 0.][0. 1. 0.]]
labels转成one-hot形式的结果:
 [[0. 1. 0.][0. 0. 1.] [1. 0. 0.] [0. 1. 0.]]
labels转化成one-hot后的大小:(4, 3)
"""

2、np.identity()

np.identity() : used to create an n*n square matrix

np.identity(n,dtype=None)

  • n: The  int type indicates that the number of rows and columns of the output matrix are both n

  • dtype:   indicates the type of output, the default is float

  • Return value: an array where the main diagonal of n*n is 1, and the rest are 0

The difference between this function and the np.eye() function is that this function can only create a square matrix, that is, N=M

a=np.identity(3)
print(a)
"""
运行结果:
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
"""

3 、 np.mat ()

np.mat() : Used to create a matrix, which must be two-dimensional.

np.mat(data, dtype=None)

  • data: array

  • dtype: indicates the type of output

# 创建矩阵
A = np.mat('1 2 3; 4 5 6; 7 8 9') 
B=np.mat([[1,9,7], [3,9,4]])
print('A:',A)
print('B:',B)
print('A的转置:',A.T)  # 矩阵的转置
print('B逆矩阵:',B.I) # 逆矩阵
"""
运行结果:
A: [[1 2 3] [4 5 6] [7 8 9]]
B: [[1 9 7] [3 9 4]]
A的转置: [[1 4 7] [2 5 8] [3 6 9]]
B逆矩阵:[[-0.17138599  0.20938897]
           [-0.04023845  0.12742176]
           [ 0.21907601 -0.19374069]]
"""

4. np.linalg.inv()

np.linalg.inv() : calculate the inverse matrix

np.linalg.inv(A): where A is the matrix form

# 创建A矩阵
A = np.mat("0 1 2; 1 0 3; 4 -3 8")
print("A:", A)
​
# 计算逆矩阵
inverse = np.linalg.inv(A)
print("inverse of A:", inverse)
"""
运行结果:
A: [[ 0  1  2] [ 1  0  3] [ 4 -3  8]]
inverse of A: [[-4.5  7.  -1.5]
     [-2.   4.  -1. ]  [ 1.5 -2.   0.5]]
"""

5. np.linalg.solve()

np.linalg.solve():  Solve linear matrix equations or linear scalar equations

np.linalg.solve(a, b)

  • a: coefficient matrix

  • b: ordinate or "dependent variable" value

Example 1: Solve the solution of linear equations 3 * x0 + x1 = 9 and x0 + 2 x1 = 8:

# 求解方程系统3 * x0 + x1 = 9和x0 + 2 x1 = 8
a = np.array([[3,1], [1,2]])
b = np.array([9,8])
x = np.linalg.solve(a, b)
print("solution",x)
# 使用dot函数检查是否正确
np.allclose(np.dot(a, x), b)
"""
运行结果:
solution [2. 3.]
True
"""

Example 2: Solve the solution of x0-2*x1+x2=0, 2*x1-8*x2=8, -4*x0+5*x1+9*x2=-9

# 求解线性方程组
# x0 - 2*x1+x2=0,2*x1-8*x2=8, -4*x0+5*x1+9*x2=-9
A = np.mat("1 -2 1;0 2 -8; -4 5 9")
print("A:",A)
b = np.array([0, 8, -9])
print("b:",b)
​
# 调用solve函数求解线性方程组
x = np.linalg.solve(A, b)
print("solution", x)
​
# 使用dot函数检查是否正确
print("check:", np.dot(A, x))
"""
运行结果:
A: [[ 1 -2  1]  [ 0  2 -8] [-4  5  9]]
b: [ 0  8 -9]
solution [29. 16.  3.]
check: [[ 0.  8. -9.]]
"""

6. np.linalg.eig ()

numpy.linalg.eig(A): Calculate the eigenvalue and right eigenvector of the matrix, A is a complex number or real-valued matrix

np.linalg.eigvals(A):  Calculate the eigenvalues ​​of the general matrix

# 求解特征值和特征向量
A = np.mat("3 -2;1 0")
print(A)
​
# 求解特征值
print("eigenvalues",np.linalg.eigvals(A))
​
# 求解特征向量和特征值
# eig函数返回一个元组, 按列排放着特征值和特征向量
eig_values, eig_vectors = np.linalg.eig(A)
​
print("eig_values:", eig_values)
print("eig_vectors:", eig_vectors)
​
# 使用dot函数验证是否正确计算 Ax = ax
for i in range(len(eig_values)):
    print("left:", np.dot(A, eig_vectors[:,i]))
    print("right:", eig_values[i] * eig_vectors[:,i])
 """
 运行结果:
 [[ 3 -2][ 1  0]]
eigenvalues [2. 1.]
eig_values: [2. 1.]
eig_vectors: [[0.89442719 0.70710678][0.4472136  0.70710678]]
left: [[1.78885438][0.89442719]]
right: [[1.78885438][0.89442719]]
left: [[0.70710678][0.70710678]]
right: [[0.70710678][0.70710678]]
 "

7. np.linalg.svd ()

np.linalg.svd():  singular value decomposition, the return value is s, u, v

numpy.linalg.svd(a, full_matrices=1, compute_uv=1)

  • a:   is a matrix of the form (M, N)

  • full_matrices: The value is 0 or 1, and the default value is 1. At this time, the size of u is (M, M) and the size of v is (N, N). Otherwise, the size of u is (M,K), the size of v is (K,N), and K=min(M,N).

  • compute_uv: The value is 0 or 1. The default value is 1, which means u, s, v are calculated. When it is 0, only s is calculated.

# 奇异值分解
A = np.mat("4 11 14;8 7 -2")
​
# 使用svd函数分解矩阵
U, Sigma, V = np.linalg.svd(A, full_matrices=False)
print(U.shape, V.shape, Sigma.shape)
print("svd: ",U * np.diag(Sigma) *V)
"""
​
运行结果:
(2, 2) (2, 3) (2,)
svd:  [[ 4. 11. 14.][ 8.  7. -2.]]
"""

8. np.linalg.det ()

np.linalg.det(A): Calculate the determinant of an array

# 计算矩阵的行列式
A = np.mat("3 4;5 6")
print("行列式:", np.linalg.det(A))
"""
运行结果:
行列式:-1.9999999999999971
​"""

Code acquisition: Scan the QR code below on WeChat and reply "numpy" in the background to get the code

 

recommended article:

Python image recognition-image similarity calculation

Simple verification code recognition-code implementation

Install GPU version of TensorFlow (cuda + cudnn) under win10

TensorFlow-GPU linear regression visualization code, and summary of the problem

Classification of all crawler articles

Selenium-based automated sliding verification code cracking

Crawl 58job, Ganji job and Zhaopin recruitment, and use data analysis to generate echarts graph

Guess you like

Origin blog.csdn.net/weixin_39121325/article/details/96304150