Python numpy package usage summary

1. The difference between array and mat

 

The numpy package in Python is a scientific computing package. This package will be used in most cases when performing scientific calculations, but the difference between the two functions array and mat should be noted. The type of data is likely to be a hard-to-find cause of a bug in the program (self-lesson)

if __name__ == '__main__':
    # 一维
    two = [1, 3, 5, 2, 3, 2]
    two1 = np.array(two)
    print(two)  # [1, 3, 5, 2, 3, 2]  type:list
    print(two1)  # [1 3 5 2 3 2]  type:tuple
    print(two1.shape)  # (6,)
    print(two1.tolist())  # [1, 3, 5, 2, 3, 2]  type:list
    print(len(two1.tolist()))  # 6
    two2 = np.mat(two)
    print(two2)  # [[1 3 5 2 3 2]]  type:tuple
    print(two2.shape)  # (1, 6)
    print(two2.tolist())  # [[1, 3, 5, 2, 3, 2]]  type:list
    print(len(two2.tolist()))  # 1

You can see that array and mat are still different.

  • array:  directly convert the list into an array, including but not limited to a one-dimensional list
  • mat:  convert the data in the list into a matrix

So after using the command tolistto convert the array, it can be converted to the original look, and the mat type will be different from the original.

The difference here is obvious in the one-dimensional case. There is no difference for the following multi-latitude situation.

For multi-dimensional lists, there is basically no difference between array and mat: but there is a difference in * operation

 # 多维数组
    two = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]
    two1 = np.array(two)
    print(two)
    '''
    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]
    '''
    print(two1)
    '''
    [[ 1  2  3  4]
     [ 5  6  7  8]
     [ 9 10 11 12]
     [13 14 15 16]
     [17 18 19 20]]
    '''
    print(two1.shape)  # (5, 4)
    print(two1.tolist())
    '''
    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]
    '''
    print(len(two1.tolist()))  # 5
    two2 = np.mat(two)
    print(two2)
    '''
    [[ 1  2  3  4]
     [ 5  6  7  8]
     [ 9 10 11 12]
     [13 14 15 16]
     [17 18 19 20]]
    '''
    print(two2.shape)  # (5, 4)
    print(two2.tolist())
    '''
    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]
    '''
    print(len(two2.tolist()))  # 5

    two3=two1.T # 数组转置
    two4=two2.T # 矩阵转置
    Y=two1 * two1 # 数组相乘:各元素对应相乘
    '''
    [[  1   4   9  16]
     [ 25  36  49  64]
     [ 81 100 121 144]
     [169 196 225 256]
     [289 324 361 400]]
    '''
    #Y1 = np.dot(two1, two1)  # dot()函数是矩阵乘,而*则表示逐个元素相乘; 5x4 和 5x4 矩阵乘报错
    Y1 = np.dot(two1, two1.T) # 可以将two1其转置 然后矩阵乘
    '''
    [[  30   70  110  150  190]
     [  70  174  278  382  486]
     [ 110  278  446  614  782]
     [ 150  382  614  846 1078]
     [ 190  486  782 1078 1374]]
    '''
    Y4 = two2 * two2.T  # 矩阵相乘:等同于 p.dot(two2, two2.T)
    '''
    [[  30   70  110  150  190]
     [  70  174  278  382  486]
     [ 110  278  446  614  782]
     [ 150  382  614  846 1078]
     [ 190  486  782 1078 1374]]
    '''

Summary: The difference between array (array) and mat (matrix) in numpy

Matrix is ​​a branch of array, matrix and array are common in many cases, whichever you use is the same. But at this time, the official suggested that if both are universal, then choose array, because array is more flexible and faster, many people also translate two-dimensional array into a matrix.
But the advantage of matrix is ​​the relatively simple operation symbol. For example, the multiplication of two matrices uses the symbol *, but the multiplication of array can not be used in this way
. The advantage of array.dot () The advantage of array is not only to represent two dimensions, but also Represents 3, 4, 5 ... dimensions, and in most Python programs, array is also more commonly used.
 

Use of dot method 

Reference blog:  https://www.cnblogs.com/Shawnyi/p/10370815.html

 

array (array) and mat (matrix)

      X [:, 0] is to take the elements of column 0 of all rows of the matrix X, and X [:, 1] is to take the elements of column 1 of all rows.

       X [:, m: n] takes the data from columns m to n-1 in all rows of matrix X, including left but not right.

       X [0 ,:] is to take all the elements of the 0th row of the matrix X, X [1 ,:] is to take all the elements of the first row of the matrix X.

    print(two1[0,:])
    '''
    [1 2 3 4]
    '''
    print(two2[:,0])
    '''
    [[ 1]
     [ 5]
     [ 9]
     [13]
     [17]]
    '''

 

Published 110 original articles · 22 praises · 70,000 views

Guess you like

Origin blog.csdn.net/yangshaojun1992/article/details/104384633