zoukankan      html  css  js  c++  java
  • 函数

    • 算数函数:如果参与运算的两个对象 都是 ndarray,并且形状相同,那么会对位彼此之间进行(+ - * /)运算。NumPy 算术函数包含简单的加减乘除: add(),subtract(),multiply() 和 divide()。
    • 数学函数:NumPy 提供了标准的三角函数:sin()、cos()、tan()。
    • 聚合函数:NumPy 提供了很多统计函数,用于从数组中查找最小元素,最大元素,百分位标准差
      和方差等。 具体如下:
    •  代码

       1 #导入numpy
       2 import numpy as np 
       3 a = np.arange(9).reshape(3,3)
       4 b = np.array([10,10,10])
       5 print('加法')
       6 print(np.add(a,b))
       7 #print(a+b)
      8 print('减法') 9 print(np.subtract(b,a)) 10 #print(b-a) 11 12 #out参数的使用 13 y = np.empty((3,3),dtype=np.int) 14 np.multiply(a,10,out=y) 15 print(y) 16 17 #三角函数 18 a = np.array([0,30,60,90]) 19 print(np.sin(a)) 20 21 #around ceil floor 22 a = np.array([1.0,4.55,123,0.567,25.32]) 23 print('around:',np.around(a)) 24 print('ceil:',np.ceil(a)) 25 print('floor:',np.floor(a)) 26 27 #统计函数 28 #power() 29 a = np.arange(1,13).reshape(3,4) 30 print('原数组a') 31 print(a) 32 print('power:',np.power(a,2)) 33 34 #power指定输出结果的用法 35 x = np.arange(5) 36 y = np.zeros(10) 37 np.power(2,x,out=y[5:]) 38 print('power:',x) 39 print(y) 40 41 #median() 42 #一维数组的中位数 43 a = np.array([4,3,2,5,2,1])#对数组排序,数组中元素个数为偶数时 44 #中位数指:中间两个数的平均值 45 print(np.median(a)) 46 a = np.array([4,3,2,5,2]) 47 print(np.median(a)) #奇数个数 48 #二维数组要通过axis指定轴 49 a = np.arange(1,13).reshape(3,4) 50 print(a) 51 print('垂直方向:',np.median(a,axis=0)) 52 print('水平方向:',np.median(a,axis=1)) 53 54 #mean 求平均值 55 a = np.array([4,3,2,5,2]) 56 print(np.mean(a)) 57 #二维数组 axis指定轴求平均值 58 a = np.arange(1,13).reshape(3,4) 59 print(a) 60 print('axis=0 垂直方向',np.mean(a,axis=0)) 61 print('axis=1 水平方向',np.mean(a,axis=1)) 62 63 #sum() max() min() 64 a = np.array([4,3,2,5,2]) 65 print('max:',np.max(a)) 66 print('sum:',np.sum(a)) 67 print('min:',np.min(a)) 68 69 #argmax argmin 70 print('argmin:',np.argmin(a)) 71 print('argmax:',np.argmax(a))
       1 加法
       2 [[10 11 12]
       3  [13 14 15]
       4  [16 17 18]]
       5 减法
       6 [[10  9  8]
       7  [ 7  6  5]
       8  [ 4  3  2]]
       9 [[ 0 10 20]
      10  [30 40 50]
      11  [60 70 80]]
      12 [ 0.         -0.98803162 -0.30481062  0.89399666]
      13 around: [  1.   5. 123.   1.  25.]
      14 ceil: [  1.   5. 123.   1.  26.]
      15 floor: [  1.   4. 123.   0.  25.]
      16 原数组a
      17 [[ 1  2  3  4]
      18  [ 5  6  7  8]
      19  [ 9 10 11 12]]
      20 power: [[  1   4   9  16]
      21  [ 25  36  49  64]
      22  [ 81 100 121 144]]
      23 power: [0 1 2 3 4]
      24 [ 0.  0.  0.  0.  0.  1.  2.  4.  8. 16.]
      25 2.5
      26 3.0
      27 [[ 1  2  3  4]
      28  [ 5  6  7  8]
      29  [ 9 10 11 12]]
      30 垂直方向: [5. 6. 7. 8.]
      31 水平方向: [ 2.5  6.5 10.5]
      32 3.2
      33 [[ 1  2  3  4]
      34  [ 5  6  7  8]
      35  [ 9 10 11 12]]
      36 axis=0 垂直方向 [5. 6. 7. 8.]
      37 axis=1 水平方向 [ 2.5  6.5 10.5]
      38 max: 5
      39 sum: 16
      40 min: 2
      41 argmin: 2
      42 [[ 1  2  3  4]
      43  [ 5  6  7  8]
      44  [ 9 10 11 12]]
      45 垂直方向: [5. 6. 7. 8.]
      46 水平方向: [ 2.5  6.5 10.5]
      47 3.2
      48 [[ 1  2  3  4]
      49  [ 5  6  7  8]
      50  [ 9 10 11 12]]
      51 axis=0 垂直方向 [5. 6. 7. 8.]
      52 axis=1 水平方向 [ 2.5  6.5 10.5]
      53 max: 5
      54 sum: 16
      55 min: 2
      56 argmin: 2
      57 argmax: 3
    正是江南好风景
  • 相关阅读:
    【校招面试 之 C/C++】第23题 C++ STL(五)之Set
    Cannot create an instance of OLE DB provider “OraOLEDB.Oracle” for linked server "xxxxxxx".
    Redhat Linux安装JDK 1.7
    ORA-10635: Invalid segment or tablespace type
    Symantec Backup Exec 2012 Agent for Linux 卸载
    Symantec Backup Exec 2012 Agent For Linux安装
    You must use the Role Management Tool to install or configure Microsoft .NET Framework 3.5 SP1
    YourSQLDba介绍
    PL/SQL重新编译包无反应
    MS SQL 监控数据/日志文件增长
  • 原文地址:https://www.cnblogs.com/monsterhy123/p/12636457.html
Copyright © 2011-2022 走看看