numpy教程:逻辑函数Logic functions
http://blog.csdn.net/pipisorry/article/details/48208433
真值测试Truth value testing
all(a[, axis, out, keepdims]) | Test whether all array elements along a given axis evaluate to True. |
any(a[, axis, out, keepdims]) | Test whether any array element along a given axis evaluates to True. |
只要数组中有一个值为True,则any()返回True;而只有数组的全部元素都为True,all()才返回True。
也可以直接当成array数组的方法使用。
判断numpy数组是否为空
if a.size:
print('array is not empty')
如果通过python列表,把一个列表作为一个布尔值会产生True如果有项目,False如果它是空的。
lst = []
if lst:
print "array has items"
if not lst:
print "array is empty"
判断numpy数组中是否有True
array.any()
数组内容Array contents
isfinite(x[, out]) | Test element-wise for finiteness (not infinity or not Not a Number). |
isinf(x[, out]) | Test element-wise for positive or negative infinity. |
isnan(x[, out]) | Test element-wise for NaN and return result as a boolean array. |
isneginf(x[, y]) | Test element-wise for negative infinity, return result as bool array. |
isposinf(x[, y]) | Test element-wise for positive infinity, return result as bool array. |
numpy.isnan
numpy判断一个元素是否为np.NaN,判断某元素是否是nan
numpy.isnan(element)
Note: 不能使用array[0] == np.NaN,总是返回False!
numpy数组元素替换numpy.nan_to_num(x)
判断某元素是否是nan,inf,neginf,如果是,nan换为0,inf换为一个非常大的数,neginf换为非常小的数
numpy.nan_to_num(x)
Replace nan with zero and inf with finite numbers.
Returns an array or scalar replacing Not a Number (NaN) with zero, (positive) infinity with a very large number and negative infinity with a very small (or negative) number.
数组类型测试Array type testing
iscomplex(x) | Returns a bool array, where True if input element is complex. |
iscomplexobj(x) | Check for a complex type or an array of complex numbers. |
isfortran(a) | Returns True if the array is Fortran contiguous but not C contiguous. |
isreal(x) | Returns a bool array, where True if input element is real. |
isrealobj(x) | Return True if x is a not complex type or an array of complex numbers. |
isscalar(num) | Returns True if the type of num is a scalar type. |
逻辑操作Logical operations
logical_and(x1, x2[, out]) | Compute the truth value of x1 AND x2 element-wise. |
logical_or(x1, x2[, out]) | Compute the truth value of x1 OR x2 element-wise. |
logical_not(x[, out]) | Compute the truth value of NOT x element-wise. |
logical_xor(x1, x2[, out]) | Compute the truth value of x1 XOR x2, element-wise. |
两个0-1array相与操作
判断两个0-1array有多少个相同的1, 两种方式
rate = np.count_nonzero(np.logical_and(fs_predict_array, ground_truth_array))rate = np.count_nonzero(fs_predict_array * ground_truth_array)
不过fs_predict_array * ground_truth_array返回的是0-1array,而np.logical_and(fs_predict_array ,ground_truth_array)返回的是True-False array,但是都可以使用sum()得到1或者True的数目。
lz亲测下面的logical_and操作运行速度更快,没有count_nonzero会更快。
比较Comparison
allclose(a, b[, rtol, atol, equal_nan]) | Returns True if two arrays are element-wise equal within a tolerance. |
isclose(a, b[, rtol, atol, equal_nan]) | Returns a boolean array where two arrays are element-wise equal within a tolerance. |
array_equal(a1, a2) | True if two arrays have the same shape and elements, False otherwise. |
array_equiv(a1, a2) | Returns True if input arrays are shape consistent and all elements equal. |
greater(x1, x2[, out]) | Return the truth value of (x1 > x2) element-wise. |
greater_equal(x1, x2[, out]) | Return the truth value of (x1 >= x2) element-wise. |
less(x1, x2[, out]) | Return the truth value of (x1 < x2) element-wise. |
less_equal(x1, x2[, out]) | Return the truth value of (x1 =< x2) element-wise. |
equal(x1, x2[, out]) | Return (x1 == x2) element-wise. |
not_equal(x1, x2[, out]) | Return (x1 != x2) element-wise. |
allclose
如果两个数组在tolerance误差范围内相等,则返回True。
from: http://blog.csdn.net/pipisorry/article/details/48208433
ref: Logic functions
numpy教程:逻辑函数Logic functions的更多相关文章
- numpy教程
[转]CS231n课程笔记翻译:Python Numpy教程 原文链接:https://zhuanlan.zhihu.com/p/20878530 译者注:本文智能单元首发,翻译自斯坦福CS231n课 ...
- 【转】numpy教程
[转载说明] 本来没有必要转载的,只是网上的版本排版不是太好,看的不舒服.所以转过来,重新排版,便于自己查看. 基础篇 NumPy的主要对象是同种元素的多维数组. 这是一个所有的元素都是一种类型.通过 ...
- 转:Numpy教程
因为用到theano写函数的时候饱受数据结构困扰 于是上网找了一篇numpy教程(theano的数据类型是基于numpy的) 原文排版更好,阅读体验更佳: http://phddreamer.blog ...
- np.random.random()函数 参数用法以及numpy.random系列函数大全
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9751471.html 1.np.random.random()函数参数 np.random.r ...
- Python 机器学习库 NumPy 教程
0 Numpy简单介绍 Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy.matplotlib一起使用.其实,list已经提供了类似于矩阵的表示形式,不过numpy ...
- scipy几乎实现numpy的所有函数
NumPy和SciPy的关系? numpy提供了数组对象,面向的任何使用者.scipy在numpy的基础上,面向科学家和工程师,提供了更为精准和广泛的函数.scipy几乎实现numpy的所有函数, ...
- ajax的使用:(ajaxReturn[ajax的返回方法]),(eval返回字符串);分页;第三方类(page.class.php)如何载入;自动加载函数库(functions);session如何防止跳过登录访问(构造函数说明)
一.ajax例子:ajaxReturn("ok","eval")->thinkphp中ajax的返回值的方法,返回参数为ok,返回类型为eval(字符串) ...
- 《理解 ES6》阅读整理:函数(Functions)(一)Default Parameter Values
对于任何语言来说,函数都是一个重要的组成部分.在ES6以前,从JavaScript被创建以来,函数一直没有大的改动,留下了一堆的问题和很微妙的行为,导致在JavaScript中使用函数时很容易出现错误 ...
- php中的匿名函数(Anonymous functions)和闭包函数(closures)
一:匿名函数 (在php5.3.0 或以上才能使用) php中的匿名函数(Anonymous functions), 也叫闭包函数(closures), 允许指定一个没有名称的函数.最常用的就是回调函 ...
随机推荐
- JavaC命令不能被执行尴尬问题解决
安装和配置环境变量都按着流程在,但在最后的检验时,发现Java Java -version 都能运行,唯独Javac 报"不能识别命令"错误信息,下面列出我遇到一个尴尬问题 在 ...
- ACM Smallest Difference
给定一些不同的十进制数字(distinct decimal digits),您可以通过选择这些数字的非空子集(non-empty subset)并以某种顺序编写它们,从而形成一个整数. 剩下的数字可以 ...
- Bootstrap3 栅格系统-实例:流式布局容器
如何让小屏幕设备上所有列不堆叠在一起,使用针对超小屏幕和中等屏幕设备所定义的类就可以做到,即 .col-xs-* 和 .col-md-*.举例: <!-- Stack the columns o ...
- linux下安装apache(httpd-2.4.3版本)各种坑
博主的linux是ubuntu 14.04.3. 在安装apache最新版httpd-2.4.3的时候遇到各种坑. 先提供安装apache httpd-2.4.3所需要的包,博主已经整理好,下载地址: ...
- ROS机器人程序设计(原书第2版)补充资料 (叁) 第三章 可视化和调试工具
ROS机器人程序设计(原书第2版)补充资料 (叁) 第三章 可视化和调试工具 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中使用. ~$ rosl ...
- RE模块错误已解决.
下面这个错误是由于在正则[...]的内部,减号'-'是一个有特殊含义的字符(代表字符范围) 所以如果需要在[...]内匹配减号'-',需要用反斜杠'\'转义. >>> import ...
- JAVA面向对象-----成员内部类访问细节
JAVA面向对象-–成员内部类访问细节 私有的成员内部类不能在其他类中直接创建内部类对象来访问. 如果内部类中包含有静态成员,那么java规定内部类必须声明为静态的访问静态内部类的形式:Outer.I ...
- Maven简介(Maven是什么)
简介 Maven,在意第绪语中意为对知识的积累.Maven最初用来在Jakarta Turbine项目中简化该项目的构建过程.Jakarta Trubine项目有多个工程,每个工程都有自己的多个Ant ...
- 集合框架之List接口
有序的 collection(也称为序列).此接口的用户可以对列表中每个元素的插入位置进行精确地控制.用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素. 与 set 不同,列表 ...
- SSH网上商城---邮件发送
注册网站账号的时候,都需要发送激活邮件,然后让注册的用户点击激活链接方可完成注册,不过话说回来,为什么注册的时候需要发送邮件呢?为什么不注册的时候直接激活呢?一定要收一封激活帐号的邮件?网站这样做的好 ...