参考链接: Python中的numpy.logical_or

numpy数学函数和逻辑函数

算术运算numpy.add()numpy.subtract()numpy.multiply()numpy.divide()numpy.floor_divide(x1, x2)numpy.power()numpy.sqrt(x, *args, **kwargs)numpy.square(x, *args, **kwargs)

三角函数numpy.sin()numpy.cos()numpy.tan()numpy.arcsin()numpy.arccos()numpy.arctan()

指数和对数numpy.exp()numpy.log()numpy.exp2()numpy.log2()numpy.log10()

加法函数、乘法函数numpy.sumnumpy.cumsumnumpy.prod 乘积numpy.cumprod 累乘numpy.diff 差值

四舍五入numpy.aroundnumpy.ceilnumpy.floor

杂项numpy.clipnumpy.absnumpy.sign

真值测试numpy.allnumpy.any

数组内容numpy.isnan

逻辑运算numpy.logical_notnumpy.logical_andnumpy.logical_ornumpy.logical_xor

对照numpy.greaternumpy.greater_equalnumpy.equalnumpy.not_equalnumpy.lessnumpy.less_equal

算术运算

numpy.add()

numpy.subtract()

numpy.multiply()

numpy.divide()

numpy.floor_divide(x1, x2)

Return the largest integer smaller or equal to the division of the inputs.

x = np.array([[11, 12, 13, 14, 15],

[16, 17, 18, 19, 20],

[21, 22, 23, 24, 25],

[26, 27, 28, 29, 30],

[31, 32, 33, 34, 35]])

y = x // 2

print(y)

print(np.floor_divide(x, 2))

# [[ 5  6  6  7  7]

#  [ 8  8  9  9 10]

#  [10 11 11 12 12]

#  [13 13 14 14 15]

#  [15 16 16 17 17]]

numpy.power()

numpy.sqrt(x, *args, **kwargs)

Return the non-negative square-root of an array, element-wise.

numpy.square(x, *args, **kwargs)

Return the element-wise square of the input.

x = np.arange(1, 5)

print(x)  # [1 2 3 4]

y = np.sqrt(x)

print(y)

# [1.         1.41421356 1.73205081 2.        ]

print(np.power(x, 0.5))

# [1.         1.41421356 1.73205081 2.        ]

y = np.square(x)

print(y)

# [ 1  4  9 16]

print(np.power(x, 2))

# [ 1  4  9 16]

三角函数

numpy.sin()

numpy.cos()

numpy.tan()

numpy.arcsin()

numpy.arccos()

numpy.arctan()

指数和对数

numpy.exp()

numpy.log()

numpy.exp2()

numpy.log2()

numpy.log10()

加法函数、乘法函数

numpy.sum

numpy.sum(a[, axis=None, dtype=None, out=None, …]) Sum of array elements over a given axis.

通过不同的 axis,numpy 会沿着不同的方向进行操作:如果不设置,那么对所有的元素操作;如果axis=0,则沿着纵轴进行操作;axis=1,则沿着横轴进行操作。但这只是简单的二位数组,如果是多维的呢?可以总结为一句话:设axis=i,则 numpy 沿着第i个下标变化的方向进行操作。

import numpy as np

x = np.array([[11, 12, 13, 14, 15],

[16, 17, 18, 19, 20],

[21, 22, 23, 24, 25],

[26, 27, 28, 29, 30],

[31, 32, 33, 34, 35]])

y = np.sum(x)

print(y)  # 575

y = np.sum(x, axis=0)

print(y)  # [105 110 115 120 125]

y = np.sum(x, axis=1)

print(y)  # [ 65  90 115 140 165]

numpy.cumsum

numpy.cumsum(a, axis=None, dtype=None, out=None) Return the cumulative sum of the elements along a given axis.

聚合函数 是指对一组值(比如一个数组)进行操作,返回一个单一值作为结果的函数。因而,求数组所有元素之和的函数就是聚合函数。ndarray类实现了多个这样的函数。

