. 1 Import numpy AS NP 2 . 3 # matrix is a two-dimensional 4 # used to create a matrix mat == asmatrix 5 # 6 # # string using the mat into a matrix . 7 M1 = np.mat ( " . 1 2. 3; 4 5 . 6;. 7. 8. 9 " ) . 8 # np.asmatrix () is equivalent to np.mat () . 9 10 # using nested list into the mat matrix . 11 M1 = np.mat ([[. 1, 2,. 3], [ . 4,. 5,. 6], [. 7,. 8,. 9 ]]) 12 is # M1 = np.mat ([[[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9]] ]) # not possible, can not be converted to two nested lists matrix 13 is 14 # using the mat into an array matrix 15= np.array ARR ([[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9 ]]) 16 # as long as the array, inside the real values are two-dimensional, it can be conversion matrix . 17 ARR = np.array ([[[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9]]]) # only three-dimensional array comprising a two-dimensional array, can be into matrix 18 is # ARR = np.array ([[[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9]], [[. 1, 2,. 3], [. 4, 5, 6], [7, 8, 9]]]) # not possible . 19 ARR = np.array ([[[[. 1, 2,. 3], [. 4, 5, 6], [7, 8, 9]]]]) # only two-dimensional array containing a four-dimensional array, can be converted into matrix 20 is m1 = np.mat (ARR) 21 is Print ( " m1: \ n- " , m1) 22 is Print ( " type m1 of: \ the n- " , of the type (M1)) 23 24 # using a matrix to create a matrix 25 # can use the matrix to a string, nested list, a two-dimensional array into a matrix of 26 # as long as the array of internal real value is two-dimensional, it can be transformed into a matrix 27 M1 = NP .matrix ( " . 1 2. 3;. 4. 5. 6;. 7. 8. 9 " ) 28 M1 = np.matrix ([[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9 ]]) 29 # M1 = np.matrix ([[[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9]]]) is not # 30 ARR = np.array ([[. 1 , 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9 ]]) 31 is ARR = np.array ([[[. 1, 2,. 3], [. 4,. 5,. 6], [. 7 ,. 8,. 9 ]]]) 32 ARR = np.array ([[[[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9 ]]]]) 33 is M1 = NP. matrix (arr) 34 is Print ( " m1: \ n- " , m1) 35 Print ( " Type of m1: \ n- " , type (m1)) 36 # directly create a matrix can be used MAT asmatrix matrix 37 [ # MAT and asmatrix same 38 # than the matrix less a Copy 39 # recommended mat or asmatrix 40 41 is 42 is # use bmat to combining matrix 43 is L1 = [[. 1, 2], [2,. 1 ]] 44 is L2 = [[0,. 1], [0,. 1 ]] 45 of arr1 = np.array (L1) 46 is arr2 is = np.array (L2) 47 Print ( " of arr1: \ n- " , of arr1) 48 Print ( " arr2 is: \ n- " , arr2 is) 49 50 # use an array of strings can BMAT --- string list conversion or combining matrix 51 is M2 = np.bmat ( " of arr1 arr2 is; arr2 is of arr1 " ) 52 is M2 = np.bmat ( " L1 L2; L1 L2 " ) 53 is # using bmat array may be a list, or a list of nested form into a matrix composition 54 is M2 = np.bmat ( [[of arr1, arr2 is], [arr2 is, of arr1]]) 55 M2 = np.bmat ([[L1, L2], [L2, L1]]) 56 is 57 is # # arr = np.array(arr1, arr2) # 不可以 58 print("~" * 60) 59 arr = np.array([[arr1, arr2], [arr2, arr1]]) 60 print(arr) 61 print(arr.ndim) 62 # print(arr.shape) 63 print("~" * 60) 64 65 # m2 = np.bmat(arr) 66 print("m2:\n", m2) 67 print("m2的类型:\n", type(m2))
[Data Analysis & Data Mining] create a matrix of
Guess you like
Origin www.cnblogs.com/Tree0108/p/12115448.html
Recommended
Ranking