Common matrix operations reprint of --python

https://blog.csdn.net/taxueguilai1992/article/details/46581861

The library provides a python numpy matrix calculation function, so when we need matrix operations, need to import numpy package.

The introduction and use 1.numpy

from numpy import *; # import numpy library functions 
import numpy as np; # this mode when using numpy function, you need to start with np..

2. Create a matrix

Create a matrix of one-dimensional or two-dimensional data

Copy the code
>>> from numpy import *
>>> a1=array([1,2,3])
>>> a1
array([1, 2, 3])
>>> a1=mat(a1)
>>> a1
matrix([[1, 2, 3]])
>>> shape(a1)
(1, 3)
>>> b=matrix([1,2,3])
>>> shape(b)
(1, 3)
Copy the code

Create a common matrix

Copy the code
>>> data1 = mat (zeros (( 3,3))) # 3 * 3 to create a zero matrix, where the matrix parameter is a function zeros tuple type (3,3) 
>>> DATAl 
Matrix ([[0 ., 0., 0.], 
        [0., 0., 0.], 
        [0., 0., 0.]]) 
>>> data2 = MAT (ones ((2, 4))) # create 1 a 2 * 4 matrix, the default floating-point data, if the int type when needed, may be used int = DTYPE 
>>> DATA2 
matrix ([[1., 1., 1., 1.], 
        [1 ., 1., 1., 1.]]) 
>>> DATA3 = MAT (random.rand (2,2 &)) where # is a random module numpy the random module, random.rand (2,2 ) is a two-dimensional array is created, it is necessary to convert it into #matrix 
>>> DATA3 
Matrix ([[.57341802, 0.51016034], 
        [.56438599, 0.70515605]]) 
>>> DATA4, MAT = (the random.randint (10, size = (3,3))) * 3 # 3 generates a random integer between 0 and 10 of the matrix, if necessary to specify a lower bound can be more parameters 
>>> DATA4, 
matrix ([[. 9,. 5,. 6],
        [3, 0, 4],
        [. 6, 0,. 7]]) 
>>> DATA5 = MAT (the random.randint (2,8, size = (2,5))) # generates a random integer matrix between 2-8 
>>> DATA5 
Matrix ([[. 5,. 4,. 6,. 3,. 7], 
        [. 5,. 3,. 3,. 4,. 6]]) 
>>> Data6 = MAT (Eye (2,2 &, DTYPE = int)) to generate a # 2 * diagonal matrix 2 
>>> Data6 
matrix ([[. 1, 0], 
        [0,. 1]]) 

A1 = [l, 2,3] 
A2 = MAT (diag (A1)) to generate a diagonal # 1,2,3 diagonal matrix 
>>> A2 
matrix ([[. 1, 0, 0], 
        [0, 2, 0], 
        [0, 0,. 3]])
Copy the code

3. Common matrix operations

1. Matrix Multiplication

A1 = MAT >>> ([1,2]);       
>>> A2 = MAT ([[1], [2]]); 
>>> A3 = A1 * A2 * # 2 is multiplied by a matrix of 2 * 1 matrix to obtain a matrix of 1 * 1 
>>> A3
matrix ([[. 5]])

2. dot matrix

Corresponding elements of the matrix multiplication

>>>a1=mat([1,1]);
>>>a2=mat([2,2]);
>>>a3=multiply(a1,a2)
>>> a3
matrix([[2, 2]])

Dot matrix

>>>a1=mat([2,2]);
>>>a2=a1*2>>>a2
matrix([[4, 4]])

3. The matrix inversion, transpose 
matrix inversion

Copy the code
A1 = MAT >>> (Eye (2,2 &) * 0.5) 
>>> A1 
matrix ([[0.5, of 0. The], 
        [of 0. The, 0.5]]) 
>>> A2 = # of Matrix matrix A1.i ([[0.5,0], [0, 0.5]]) is the inverse matrix 
>>> A2 
matrix ([[2., of 0. the], 
        [of 0. the, 2.]])
Copy the code

Matrix transpose

Copy the code
>>> a1=mat([[1,1],[0,0]])
>>> a1
matrix([[1, 1],
        [0, 0]])
>>> a2=a1.T
>>> a2
matrix([[1, 0],
        [1, 0]])
Copy the code

4. Calculate the matrix corresponding to the ranks of the maximum, minimum, and.

3>>>a1=mat([[1,1],[2,3],[4,2]])
>>> a1
matrix([[1, 1],
        [2, 3],
        [4, 2]])

Calculated for each column, row, and

Copy the code
>>> a2 = a1.sum (axis = 0 ) # and columns, the matrix here is that 1 * 2 
>>> A2 
Matrix ([[. 7,. 6]]) 
>>> A3 = a1.sum (Axis = 1) and # line, obtained here is the matrix of 3 * 1 
>>> A3 
matrix ([[2], 
        [. 5], 
        [. 6]]) 
>>> A4 = SUM (A1 [1 ,:]) # calculating a first row and all the columns, is a value obtained here 
>>> A4 
. 5 line # 0: 1 + 1; line 2: 3 + 2; lane 3: 4 + 2
Copy the code