x = np.array([[11, 12, 13, 14, 15],

[16, 17, 18, 19, 20],

[21, 22, 23, 24, 25],

[26, 27, 28, 29, 30],

[31, 32, 33, 34, 35]])

y = np.cumsum(x)

print(y)

# [ 11  23  36  50  65  81  98 116 135 155 176 198 221 245 270 296 323 351

#  380 410 441 473 506 540 575]

y = np.cumsum(x, axis=0)

print(y)

# [[ 11  12  13  14  15]

#  [ 27  29  31  33  35]

#  [ 48  51  54  57  60]

#  [ 74  78  82  86  90]

#  [105 110 115 120 125]]

y = np.cumsum(x, axis=1)

print(y)

# [[ 11  23  36  50  65]

#  [ 16  33  51  70  90]

#  [ 21  43  66  90 115]

#  [ 26  53  81 110 140]

#  [ 31  63  96 130 165]]

numpy.prod 乘积

numpy.prod(a[, axis=None, dtype=None, out=None, …]) Return the product of array elements over a given axis.

x = np.array([[11, 12, 13, 14, 15],

[16, 17, 18, 19, 20],

[21, 22, 23, 24, 25],

[26, 27, 28, 29, 30],

[31, 32, 33, 34, 35]])

y = np.prod(x)

print(y)  # 788529152

y = np.prod(x, axis=0)

print(y)

# [2978976 3877632 4972968 6294624 7875000]

y = np.prod(x, axis=1)

print(y)

# [  360360  1860480  6375600 17100720 38955840]

numpy.cumprod 累乘

numpy.cumprod(a, axis=None, dtype=None, out=None) Return the cumulative product of elements along a given axis.

x = np.array([[11, 12, 13, 14, 15],

[16, 17, 18, 19, 20],

[21, 22, 23, 24, 25],

[26, 27, 28, 29, 30],

[31, 32, 33, 34, 35]])

y = np.cumprod(x)

print(y)

# [         11         132        1716       24024      360360     5765760

#     98017920  1764322560  -837609728   427674624   391232512    17180672

#    395155456   893796352   870072320  1147043840   905412608  -418250752

#    755630080  1194065920 -1638662144  -897581056   444596224 -2063597568

#    788529152]

y = np.cumprod(x, axis=0)

print(y)

# [[     11      12      13      14      15]

#  [    176     204     234     266     300]

#  [   3696    4488    5382    6384    7500]

#  [  96096  121176  150696  185136  225000]

#  [2978976 3877632 4972968 6294624 7875000]]

y = np.cumprod(x, axis=1)

print(y)

# [[      11      132     1716    24024   360360]

#  [      16      272     4896    93024  1860480]

#  [      21      462    10626   255024  6375600]

#  [      26      702    19656   570024 17100720]

#  [      31      992    32736  1113024 38955840]]

numpy.diff 差值

numpy.diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue) Calculate the n-th discrete difference along the given axis. a:输入矩阵 n:可选,代表要执行几次差值 axis:默认是最后一个

The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively.

A = np.arange(2, 14).reshape((3, 4))

A[1, 1] = 8

print(A)

# [[ 2  3  4  5]

#  [ 6  8  8  9]

#  [10 11 12 13]]

print(np.diff(A))

# [[1 1 1]

#  [2 0 1]

#  [1 1 1]]

print(np.diff(A, axis=0))

# [[4 5 4 4]

#  [4 3 4 4]]

四舍五入

numpy.around

numpy.around(a, decimals=0, out=None) Evenly round to the given number of decimals.

x = np.random.rand(3, 3) * 10

print(x)

# [[6.59144457 3.78566113 8.15321227]

#  [1.68241475 3.78753332 7.68886328]

#  [2.84255822 9.58106727 7.86678037]]

y = np.around(x)

print(y)

# [[ 7.  4.  8.]

#  [ 2.  4.  8.]

#  [ 3. 10.  8.]]

y = np.around(x, decimals=2)

print(y)

# [[6.59 3.79 8.15]

#  [1.68 3.79 7.69]

