Python np.where()的详解以及代码应用
作者:Kingyanhui 时间:2023-02-15 18:33:01
np.where共两种用法:
第一种np.where(condition, x, y)
,即condition为条件,当满足条件输出为x,不满足条件则输出y.直接上代码:
a = np.arange(10)
//array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(np.where(a > 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]])
可以这么理解,第一行的bool值表示条件,它表示是否取值的意思,首先看[True,False],即第一的True值表示第一行取数值第一行的[1, 2]中的1,而不取下面的9,False表示不取第一行[1, 2]中的2,而取第二行[9, 8]中的8.下面同理得[3, 4].
为了方便理解再举一个例子:
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')
第一行a>5True,则取第一行的第一个值,a<5取第二行的第二个值,下面也同理.
理解完第一种方法后,再来看np.where第二种方法:
即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)]
//array([ 6, 8, 10]) 返回元素值,即a[索引]
举一个代码例子,也是我遇到的:
a = array([[0., 1.],
[0., 1.],
[0., 1.],
[0., 1.],
[0., 1.],
[0., 1.],
[0., 1.],
[0., 1.],
[1., 0.],
[0., 1.],
[0., 1.],
[0., 1.],
[1., 0.],
[1., 0.],
[0., 1.],
[0., 1.],
[1., 0.],
[0., 1.],
[1., 0.],
[0., 1.]])
np.where(a == 1)
//(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
// 17, 18, 19], dtype=int64),
// array([1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1],
// dtype=int64))
返回的两个array数组分表示第几行的第几个值为1,所以结果中的第一个array数组表示行索引,第二个array数组表示列索引也就是1的碎银索引.
附:np.where()多条件用法
1.np.where(condition,x,y) 当where内有三个参数时,第一个参数表示条件,当条件成立时where方法返回x,当条件不成立时where返回y
2.np.where(condition) 当where内只有一个参数时,那个参数表示条件,当条件成立时,where返回的是每个符合condition条件元素的坐标,返回的是以元组的形式
3.多条件时condition,&表示与,|表示或。如a = np.where((0<a)&(a<5), x, y),当0<a与a<5满足时,返回x的值,当0<a与a<5不满足时,返回y的值。注意x, y必须和a保持相同尺寸。
例如:
import numpy as np
data = np.array([[0, 2, 0],
[3, 1, 2],
[0, 4, 0]])
new_data = np.where((data>= 0) & (data<=2), np.ones_like(data), np.zeros_like(data))
print(new_data)
结果:
[[1 1 1]
[0 1 1]
[1 0 1]]
从中可以看出data中每个元素只要满足data>=0并且data<=2, 满足就返回np.ones_like(data)对应坐标的值,不满足就返回np.zeros_like(data)对应坐标的值。当然x , y可以换成其他的值,只要与条件相同尺寸就可以。
来源:https://blog.csdn.net/Kingyanhui/article/details/121385646
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
玩转python selenium鼠标键盘操作(ActionChains)
在python中将list分段并保存为array类型的方法
Python使用Appium在移动端抓取微博数据的实现
![](https://img.aspxhome.com/file/2023/3/77903_0s.jpg)
使用python-cv2实现视频的分解与合成的示例代码
SQL Server讲堂:备份和恢复措施
python 读取二进制 显示图片案例
![](https://img.aspxhome.com/file/2023/6/77636_0s.jpg)
python matplotlib实现条形图的填充效果
![](https://img.aspxhome.com/file/2023/1/79561_0s.jpg)
Oracle 当前用户下所有表的记录总数
使用Python的Flask框架表单插件Flask-WTF实现Web登录验证
![](https://img.aspxhome.com/file/2023/0/77710_0s.jpg)
python+splinter实现12306网站刷票并自动购票流程
在系统崩溃的时候如何恢复原有的数据
asp 根据IP地址自动判断转向分站的代码
在Lighttpd服务器中运行Django应用的方法
基于python编写的微博应用
web标准:CSS clear的属性及使用方法
从Vista地址栏到网站导航菜单
![](https://img.aspxhome.com/file/UploadPic/20099/8/nav-62s.jpg)
Python快速查找list中相同部分的方法
![](https://img.aspxhome.com/file/2023/0/91890_0s.jpg)
关于pycharm中pip版本10.0无法使用的解决办法
![](https://img.aspxhome.com/file/2023/9/85019_0s.jpg)