numpy.where() 用法详解


Posted in Python onMay 27, 2019

numpy.where (condition[, x, y])

numpy.where() 有两种用法:

1. np.where(condition, x, y)

满足条件(condition),输出x,不满足输出y。

如果是一维数组,相当于[xv if c else yv for (c,xv,yv) in zip(condition,x,y)]

>>> aa = np.arange(10)
>>> np.where(aa,1,-1)
array([-1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) # 0为False,所以第一个输出-1
>>> np.where(aa > 5,1,-1)
array([-1, -1, -1, -1, -1, -1, 1, 1, 1, 1])

>>> np.where([[True,False], [True,True]],  # 官网上的例子
       [[1,2], [3,4]],
       [[9,8], [7,6]])
array([[1, 8],
    [3, 4]])

上面这个例子的条件为[[True,False], [True,False]],分别对应最后输出结果的四个值。第一个值从[1,9]中选,因为条件为True,所以是选1。第二个值从[2,8]中选,因为条件为False,所以选8,后面以此类推。类似的问题可以再看个例子:

>>> a = 10
>>> np.where([[a > 5,a < 5], [a == 10,a == 7]],
       [["chosen","not chosen"], ["chosen","not chosen"]],
       [["not chosen","chosen"], ["not chosen","chosen"]])

array([['chosen', 'chosen'],
    ['chosen', 'chosen']], dtype='<U10')

2. np.where(condition)

只有条件 (condition),没有x和y,则输出满足条件 (即非0) 元素的坐标 (等价于numpy.nonzero)。这里的坐标以tuple的形式给出,通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标。

>>> a = np.array([2,4,6,8,10])
>>> np.where(a > 5)       # 返回索引
(array([2, 3, 4]),)  
>>> a[np.where(a > 5)]       # 等价于 a[a>5]
array([ 6, 8, 10])

>>> np.where([[0, 1], [1, 0]])
(array([0, 1]), array([1, 0]))

上面这个例子条件中[[0,1],[1,0]]的真值为两个1,各自的第一维坐标为[0,1],第二维坐标为[1,0] 。

下面看个复杂点的例子:

>>> a = np.arange(27).reshape(3,3,3)
>>> a
array([[[ 0, 1, 2],
    [ 3, 4, 5],
    [ 6, 7, 8]],

    [[ 9, 10, 11],
    [12, 13, 14],
    [15, 16, 17]],

    [[18, 19, 20],
    [21, 22, 23],
    [24, 25, 26]]])

>>> np.where(a > 5)
(array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
 array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2]),
 array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]))


# 符合条件的元素为
    [ 6, 7, 8]],

   [[ 9, 10, 11],
    [12, 13, 14],
    [15, 16, 17]],

   [[18, 19, 20],
    [21, 22, 23],
    [24, 25, 26]]]

所以np.where会输出每个元素的对应的坐标,因为原数组有三维,所以tuple中有三个数组。

1、numpy.where的返回结果

numpy.where调用方式为numpy.where(condition,1,2)

满足条件的位置上返回结果1,不满足的位置上返回结果2

 例如通过where()函数将a数组中负值设为0,正值不变

numpy.where() 用法详解

如果没有指定返回结果,只有查找条件则返回满足条件的位置。返回的结果是一个元组(tuple),包含两个数组,第一个数组纪录的是行,第二个数组纪录的是列。

numpy.where() 用法详解

可以使用zip函数将返回的位置组成一个个坐标对,方便调用。zip函数直接返回的是一个对象,可以用过for循环遍历出里面的元素,也可以使用list直接列出所有坐标对元素。

numpy.where() 用法详解

 2、numpy.where多条件查询

与: numpy.where((con1)*(con2))或者用&

或:numpy.where((con1)|(con2))  (重点:多条件查询时条件一定要用括号!一定要用括号!一定要用括号!)

