Numpy入门教程:05. 逻辑函数

news/2024/7/5 2:07:26

背景

什么是 NumPy 呢?

NumPy 这个词来源于两个单词 – NumericalPython。其是一个功能强大的 Python 库,可以帮助程序员轻松地进行数值计算,通常应用于以下场景:

  • 执行各种数学任务,如:数值积分、微分、内插、外推等。因此,当涉及到数学任务时,它形成了一种基于 Python 的 MATLAB 的快速替代。
  • 计算机中的图像表示为多维数字数组。NumPy 提供了一些优秀的库函数来快速处理图像。例如,镜像图像、按特定角度旋转图像等。
  • 在编写机器学习算法时,需要对矩阵进行各种数值计算。如:矩阵乘法、求逆、换位、加法等。NumPy 数组用于存储训练数据和机器学习模型的参数。

逻辑函数

真值测试

  • numpy.all(a, axis=None, out=None, keepdims=np._NoValue) Test whether all array elements along a given axis evaluate to True.
  • numpy.any(a, axis=None, out=None, keepdims=np._NoValue) Test whether any array element along a given axis evaluates to True.
import numpy as npa = 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))  # Trueprint(np.all([1.0, np.nan]))  # True
print(np.any([1.0, np.nan]))  # Truea = np.eye(3)
print(np.all(a, axis=0))  # [False False False]
print(np.any(a, axis=0))  # [ True  True  True]

逻辑运算

  • numpy.logical_not(x, *args, **kwargs)Compute the truth value of NOT x element-wise.
  • numpy.logical_and(x1, x2, *args, **kwargs) Compute the truth value of x1 AND x2 element-wise.
  • numpy.logical_or(x1, x2, *args, **kwargs)Compute the truth value of x1 OR x2 element-wise.
  • numpy.logical_xor(x1, x2, *args, **kwargs)Compute the truth value of x1 XOR x2, element-wise.

【例】

import numpy as npx = np.arange(5)
print(np.logical_not(3))  
# False
print(np.logical_not([True, False, 0, 1]))
# [False  True  True False]
print(np.logical_not(x < 3))
# [False False False  True  True]print(np.logical_and(True, False))  
# False
print(np.logical_and([True, False], [True, False]))
# [ True False]
print(np.logical_and(x > 1, x < 4))
# [False False  True  True False]print(np.logical_or(True, False))
# True
print(np.logical_or([True, False], [False, False]))
# [ True False]
print(np.logical_or(x < 1, x > 3))
# [ True False False False  True]print(np.logical_xor(True, False))
# True
print(np.logical_xor([True, True, False, False], [True, False, True, False]))
# [False  True  True False]
print(np.logical_xor(x < 1, x > 3))
# [ True False False False  True]
print(np.logical_xor(0, np.eye(2)))
# [[ True False]
#  [False  True]]

对照

  • numpy.greater(x1, x2, *args, **kwargs) Return the truth value of (x1 > x2) element-wise.
  • numpy.greater_equal(x1, x2, *args, **kwargs) Return the truth value of (x1 >= x2) element-wise.
  • numpy.equal(x1, x2, *args, **kwargs) Return (x1 == x2) element-wise.
  • numpy.not_equal(x1, x2, *args, **kwargs) Return (x1 != x2) element-wise.
  • numpy.less(x1, x2, *args, **kwargs) Return the truth value of (x1 < x2) element-wise.
  • numpy.less_equal(x1, x2, *args, **kwargs) Return the truth value of (x1 =< x2) element-wise.

【例】numpy对以上对照函数进行了运算符的重载。

import numpy as npx = np.array([1, 2, 3, 4, 5, 6, 7, 8])y = x > 2
print(y)
print(np.greater(x, 2))
# [False False  True  True  True  True  True  True]y = x >= 2
print(y)
print(np.greater_equal(x, 2))
# [False  True  True  True  True  True  True  True]y = x == 2
print(y)
print(np.equal(x, 2))
# [False  True False False False False False False]y = x != 2
print(y)
print(np.not_equal(x, 2))
# [ True False  True  True  True  True  True  True]y = x < 2
print(y)
print(np.less(x, 2))
# [ True False False False False False False False]y = x <= 2
print(y)
print(np.less_equal(x, 2))
# [ True  True False False False False False False]

【例】