#  [2.84 9.58 7.87]]

numpy.ceil

上限 numpy.ceil(x, *args, **kwargs) Return the ceiling of the input, element-wise.

numpy.floor

下限 numpy.floor(x, *args, **kwargs) Return the floor of the input, element-wise.

x = np.random.rand(3, 3) * 10

print(x)

# [[0.67847795 1.33073923 4.53920122]

#  [7.55724676 5.88854047 2.65502046]

#  [8.67640444 8.80110812 5.97528726]]

y = np.ceil(x)

print(y)

# [[1. 2. 5.]

#  [8. 6. 3.]

#  [9. 9. 6.]]

y = np.floor(x)

print(y)

# [[0. 1. 4.]

#  [7. 5. 2.]

#  [8. 8. 5.]]

杂项

numpy.clip

裁剪 numpy.clip(a, a_min, a_max, out=None, **kwargs): Clip (limit) the values in an array. Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.

x = np.array([[11, 12, 13, 14, 15],

[16, 17, 18, 19, 20],

[21, 22, 23, 24, 25],

[26, 27, 28, 29, 30],

[31, 32, 33, 34, 35]])

y = np.clip(x, a_min=20, a_max=30)

print(y)

# [[20 20 20 20 20]

#  [20 20 20 20 20]

#  [21 22 23 24 25]

#  [26 27 28 29 30]

#  [30 30 30 30 30]]

numpy.abs

numpy.sign

返回数字符号的逐元素指示

x = np.arange(-5, 5)

print(x)

#[-5 -4 -3 -2 -1  0  1  2  3  4]

print(np.sign(x))

#[-1 -1 -1 -1 -1  0  1  1  1  1]

真值测试

numpy.all

numpy.all(a, axis=None, out=None, keepdims=np._NoValue) Test whether all array elements along a given axis evaluate to True.

numpy.any

numpy.any(a, axis=None, out=None, keepdims=np._NoValue) Test whether any array element along a given axis evaluates to True.

a = np.array([0, 4, 5])

b = np.copy(a)

print(np.all(a == b))  # True

print(np.any(a == b))  # True

b[0] = 1

print(np.all(a == b))  # False

print(np.any(a == b))  # True

print(np.all([1.0, np.nan]))  # True

print(np.any([1.0, np.nan]))  # True

a = np.eye(3)

print(np.all(a, axis=0))  # [False False False]

print(np.any(a, axis=0))  # [ True  True  True]

数组内容

numpy.isnan

numpy.isnan(x, *args, **kwargs) Test element-wise for NaN and return result as a boolean array.

a=np.array([1,2,np.nan])

print(np.isnan(a))

#[False False  True]

逻辑运算

numpy.logical_not

print(np.logical_not(3))

# False

print(np.logical_not([True, False, 0, 1]))

# [False  True  True False]

x = np.arange(5)

print(np.logical_not(x < 3))

# [False False False  True  True]

numpy.logical_and

print(np.logical_and(True, False))

# False

print(np.logical_and([True, False], [True, False]))

# [ True False]

numpy.logical_or

print(np.logical_or(True, False))

# True

print(np.logical_or([True, False], [False, False]))

# [ True False]

numpy.logical_xor

print(np.logical_xor(True, False))

# True

print(np.logical_xor([True, True, False, False], [True, False, True, False]))

# [False  True  True False]

对照

numpy.greater

numpy.greater_equal

numpy.equal

numpy.not_equal

numpy.less

numpy.less_equal

