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自定义函数的创建、调用和函数的参数详解
Mar 11 Python
Python写入CSV文件的方法
Jul 08 Python
详解Python中使用base64模块来处理base64编码的方法
Jul 01 Python
Python爬取网易云音乐热门评论
Mar 31 Python
详解如何在python中读写和存储matlab的数据文件(*.mat)
Feb 24 Python
python使用opencv驱动摄像头的方法
Aug 03 Python
Python中应该使用%还是format来格式化字符串
Sep 25 Python
解决python3 安装完Pycurl在import pycurl时报错的问题
Oct 15 Python
只需7行Python代码玩转微信自动聊天
Jan 27 Python
初探利用Python进行图文识别(OCR)
Feb 26 Python
Django Aggregation聚合使用方法解析
Aug 01 Python
Python Scrapy框架:通用爬虫之CrawlSpider用法简单示例
Apr 11 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
发布一个迷你php+AJAX聊天程序[聊天室]提供下载
2007/07/21 PHP
关于页面优化和伪静态
2009/10/11 PHP
PHP实现把数字ID转字母ID
2013/08/12 PHP
php 批量替换html标签的实例代码
2013/11/26 PHP
ThinkPHP单字母函数(快捷方法)使用总结
2014/07/23 PHP
WebQQ最新登陆协议的用法
2014/12/22 PHP
PHP基本语法实例总结
2016/09/09 PHP
JQuery 简便实现页面元素数据验证功能
2007/03/24 Javascript
syntaxhighlighter 使用方法
2007/07/02 Javascript
关于URL中的特殊符号使用介绍
2011/11/03 Javascript
简单常用的幻灯片播放实现代码
2013/09/25 Javascript
jquery分割字符串的方法
2015/06/24 Javascript
js中获取时间new Date()的全面介绍
2016/06/20 Javascript
JS对HTML表格进行增删改操作
2016/08/22 Javascript
微信小程序 绘图之饼图实现
2016/10/24 Javascript
Bootstrap开发中Tab标签页切换图表显示问题的解决方法
2018/07/13 Javascript
js 实现ajax发送步骤过程详解
2019/07/25 Javascript
vue封装可复用组件confirm,并绑定在vue原型上的示例
2019/10/31 Javascript
vue全屏事件开发详解
2020/06/17 Javascript
Python中使用item()方法遍历字典的例子
2014/08/26 Python
python实现简单遗传算法
2018/03/19 Python
Python3实现的判断回文链表算法示例
2019/03/08 Python
Python3+selenium实现cookie免密登录的示例代码
2020/03/18 Python
PyInstaller将Python文件打包为exe后如何反编译(破解源码)以及防止反编译
2020/04/15 Python
keras中模型训练class_weight,sample_weight区别说明
2020/05/23 Python
Python用户自定义异常的实现
2020/12/25 Python
加利福尼亚州威尼斯的女性奢侈品设计师服装和概念店:Mona Moore
2018/09/13 全球购物
12岁生日感言
2014/01/21 职场文书
舞蹈专业求职信
2014/06/13 职场文书
2014年银行柜员工作总结
2014/11/12 职场文书
2015年团支部工作总结
2015/04/03 职场文书
生活小常识广播稿
2015/08/19 职场文书
假如给我三天光明:舟逆水而行,人遇挫而达 
2019/10/29 职场文书
Python echarts实现数据可视化实例详解
2022/03/03 Python
Mysql中@和@@符号的详细使用指南
2022/06/05 MySQL
Python+DeOldify实现老照片上色功能
2022/06/21 Python