import numpy as npx = 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 > 20
print(y)
print(np.greater(x, 20))
# [[False False False False False]
#  [False False False False False]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]]y = x >= 20
print(y)
print(np.greater_equal(x, 20))
# [[False False False False False]
#  [False False False False  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]]y = x == 20
print(y)
print(np.equal(x, 20))
# [[False False False False False]
#  [False False False False  True]
#  [False False False False False]
#  [False False False False False]
#  [False False False False False]]y = x != 20
print(y)
print(np.not_equal(x, 20))
# [[ True  True  True  True  True]
#  [ True  True  True  True False]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]]y = x < 20
print(y)
print(np.less(x, 20))
# [[ True  True  True  True  True]
#  [ True  True  True  True False]
#  [False False False False False]
#  [False False False False False]
#  [False False False False False]]y = x <= 20
print(y)
print(np.less_equal(x, 20))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [False False False False False]
#  [False False False False False]
#  [False False False False False]]

【例】

import numpy as npnp.random.seed(20200611)
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.random.randint(10, 40, [5, 5])
print(y)
# [[32 28 31 33 37]
#  [23 37 37 30 29]
#  [32 24 10 33 15]
#  [27 17 10 36 16]
#  [25 32 23 39 34]]z = x > y
print(z)
print(np.greater(x, y))
# [[False False False False False]
#  [False False False False False]
#  [False False  True False  True]
#  [False  True  True False  True]
#  [ True False  True False  True]]z = x >= y
print(z)
print(np.greater_equal(x, y))
# [[False False False False False]
#  [False False False False False]
#  [False False  True False  True]
#  [False  True  True False  True]
#  [ True  True  True False  True]]z = x == y
print(z)
print(np.equal(x, y))
# [[False False False False False]
#  [False False False False False]
#  [False False False False False]
#  [False False False False False]
#  [False  True False False False]]z = x != y
print(z)
print(np.not_equal(x, y))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True False  True  True  True]]z = x < y
print(z)
print(np.less(x, y))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True False  True False]
#  [ True False False  True False]
#  [False False False  True False]]z = x <= y
print(z)
print(np.less_equal(x, y))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True False  True False]
#  [ True False False  True False]
#  [False  True False  True False]]

【例】

import numpy as npx = 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]])np.random.seed(20200611)
y = np.random.randint(10, 50, 5)print(y)
# [32 37 30 24 10]z = x > y
print(z)
print(np.greater(x, y))
# [[False False False False  True]
#  [False False False False  True]
#  [False False False False  True]
#  [False False False  True  True]
#  [False False  True  True  True]]z = x >= y
print(z)
print(np.greater_equal(x, y))
# [[False False False False  True]
#  [False False False False  True]
#  [False False False  True  True]
#  [False False False  True  True]
#  [False False  True  True  True]]z = x == y
print(z)
print(np.equal(x, y))
# [[False False False False False]
#  [False False False False False]
#  [False False False  True False]
#  [False False False False False]
#  [False False False False False]]z = x != y
print(z)
print(np.not_equal(x, y))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True False  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]]z = x < y
print(z)
print(np.less(x, y))
# [[ True  True  True  True False]
#  [ True  True  True  True False]
#  [ True  True  True False False]
#  [ True  True  True False False]
#  [ True  True False False False]]z = x <= y
print(z)
print(np.less_equal(x, y))
# [[ True  True  True  True False]
#  [ True  True  True  True False]
#  [ True  True  True  True False]
#  [ True  True  True False False]
#  [ True  True False False False]]
  • numpy.isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False) Returns a boolean array where two arrays are element-wise equal within a tolerance.
  • numpy.allclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False) Returns True if two arrays are element-wise equal within a tolerance.
  • numpy.all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))

The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.

判断是否为True的计算依据:

np.absolute(a - b) <= (atol + rtol * absolute(b))- atol:float,绝对公差。
- rtol:float,相对公差。

NaNs are treated as equal if they are in the same place and if equal_nan=True. Infs are treated as equal if they are in the same place and of the same sign in both arrays.

【例】比较两个数组是否可以认为相等。

import numpy as npx = np.isclose([1e10, 1e-7], [1.00001e10, 1e-8])
print(x)  # [ True False]x = np.allclose([1e10, 1e-7], [1.00001e10, 1e-8])
print(x)  # Falsex = np.isclose([1e10, 1e-8], [1.00001e10, 1e-9])
print(x)  # [ True  True]x = np.allclose([1e10, 1e-8], [1.00001e10, 1e-9])
print(x)  # Truex = np.isclose([1e10, 1e-8], [1.0001e10, 1e-9])
print(x)  # [False  True]x = np.allclose([1e10, 1e-8], [1.0001e10, 1e-9])
print(x)  # Falsex = np.isclose([1.0, np.nan], [1.0, np.nan])
print(x)  # [ True False]x = np.allclose([1.0, np.nan], [1.0, np.nan])
print(x)  # Falsex = np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
print(x)  # [ True  True]x = np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
print(x)  # True

