zoukankan      html  css  js  c++  java
  • NumPy: 通用函数

    通用函数:能同时对数组中所有的元素进行运算的函数

    常见通用函数

    一元通用函数:abs, sqrt, exp,log,ceil,floor,rint/round,trunc,modf,isnan,isinf,cos,sin,tan

     1 rint/round  四舍五入
     2 
     3 In [2]: a = np.arange(0,5, 0.2)
     4 In [3]: a
     5 Out[3]:
     6 array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4,
     7        2.6, 2.8, 3. , 3.2, 3.4, 3.6, 3.8, 4. , 4.2, 4.4, 4.6, 4.8])
     8 In [4]: np.round(a)   #四舍五入
     9 Out[4]:
    10 array([0., 0., 0., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 3., 3., 3., 3.,
    11        3., 4., 4., 4., 4., 4., 5., 5.])
    12 
    13 In [35]: np.floor(3.5)  # floor向下取整
    14 Out[35]: 3.0
    15 
    16 In [36]: np.ceil(3.5)  # ceil向上取整
    17 Out[36]: 4.0
    18 
    19 In [37]: np.trunc(-3.1)  # 向零取整
    20 Out[37]: -3.0
    21 
    22 In [38]: np.trunc(3.1)
    23 Out[38]: 3.0
    24 
    25 
    26 In [39]: np.abs(3)  # abs绝对值,
    27 Out[39]: 3
    28 
    29 In [40]: np.abs(-3)
    30 Out[40]: 3
    31 
    32 In [41]: np.abs(0)
    33 Out[41]: 0
    34 
    35 # modf返回两个数组,一个为小数部分,一个为整数部分
    36 In [43]: a = np.array([1.2, 3.2, 3.3, 4,5, 6])
    37 
    38 In [44]: a
    39 Out[44]: array([1.2, 3.2, 3.3, 4. , 5. , 6. ])
    40 
    41 In [45]: np.modf(a)
    42 Out[45]: (array([0.2, 0.2, 0.3, 0. , 0. , 0. ]), array([1., 3., 3., 4., 5., 6.]))
    43 
    44 In [46]: b,c = np.modf(a)
    45 
    46 In [47]: b
    47 Out[47]: array([0.2, 0.2, 0.3, 0. , 0. , 0. ])
    48 
    49 In [48]: c
    50 Out[48]: array([1., 3., 3., 4., 5., 6.])

    二元函数:add, substract, multiply,divide,power, mod,maximun,mininum

     1 In [15]: a = np.array([1,2,3,4,5])
     2 
     3 In [16]: b = np.array([5,4,3,2,1])
     4 
     5 In [17]: a
     6 Out[17]: array([1, 2, 3, 4, 5])
     7 
     8 In [18]: b
     9 Out[18]: array([5, 4, 3, 2, 1])
    10 
    11 In [19]: np.maximum(a,b)   # 取两个数组每个位置上的最大值
    12 Out[19]: array([5, 4, 3, 4, 5])
    13 
    14 In [20]: np.minimum(a,b)   # 取两个数组最小值
    15 Out[20]: array([1, 2, 3, 2, 1])

    NumPy:数学和统计方法

    常用函数:

     1 In [52]: a = np.array([1,2,3,4,5])
     2 In [26]: a.sum()  #sum求和
     3 Out[26]: 15
     4 
     5 In [27]: a.cumsum()  #cumsum 求累计和,数组元素相加的和
     6 Out[27]: array([ 1,  3,  6, 10, 15])
     7 
     8 In [28]: a.mean()  #mean求平均值
     9 Out[28]: 3.0
    10 
    11 In [29]: a.std()  #std求标准差
    12 Out[29]: 1.4142135623730951
    13 
    14 In [30]: a.var()  #var求方差
    15 Out[30]: 2.0
    16 
    17 In [31]: a.min()  #min求最小值
    18 Out[31]: 1
    19 
    20 In [32]: a.max()  #max求最大值
    21 Out[32]: 5
    22 
    23 In [33]: a.argmin()  #argmin求最小索引值
    24 Out[33]: 0
    25 
    26 In [34]: a.argmax()  #argmax求最大索引值
    27 Out[34]: 4

    NumPy:随机生成数

     1 rand 给定形状产生随机数组(0到1之间的数)
     2 randint 给定形状产生随机整数
     3 choice  给定形状产生随机选择
     4 shuffle  和random,shuffle相同
     5 uniform 给定形状产生随机数组
     6 
     7 
     8 In [54]: np.random.randint(1, 10)
     9 Out[54]: 8
    10 
    11 In [55]: 
    12 
    13 In [55]: np.random.randint(1, 10)
    14 Out[55]: 5
    15 
    16 In [56]: np.random.randint(1, 10, 10)  # 给定形状10,产生10个随机数
    17 Out[56]: array([2, 5, 7, 5, 6, 8, 2, 1, 4, 6])
    18 
    19 In [57]: 
    20 
    21 In [57]: np.random.randint(1, 10, (3,5))  # 给定形状,产生3行5列的数组
    22 Out[57]: 
    23 array([[9, 5, 5, 5, 2],
    24        [6, 9, 1, 3, 8],
    25        [7, 7, 5, 6, 7]])
    26 
    27 In [58]: np.random.rand()
    28 Out[58]: 0.44549833476797984
    29 
    30 In [59]: np.random.rand(10)  # 给定形状,产生10个随机数
    31 Out[59]: 
    32 array([0.86313697, 0.39311928, 0.29385099, 0.93888756, 0.1120862 ,
    33        0.12951437, 0.73375749, 0.63620282, 0.75099299, 0.08967599])
  • 相关阅读:
    Git官方推荐用书
    二叉树-补习
    POJ 2251 Dungeon Master(三维BFS)
    Codeforces 675C Money Transfers (思维题)
    HDU 1195 Open the Lock(BFS)
    HDU 1010 Tempter of the Bone(DFS+剪枝)
    POJ 1426 Find The Multiple(DFS,BFS)
    POJ 3216 Prime Path (BFS)
    POJ 3278 Catch that cow(BFS)
    UVa 572 Oil Deposits(简单DFS)
  • 原文地址:https://www.cnblogs.com/YingLai/p/9286491.html
Copyright © 2011-2022 走看看