[转载] python:numpy数学函数和逻辑函数相关推荐

  1. [转载] Numpy 数学函数及逻辑函数

    参考链接: Numpy 数学函数 目录 一.向量化和广播 二.数学函数 算数运算 numpy.add numpy.subtract numpy.multiply numpy.divide numpy. ...

  2. if函数or和and混用 php,IF函数与逻辑函数And、OR嵌套运用,让多条件判断变得更加简单...

    IF函数相信许多朋友都已经见到过了,IF函数在Excel函数当中属于非常使用的条件判断类函数,利用这个函数我们可以实现非常高效的许多操作. 但是许多朋友对于这个函数却不是特别的会用,因为会涉及逻辑思维 ...

  3. Matlab:logical函数(逻辑函数)的使用及注意事项

    logical函数(逻辑函数) logical(x):x ~=0时,logical(x)=1:x = 0时,logical(x)=0 注意: 一·logical函数可以将矩阵转化为逻辑矩阵 例: a ...

  4. python transpose函数_转载:numpy中transpose和swapaxes函数讲解

    看<利用python进行数据分析>,有些不大清楚numpy中transpose和swapaxes函数的原理,这篇文章写的比较清楚,转载过来方便个人随时阅读和温习 版权声明:本文为CSDN博 ...

  5. python if and函数_逻辑函数And,OR,IF

    越是碎片化时代,越是要进行系统化学习! 今天7月22日E战到底训练营打卡第十,今天学的是<逻辑函数And,or,if>也是一个非常实用的技能.在许多数据处理中都可以发挥很大作用. 一.介绍 ...

  6. [转载] python numpy 总结

    参考链接: Python中的numpy.compress 先决条件 在阅读这个教程之前,你多少需要知道点python.如果你想重新回忆下,请看看Python Tutorial. 如果你想要运行教程中的 ...

  7. Excel常用函数、逻辑函数(一)

    常用函数 求和函数:sum( 求和范围 ) 计数函数:count( 计数范围 ) :只能对数值类型计数 counta( 计数范围 ) :对所有类型都可以计数 平均值函数:average( 计算的平均值 ...

  8. [转载] python numpy np.finfo()函数 eps

    参考链接: Python中的numpy.log2 用法 finfo函数是根据括号中的类型来获得信息,获得符合这个类型的数型 例1: import numpy as np a=np.array([[1] ...

  9. [转载] python numpy np.exp()函数

    参考链接: Python中的numpy.exp def exp(x, *args, **kwargs): # real signature unknown; NOTE: unreliably rest ...

最新文章

  1. 二叉树的前序遍历,中序遍历,后序遍历学习 (原)
  2. 职责链模式应用——下机(机房重构知识点总结)
  3. R语言观察日志(part6)--初识rMarkdown
  4. linux命令数据盘分多个区,pvmove命令 – 移动物理盘区
  5. Vuejs 计算属性
  6. hdu-5493 Queue(二分+树状数组)
  7. 吴恩达《机器学习》第六章:逻辑回归
  8. 什么是Kubernetes?科普文
  9. fn:startsWith()函数
  10. execle java,Java使用POI操作Excel
  11. 16比9尺寸是多少厘米_16比9(16比9分辨率大全)
  12. 泉州程序员置业小指南
  13. 2021蓝桥杯B组 第I题杨辉三角形
  14. 两个同一牌子无线路由器连接设置步骤!实用!
  15. 视频分析中的那点事情
  16. 高中学业水平计算机时间,2020高中学业水平考试成绩什么时候公布
  17. 已知顺丰快递既可以发陆运,也可以发空运;EMS只能发空运,圆通只能发陆运。 小明现在发送快递,为其设计两个方法,分别用来发空运和陆运。
  18. SnowflakeIdWorker类中SystemUtils.getHostName()在Mac环境下为空,导致空指针异常
  19. 微信小程序访问阿里云flask网页
  20. 外部信号输入源阻抗与ADC内部开关电阻关系

热门文章

  1. 【NOIP2010】【codevs1069】关押罪犯(并查集补集,拆点)
  2. php 实现资料下载功能,学习猿地-php如何实现下载功能
  3. gen阻抗 pcie_COM载板设计之一: PCB的设计
  4. $(document).ready()和onload区别
  5. 计算机与pmac2型卡串口怎么通信,简述PMAC2型运动控制卡
  6. The CC version check failed下出现Failed CC version check. Bailing out! 解决方案
  7. [leetcode]5170. 验证二叉树
  8. UnityShader17:光照属性与阴影
  9. C#基础7:类的定义
  10. TCP和UDP的不同