zoukankan      html  css  js  c++  java
  • Numpy入门练习

    
    
     1 #numpy创建一维数组
     2 import numpy as np
     3 my_arry=np.array([1,2,3,4,5])
     4 print (my_arry)
     5 print(my_arry.shape)
     6 my_new_arry=np.zeros((5))
     7 print(my_new_arry)
     8 my_new_arry1=np.ones((5))
     9 print(my_new_arry1)
    10 my_new_arry2=np.random.random((5))
    11 print(my_new_arry2)
    12 
    13 
    14 #numpy创建二维数组
    15 import numpy as np
    16 
    17 my_2d_arry=np.zeros((3,3))
    18 print(my_2d_arry)
    19 my_2d_arry1=np.ones((3,3))
    20 print(my_2d_arry1)
    21 my_arry1=np.array([[4,5],[5,6]])
    22 print(my_arry1)
    23 print(my_arry1[0][1])
    24 my_array_column_2 = my_arry1[:, 1]
    25 print (my_array_column_2)
    26 
    27 
    28 #数组的加减乘除
    29 import numpy as np
    30 a = np.array([[1.0, 2.0], [3.0, 4.0]])
    31 b = np.array([[5.0, 6.0], [7.0, 8.0]])
    32 sum = a + b
    33 difference = a - b
    34 product = a * b
    35 quotient = a / b
    36 print ("Sum = ", sum )
    37 print ("Difference = ", difference)
    38 print ("Product = ", product )
    39 print ("Quotient = ", quotient)
    #numpy创建一维数组
    F:anacondapython.exe F:/code/practice.py
    [1 2 3 4 5]
    (5,)
    [0. 0. 0. 0. 0.]
    [1. 1. 1. 1. 1.]
    [0.43426558 0.12712648 0.26232948 0.98755646 0.65736363]

    进程已结束,退出代码0



    #numpy创建二维数组
    F:anacondapython.exe F:/code/practice.py
    [[0. 0. 0.]
     [0. 0. 0.]
     [0. 0. 0.]]
    [[1. 1. 1.]
     [1. 1. 1.]
     [1. 1. 1.]]
    [[4 5]
     [5 6]]
    5
    [5 6]

    进程已结束,退出代码0

    #数组的加减乘除
    F:anacondapython.exe F:/code/practice.py
    Sum =  [[ 6.  8.]
     [10. 12.]]
    Difference =  [[-4. -4.]
     [-4. -4.]]
    Product =  [[ 5. 12.]
     [21. 32.]]
    Quotient =  [[0.2        0.33333333]
     [0.42857143 0.5       ]]

    进程已结束,退出代码0

    如你所见,乘法运算符执行逐元素乘法而不是矩阵乘法。 要执行矩阵乘法,你可以执行以下操作:

    
    
    matrix_product = a.dot(b) 
    print "Matrix Product = ", matrix_product
    
    

    输出将是:

    
    
    [[19. 22.]
    
    [43. 50.]]


  • 相关阅读:
    1582. Special Positions in a Binary Matrix
    623. Add One Row to Tree
    617. Merge Two Binary Trees
    643. Maximum Average Subarray I
    645. Set Mismatch
    1022. Sum of Root To Leaf Binary Numbers
    835. Image Overlap
    HDU Marriage is Stable (稳定婚姻匹配)
    HDU Stable Match (稳定婚姻匹配)
    HDU Maximum Clique (最大团)
  • 原文地址:https://www.cnblogs.com/MT-IT/p/11020037.html
Copyright © 2011-2022 走看看