当前活动


我是 终身学习者“老马”,一个长期践行“结伴式学习”理念的 中年大叔

我崇尚分享,渴望成长,于2010年创立了“LSGO软件技术团队”,并加入了国内著名的开源组织“Datawhale”,也是“Dre@mtech”、“智能机器人研究中心”和“大数据与哲学社会科学实验室”的一员。

愿我们一起学习,一起进步,相互陪伴,共同成长。

后台回复「搜搜搜」,随机获取电子资源!
欢迎关注,请扫描二维码:


http://lihuaxi.xjx100.cn/news/254283.html

相关文章

[工具推荐]用了TrueCrypt 再无难掩之隐

缘起&#xff1a;混在网络n多年了&#xff0c;手头总有些东西不想被别人看到的东西&#xff0c;由于小弟人品好&#xff0c;相貌佳&#xff0c;总有很多朋友喜欢用我的电脑玩啊玩啊……。 近日&#xff0c;冠希、柏芝等前辈以身示法&#xff0c;为我等上了很好一堂关于隐私保护…

WTF!只需一行 Python 代码即可玩 20 几款小游戏

作者 | Python小二来源 | Python小二今天分享一个有趣的 github 项目&#xff1a;https://github.com/kingser/free-python-games&#xff0c;通过该项目&#xff0c;我们只需一行代码即可玩 20 几款小游戏&#xff0c;下面具体来看一下。安装首先&#xff0c;我们进行安装&…

ARM体系结构

工作模式_ufisaus USRFRQIRQSVCABTUNDSYSR0R1R2R3R4R5R6R7R8R8_FRQR9R9_FRQR10R10_FRQR11R11_FRQR12R12_FRQSPSP_FRQSP_IRQSP_SVCSP_ABTSP_UNDLRLR_FRQLR_IRQLR_SVCLR_ABTLR_UNDPCCPSRSPSR_FRQSPSR_IRQSPSR_SVCSPSR_UNDSPSR_ABTUSR(User) &#xff1a;正常程序的执行状态FIQ(Fa…

Spring中毒太深,离开Spring居然连最基本的接口都不会写了

欢迎关注方志朋的博客&#xff0c;回复”666“获面试宝典来源&#xff1a;cnblogs.com/lonely-wolf/p/14127957.html前言Spring 能帮我们做什么控制反转&#xff08;IOC&#xff09;依赖注入&#xff08;DI&#xff09;面向切面编程&#xff08;AOP&#xff09;利用 Spring 来完…

Datawhale x OpenMMLab:共建国际一流开源项目!

CV 技术盛事超级视客营你的热门活动小助手已上线百万算力支持&#xff0c;100 实战项目任你挑选&#xff01;欢迎来到由 OpenMMLab 联合北京超级云计算中心主办、Datawhale 社区及上海市人工智能行业协会协办的计算机视觉任务实战活动——【超级视客营】第一期。我们诚邀全球开…

php获取输入流

uc中的用到的代码(在api/uc.php)代码&#xff1a; $post xml_unserialize(file_get_contents(php://input));&#xfeff; php手册&#xff08;http://cn.php.net/manual/zh/wrappers.php.php&#xff09;说明: php://input allows you to read raw data from the request bod…

Pandas 实用技能,将列(column)排序的几种方法

作者 | 阳哥来源 | Python数据之道Pandas 可以说是 在Python数据科学领域应用最为广泛的工具之一。Pandas是一种高效的数据处理库&#xff0c;它以 dataframe 和 series 为基本数据类型&#xff0c;呈现出类似excel的二维数据。在数据处理过程中&#xff0c;咱们经常需要将列按…

Numpy入门教程:06. 排序,搜索和计数

背景 什么是 NumPy 呢&#xff1f; NumPy 这个词来源于两个单词 – Numerical和Python。其是一个功能强大的 Python 库&#xff0c;可以帮助程序员轻松地进行数值计算&#xff0c;通常应用于以下场景&#xff1a; 执行各种数学任务&#xff0c;如&#xff1a;数值积分、微分、…