Calculate the maximum, minimum and index

Copy the code
>>> a1.max () # a1 calculated maximum value of all elements in the matrix, the result is a value obtained here 
. 4 
>>> A2 = max (a1 [:,. 1]) calculated maximum of the second column #, a matrix obtained here is 1 * 1 
>>> A2 
matrix ([[. 3]]) 
>>> A1 [1,:] max () calculates the maximum value of the second row #, obtained here is a one. value 
. 3 
>>> np.max (A1,0) # calculates the maximum value of all the columns, the max function used here is the numpy 
Matrix ([[. 4,. 3]]) >>> np.max (a1,1 ) # calculates the maximum of all the rows, there is obtained a matrix matrix ([[. 1],
        [. 3],
        [. 4]]) >>> np.argmax (A1,0) # calculated maximum value corresponding to all columns in the column index
Matrix ([[2, 1]]) >>> np.argmax (A1 [1 ,:]) # calculating a second line corresponding to the maximum index of the row
1
Copy the code

The separated and combined matrix 
separated matrix consistent with the partition arrays and lists.

Copy the code
MAT = A >>> (ones ((3,3))) 
>>> A 
Matrix ([[1., 1., 1.], 
        [1., 1., 1.], 
        [1.,. 1 ., 1.]]) 
>>> B = a [. 1:,. 1:] # divided row after row of the second column and second row all elements subsequent 
>>> B 
Matrix ([[1., 1], 
        [1, 1]])
Copy the code

The combined matrix

Copy the code
MAT = A >>> (ones ((2,2 &))) 
>>> A 
Matrix ([[1., 1.], 
        [1., 1.]]) 
>>> B = MAT (Eye (2 )) 
>>> B 
Matrix ([[1., of 0. The], 
        [of 0. The, 1.]]) 
>>> vStack C = ((A, B)) # columns were combined, i.e., increasing the number of rows 
>> > C 
Matrix ([[1., 1.], 
        [1., 1.], 
        [1., of 0. The], 
        [of 0. The, 1.]]) 
>>> hstack D = ((A, B) ) # rows were combined, i.e. the same number of rows, columns extend 
>>> D 
Matrix ([[1., 1., 1., of 0. The], 
        [1., 1., of 0. The, 1.]])
Copy the code

4. The conversion matrix, a list, an array of

The list can be modified, and the elements in the list can make different types of data, as follows:

l1=[[1],'hello',3];

numpy in an array, with all the elements of an array must be the same type, there are several common attributes:

Copy the code
>>>a=array([[2],[1]])
>>> a
array([[2],
       [1]])
>>>dimension=a.ndim
>>> dimension
2
>>>m,n=a.shape
>>> m
2
>>> n
1
>>>number=a.size #元素总个数
>>> number
2
>>>str=a.dtype #元素的类型
>>> str
dtype('int64')
Copy the code

numpy matrix in there with an array of several common attributes. 
Conversion between them:

Copy the code
>>> a1 = [[1,2], [3,2], [5,2]] # listing 
>>> A1 
[[. 1, 2], [. 3, 2], [. 5, 2]] 
> >> a2 = array (a1) # the list into a two-dimensional array 
>>> A2 
array ([[. 1, 2], 
       [. 3, 2], 
       [. 5, 2]]) 
>>> A3 = MAT (A1 ) is converted into a matrix list # 
>>> A3 
matrix ([[. 1, 2], 
        [. 3, 2], 
        [. 5, 2]]) 
>>> array A4 = (A3) to the transformation matrix array # 
>> > A4 
array ([[. 1, 2], 
       [. 3, 2], 
       [. 5, 2]]) 
>>> A41 = a3.getA () # convert into an array matrix
>>> A41
array ([[. 1, 2]
       [3,2-]
       [5,2]]) >>> A5 = a3.tolist () into the transformation matrix list # >>> A5 [[. 1, 2], [. 3, 2], [. 5, 2]] >>> a6 = a2.tolist () # the array into the list >>> a6>> a6 = a2.tolist () # converting the array into a list [[1, 2], [3, 2], [5, 2]]

Copy the code

Here can be found between the three conversion is very simple to note here is that, when the lists are one-dimensional, and converts it into an array matrix, and then by ToList () is converted into a list is not the same, We need to make some minor modifications. as follows:

Copy the code
>>>a1=[1,2,3]   #列表
>>>a2=array(a1)
>>> a2
array([1, 2, 3])
>>>a3=mat(a1)
>>> a3
matrix([[1, 2, 3]])
>>> a4=a2.tolist()
>>> a4
[1, 2, 3]
>>> a5=a3.tolist()
>>> a5
[[1, 2, 3]]
>>> a6=(a4==a5)
>>> a6
False
>>> a7=(a4 is a5[0])
>>> a7
True
Copy the code

Converted into a numerical matrix, the presence of the following conditions:

MAT = Datamat >>> ([. 1]) 
>>> Datamat Val = [0,0] # is the matrix element values acquired at this time, rather than the type of matrix 
>>> Val 
. 1

Guess you like

Origin www.cnblogs.com/llxxs/p/11228119.html