Requires NumPy library support
Save Link
Http://www.cnblogs.com/chamie/p/4870078.html
Import and use of 1.numpy
from numpy import *; #导入numpy的库函数import NumPy as NP; #这个方式使用numpy的函数时, need to start with NP.
2. Creation of matrices
Create matrices from one-dimensional or two-dimensional data
>>> from numpy Import *
>>> A1=array ([+])
>>> A1
Array ([1, 2, 3])
>>> A1=mat (A1)
>>> A1
Matrix ([[[1, 2, 3]])
>>> shape (A1)
(1, 3)
>>> B=matrix ([+])
>>> shape (b)
(1, 3)
Create a common matrix
>>>data1=mat (Zeros (3,3)) #创建一个3 0 matrix, the matrix here the Zeros function parameter is a tuple type (3,3) >>> Data1matrix ([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]] >>>data2=mat (Ones (2,4)) #创建一个2 1 matrix, the default is floating-point data, and if required int type, you can use dtype=int>>> data2matrix ([[1., 1., 1., 1.], [1., 1., 1., 1.]] >>>data3=mat (Random.rand (2,2)) #这里的random模块使用的是numpy中的random模块, Random.rand (2,2) creates a two-dimensional array that needs to be converted to # Matrix>>> Data3matrix ([[0.57341802, 0.51016034], [0.56438599, 0.70515605]]) >>>data4=mat (rando M.randint (10,size= (3,3))) #生成一个3 a random integer matrix between 0-10 of a., if you need to specify the nether, you can add a parameter >>> data4matrix ([[9, 5, 6], [3, 0, 4], [6, 0, 7]]) >>>data5=mat (Random.randint (2,8,size= (2,5))) #产生一个2-8 random integer matrix >>> data5matrix ([[5, 4, 6, 3, 7], [ 5, 3, 3, 4, 6]]) >>>data6=mat (eye (2,2,dtype=int)) #产生一个2 * * Diagonal matrix >>> Data6matrix ([[1, 0], [0, 1]]) A1 =[1,2,3]a2=mat (DIAG (A1)) #生成一个对角线为1, 2, 3 diagonal matrices >>> a2mAtrix ([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
3. Common matrix Operations
1. Multiplication of matrices
>>>a1=mat ([up]); >>>a2=mat ([[1],[2]]); >>>a3=a1*a2 matrix multiplied by the 2*1 matrix to get the 1*1 matrix #1
>>> A3
Matrix ([[5]])
2. Matrix Point Multiplication
Multiply matrix corresponding elements
>>>a1=mat ([+]); >>>a2=mat ([2,2]); >>>a3=multiply (A1,A2) >>> A3matrix ([[2, 2]] )
Matrix Point Multiplication
>>>a1=mat ([2,2]); >>>a2=a1*2>>>a2matrix ([[4, 4]])
3. Matrix inversion, Transpose
Matrix inversion
>>>a1=mat (Eye (2,2) *0.5) >>> A1matrix ([[0.5, 0.], [0., 0.5]]) >>>a2=a1. I #求矩阵matrix ([[[0.5,0],[0,0.5]]) inverse matrix >>> A2matrix ([[2., 0.], [0., 2.])
Matrix Transpose
>>> A1=mat ([[[1,1],[0,0]]) >>> A1matrix ([[[1, 1], [0, 0]]) >>> a2=a1. T>>> A2matrix ([[1, 0], [1, 0]])
4. Calculate the maximum, minimum, and value of the matrix corresponding to the column.
3>>>a1=mat ([[1,1],[2,3],[4,2]])
>>> A1
Matrix ([[1, 1],
[2, 3],
[4, 2]])
Calculate each column, row, and
>>>a2=a1.sum (axis=0) #列和, here is 1*2 matrix >>> a2matrix ([[[7, 6]]) >>>a3=a1.sum (Axis=1) #行和, Here is the matrix of 3*1 >>> A3matrix ([[2], [5], [6]]) >>>a4=sum (a1[1,:]) #计算第一行所有列的和, here is a value > >> A45 #第0行: The 2nd line: 2+3; line 3rd: 4+2
Calculate maximum, minimum, and index values
>>>a1.max () #计算a1矩阵中所有元素的最大值, the result here is a numeric 4>>>a2=max (a1[:,1]) #计算第二列的最大值, here is a 1*1 matrix >> > A2matrix ([[3]]) >>>a1[1,:].max () #计算第二行的最大值, here is a numeric 3>>>np.max (a1,0) #计算所有列的最大值, The Max function in NumPy is used here.
Matrix ([[[[4, 3]]) >>>np.max (a1,1) #计算所有行的最大值, here gets a matrix ([[1],
[3],
[4]]) >>>np.argmax (a1,0) #计算所有列的最大值对应在该列中的索引
Matrix ([[[2, 1]]) >>>np.argmax (a1[1,:]) #计算第二行中最大值对应在该行的索引
1
5. Partitioning and merging of matrices
The separation of matrices is consistent with the separation of the list and array.
>>>a=mat (Ones (3,3)) >>> Amatrix ([[1., 1., 1.], [1., 1., 1.], [1., 1., 1]]) >>>b=a[1:,1:] #分割出第二行以后的行和第二列以后的列的所有元素 >>> Bmatrix ([[1., 1.], [1., 1.]])
Merging of matrices
>>>a=mat (Ones (2,2)) >>> Amatrix ([[1., 1.], [1., 1.]) >>>b=mat (Eye (2)) >>> Bmatrix ([[1., 0.], [0., 1.]]) >>>c=vstack ((a)) #按列合并, that is, increase the number of rows >>> Cmatrix ([[1., 1.], [1., 1.], [1., 0.], [0., 1.]) >>>d=hstack ((a)) #按行合并, number of rows, extension columns >>> dmatrix ([[1., 1., 1., 0.], [ 1., 1., 0., 1.])
4. Conversion of matrices, lists, and arrays
The list can be modified, and the elements in the list can make different types of data, as follows:
L1=[[1], ' Hello ', 3];
NumPy array, all elements in the same array must be of the same type, there are several common properties:
>>>a=array ([[[2],[1]]) >>> Aarray ([[2], [1]]) >>>dimension=a.ndim>>> dimension2>>>m,n=a.shape>>> m2>>> n1>>>number=a.size #元素总个数 >>> Number2>>>str=a.dtype #元素的类型 >>> strdtype (' Int64 ')
The matrices in NumPy also have several properties that are common to arrays.
The transitions between them:
>>>a1=[[1,2],[3,2],[5,2]] #列表 >>> a1[[1, 2], [3, 2], [5, 2]]>>>a2=array (A1) # Convert the list to a two-dimensional array >>> a2array ([[1, 2], [3, 2], [5, 2]]) >>>a3=mat (A1) #将列表转化成矩阵 >>> A3matrix ([[1, 2], [3, 2], [5, 2]]) >>>a4=array (A3) #将矩阵转换成数组 >>> A4array ([[1, 2], [ 3, 2], [5, 2]])
>>>a41=a3.geta () #将矩阵转换成数组
>>>a41
Array ([[Up]
[3,2]
[5,2]]) >>>a5=a3.tolist () #将矩阵转换成列表 >>> a5[[1, 2], [3, 2], [5, 2]]>>>a6=a2.tolist () # Convert arrays to lists >>> a6[[1, 2], [3, 2], [5, 2]]
Here you can see that the conversion between the three is very simple, it is important to note that when the list is a one-dimensional, it is converted into arrays and matrices, and then through the ToList () conversion to the list is not the same, need to make some minor changes. As follows:
>>>a1=[1,2,3] #列表 >>>a2=array (A1) >>> A2array ([1, 2, 3]) >>>a3=mat (A1) > >> A3matrix ([[1, 2, 3]]) >>> a4=a2.tolist () >>> a4[1, 2, 3]>>> a5=a3.tolist () >> > a5[[1, 2, 3]]>>> a6= (a4==a5) >>> a6false>>> a7= (A4 is a5[0]) >>> a7true
The matrix is converted into a numeric value, and there is one of the following:
>>> Datamat=mat ([1]) >>> val=datamat[0,0] #这个时候获取的就是矩阵的元素的数值, but no longer the type of matrix >>> val1
Python Learning Note 5 "reprint" Basic matrix Operation _20170618