Python中的np.where()是什么意思(附代码解读)

总结不易,希望各位看官大大能点个赞👍

首先我们给出Numpy.where()的语法结构:

import numpy as np
np.where (condition[, x, y])

np.where() 有两种用法:

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

满足条件(condition),输出x,不满足输出y。
>>> A = np.arange(10)
>>> A
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.where(A,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]])

当条件(condition)为多维数组时,根据条件中为True则选择x中相应的数值,为False选择y中相应的数值。

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

类似的问题可以再看个例子:

>>> y=5
>>> np.where([[y > 6, y < 3], [y == 2, y == 3]],[["正确", "正确"], ["正确", "不正确"]],[["不正确", "正确"], ["不正确", "正确"]])
array([['不正确', '正确'],
       ['不正确', '正确']], dtype='<U3')

我们的判断条件为[[False,True],[False,False]]

2. np.where(condition)

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

>>> a=np.array([1,2,3,4,5,6,7,8])
>>> print(np.where(a>3))

(array([3, 4, 5, 6, 7], dtype=int64),)

此时我们返回的是一组索引值,我们可以这样转换:

>>> idx=np.where(a>3)
>>> print(a[idx])
[4 5 6 7 8]

其实就是等价于a[a>3]

当参数中只给出条件时,则返回非零元素的索引:

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

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

下面看个复杂点的例子:

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

       [[ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15]]])
>>> np.where(a > 5)

(array([0, 0, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int64), 
array([3, 3, 0, 0, 1, 1, 2, 2, 3, 3], dtype=int64), 
array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1], dtype=int64))

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

根据找到的元素的索引找到元素的具体位置:

>>> b=np.where(a > 5)
>>> a[b]
array([ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

np.where的返回结果

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

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

例如通过where()函数将数组中负值设为0,正值为1:

>>> a=np.array([[1,-1,2],[-1,-2,-3],[2,1,1]])
>>> np.where(a>0,1,0)
array([[1, 0, 1],
       [0, 0, 0],
       [1, 1, 1]])

np.where多条件查询

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

或:np.where((con1)|(con2))

(重点:多条件查询时条件一定要用括号!一定要用括号!一定要用括号!)

我们举个例子来示范一下:

>>> a=np.array([[1,-1,2],[-1,-2,-3],[2,1,1]])
>>> b=np.array([[3,-1,-2],[1,-2,-3],[-2,1,-1]])
>>> np.where(a<0 & b<0)

Traceback (most recent call last):
  File "F:\anaconda\envs\sklearn-env\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我们不加括号时,就会报错,无法使用!

>>> np.where((a<0) & (b<0))

(array([0, 1, 1], dtype=int64), 
array([1, 1, 2], dtype=int64))

带上括号后就能输出正确结果!

>>> c=np.where((a<0) & (b<0))
>>> a[c]
array([-1, -2, -3])
>>> b[c]
array([-1, -2, -3])

我们看一下运算

>>> np.where((a<0) | (b<0))
(array([0, 0, 1, 1, 1, 2, 2], dtype=int64), array([1, 2, 0, 1, 2, 0, 2], dtype=int64))
>>> d=np.where((a<0) | (b<0))
>>> a[d]
array([-1,  2, -1, -2, -3,  2,  1])
>>> b[d]
array([-1, -2,  1, -2, -3, -2, -1])

以上就是本文的全部内容,希望对大家的学习有所帮助!

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旅途中的宽~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值