[Python study notes] Numpy example

Numpy example

  1. Generate a one-dimensional array with a starting value of 5, an end value of 15, and 10 samples

    import numpy as np
    a=np.arange(5,15)
    #a=np.array([5,6,7,8,9,10,11,12,13,14])
    print(a)
    

    [ 5 6 7 8 9 10 11 12 13 14]

  2. Output diagonal matrix

    #第一种方法
    import numpy as np
    a=np.zeros((3,3))
    for i in range(3):
        for j in range(3):
            if(i==j):
                a[i][i]=1.0
            elif(i!=j):
                a[i][j]=0.0
    print(a)
    

    [[ 1. 0. 0.]

    [ 0. 1. 0.]

    [ 0. 0. 1.]]

    #第二种方法
    import numpy as np
    b=np.identity(3)#也可以用eye函数
    print(b)
    

    [[ 1. 0. 0.]

    [ 0. 1. 0.]

    [ 0. 0. 1.]]

  3. Create an array with a boundary value of 1 and internal 0

    #第一种方法
    import numpy as np
    a = np.zeros((10,10),dtype=float)
    for i in range(0,10):
        for j in range(0,10):
            if i == 0 or i == 9 or j == 0 or j == 9:
                a[i][j] = 1
    print(a)
    

    [[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]

    #第二种方法
    import numpy as np
    S = np.ones((10, 10))
    S[1:9, 1:9] = 0
    print(S)
    

    [[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

    [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]

  4. Generate a random integer matrix of 5 rows and 10 columns, with numbers ranging from 0 to 100

    data1=np.random.randint(100,size=(5,10)) 
    print(data1)
    print(data1[:2,:])
    

    [[74 10 60 12 66 30 88 12 78 0]

    [10 71 54 62 51 0 47 82 3 67]

    [68 44 2 85 82 36 90 99 63 71]

    [95 25 7 19 85 49 7 15 7 15]

    [47 38 89 52 43 93 97 84 10 24]]

    [[74 10 60 12 66 30 88 12 78 0]

    [10 71 54 62 51 0 47 82 3 67]]

  5. Knowing that the matrices A and B are as follows, find the addition and multiplication of the two matrices

    A=np.array([
        [1,2,3],
        [4,5,6],
        [7,8,9]
    ])
    B=np.array([
        [1,1,1],
        [1,1,1],
        [1,1,1]
    ])
    print(A+B)
    print(A.dot(B))
    

    [[ 2 3 4]

    [ 5 6 7]

    [ 8 9 10]]

    [[ 6 6 6]

    [15 15 15]

    [24 24 24]]

    import numpy as np
    
    A=[[1,2,3],[4,5,6],[7,8,9]]
    B=[[1,1,1],[1,1,1],[1,1,1]]
    A=np.array(A)
    B=np.array(B)
    print("和为:",A+B)
    print("乘积为:",A*B)
    

    The sum is: [[2 3 4]

    [ 5 6 7]

    [ 8 9 10]]

    The product is: [[1 2 3]

    [4 5 6]

    [7 8 9]]

Published 18 original articles · Likes6 · Visits 1859

Guess you like

Origin blog.csdn.net/qq_43479203/article/details/105345194