(First chapter above)
1.2 Object, matrix and vectorization programming
1.2.1 Objects and dimensions (slightly)
1.2.2 Primary Knowledge Matrix (abbreviated)
1.2.3 Vectorization Programming and GPU operations (slightly)
1.2.4 Understanding mathematical formulas and NumPy matrix operations
1. Initialization of The Matrix
# Coding:utf-8 Import # Import NumPy Package # Create a 3*5 full 0 matrix and a full 1 matrix Myzero = Np.zeros ([3,5])#3*5 's full 0 matrix print= Np.ones ([ 3,5]##3 full 1 matrix print Myzero
Output Result:
Connected to Pydev Debugger (build 141.1580) [[0]. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 1. 1. 1. 1. 11. 1. 1. 1. 11. 1. 1. 1. 1.]
# Generate random matrix Myrand = Np.random.rand (3,4)#3 Rows 4 columns of the random number matrix between 0~1 and print0.14689327 0.15077077 0.88018968 0.751123480.30944489 0.77563281 0.82905038 0.254303670.53958541 0.89695376 0.90681161 0.25453046]]
# Unit array myeye = Np.eye (3)#3*3 of the matrix print1. 0. 0.] [0. 1. 0.] [0. 0. 1.]
2. Element operations of matrices
The element operations of matrices refer to the subtraction operations of matrices at the element level.
# The addition and subtraction of elements: The condition is that the number of rows and columns of the matrix must be the same from numpy import *# import numpy package myones = ones ([3,3]) # Span style= "COLOR: #008000" >3*3 full 1 matrix MyEye = Eye (3 print myones+myeye print Myones-myeye is as follows: [[ 2]. 1.1. [ 1.2. 1. [ 1.1. 2.] [0. 1. 1. [ 1.0. 1. [ 1.1. 0.]]
# matrix multiplication Mymatrix = Mat ([[1,2,3],[4,5,6],[7,8,9=]print A *mymatrix output: [[10 20 30 ] [[70 8090]]
# sum of all elements of the Matrix Mymatrix = Mat ([[[1,2,3],[4,5,6],[7,8,9]])print mymatrix.sum () output: 45
" " The product of each element of the matrix: the multiplication of the points of the matrix by the corresponding elements of the same dimension. When the dimensions of the matrix are not the same, the dimensions are expanded to a consistent form according to certain broadcasts '= Mat ([[1,2,3],[4,5,6],[7,8,9= 1.5*ones ([3,3 ])print multiply (mymatrix1,mymatrix2) output: [[ 1.5 3. 4.5] [ 6. 7.5 910.5 . 13.5]]
# n Powers of each element of the matrix: n=2mymatrix1 = Mat ([[[1,2,3],[4,5,6],[7,8,9]])print power (mymatrix1,2 1 4 9] [[49 6481]]
# matrix multiplied by matrix mymatrix1 = Mat ([[1,2,3],[4,5,6],[7,8,9 = Mat ([[[1],[2],[3]])print mymatrix1*mymatrix2 output: [[[][+][50]]
# Transpose of the matrix mymatrix1 = Mat ([[[1,2,3],[4,5,6],[7,8,9]])print mymatrix1. The transpose of the # Matrix to the transpose of the T # Matrix print mymatrix1 output results as follows: [[1 4 7 ][2 5 8] [3 6 9]][[1 2 3] [4 5 6] [7 8 9]]
Mymatrix = mymatrix1[0]#Slice by rowPrintU"Slice by row:", Mymatrixmymatrix= mymatrix1. T[0]#Slice by columnPrintU"Slice by column:", Mymatrixmymatrix= Mymatrix1.copy ()#replication of matricesPrintU"Copy matrix:", Mymatrix#ComparePrintU"Comparison of matrix elements: \ n",mymatrix<mymatrix1. T output: Number of rows and columns of the matrix:7 ·Slice by row: [[1 2 3]] By column slice: [[1 4 7]] Copy matrix: [[1 2 3] [4 5 6] [7 8 9] ] Matrix element comparison: [[False True True] [false true] [false]]
Source: "Machine learning algorithm principles and programming practices" Zheng Jie
"Machine learning algorithms principles and programming practices" learning notes (II.)