numpy.where() 用法详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python命名空间详解
Aug 18 Python
Python多线程实现同步的四种方式
May 02 Python
python中利用await关键字如何等待Future对象完成详解
Sep 07 Python
Numpy中stack(),hstack(),vstack()函数用法介绍及实例
Jan 09 Python
Python pymongo模块用法示例
Mar 31 Python
使用selenium和pyquery爬取京东商品列表过程解析
Aug 15 Python
pytorch 修改预训练model实例
Jan 18 Python
tensorflow实现训练变量checkpoint的保存与读取
Feb 10 Python
pyqt5 QlistView列表显示的实现示例
Mar 24 Python
Python可以实现栈的结构吗
May 27 Python
VSCode中autopep8无法运行问题解决方案(提示Error: Command failed,usage)
Mar 02 Python
如何理解及使用Python闭包
Jun 01 Python
python numpy实现文件存取的示例代码
May 26 #Python
numpy linalg模块的具体使用方法
May 26 #Python
python flask解析json数据不完整的解决方法
May 26 #Python
如何使用pyinstaller打包32位的exe程序
May 26 #Python
让你Python到很爽的加速递归函数的装饰器
May 26 #Python
Django框架模板的使用方法示例
May 25 #Python
Django框架搭建的简易图书信息网站案例
May 25 #Python
You might like
杏林同学录(九)
2006/10/09 PHP
php+javascript的日历控件
2009/11/19 PHP
那些年一起学习的PHP(二)
2012/03/21 PHP
php实现的百度搜索某地天气的小偷代码
2014/04/23 PHP
php检测文本的编码
2015/07/26 PHP
功能强大的PHP图片处理类(水印、透明度、旋转)
2015/10/21 PHP
LAMP环境使用Composer安装Laravel的方法
2017/03/25 PHP
Laravel 自带的Auth验证登录方法
2019/09/30 PHP
JavaScript面向对象知识串结(读JavaScript高级程序设计(第三版))
2012/07/17 Javascript
js图片处理示例代码
2014/05/12 Javascript
Node.js异步I/O学习笔记
2014/11/04 Javascript
Jquery中find与each方法用法实例
2015/02/04 Javascript
用JS动态改变表单form里的action值属性的两种方法
2016/05/25 Javascript
JavaScript 对象详细整理总结
2016/09/29 Javascript
纯原生js实现table表格的增删
2017/01/05 Javascript
python爬取安居客二手房网站数据(实例讲解)
2017/10/19 Javascript
js时间戳与日期格式之间相互转换
2017/12/11 Javascript
Node.Js生成比特币地址代码解析
2018/04/21 Javascript
用react-redux实现react组件之间数据共享的方法
2018/06/08 Javascript
node.js中npm包管理工具用法分析
2020/02/14 Javascript
vue实现购物车结算功能
2020/06/18 Javascript
centos系统升级python 2.7.3
2014/07/03 Python
Python函数式编程指南(二):从函数开始
2015/06/24 Python
Python 中 Meta Classes详解
2016/02/13 Python
numpy 返回函数的上三角矩阵实例
2019/11/25 Python
python pip安装包出现:Failed building wheel for xxx错误的解决
2019/12/25 Python
Python3自动生成MySQL数据字典的markdown文本的实现
2020/05/07 Python
python中entry用法讲解
2020/12/04 Python
three.js模拟实现太阳系行星体系功能
2019/09/03 HTML / CSS
2014年高考决心书
2014/03/11 职场文书
小学运动会演讲稿
2014/08/25 职场文书
2014年安全管理工作总结
2014/12/01 职场文书
Pytorch 实现变量类型转换
2021/05/17 Python
Mysql数据库中datetime、bigint、timestamp来表示时间选择,谁来存储时间效率最高
2021/08/23 MySQL
MySQL中的引号和反引号的区别与用法详解
2021/10/24 MySQL
php解析非标准json、非规范json的方式实例
2022/05/10 PHP