numpy.where()函数的详细说明

本文深入探讨了Python中numpy库的where()函数,通过官方文档的示例和一种特殊情况的解释,阐述了该函数在处理条件判断时的列向量化行为,帮助理解其返回结果的逻辑。

python里的numpy.where()函数还是比较奇特的。看了官方文档的例子感觉有一种用法还是没说明。后面主要说这个。

先把官方文档中的例子贴出来,文字说明就不贴了,相信直接看例子更浅显易懂

Examples
--------
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.where(a < 5, a, 10*a)
array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])

This can be used on multidimensional arrays too:

>>> np.where([[True, False], [True, True]],
...          [[1, 2], [3, 4]],
...          [[9, 8], [7, 6]])
array([[1, 8],
       [3, 4]])

The shapes of x, y, and the condition are broadcast together:

>>> x, y = np.ogrid[:3, :4]
>>> np.where(x < y, x, 10 + y)  # both x and 10+y are broadcast
array([[10,  0,  0,  0],
       [10, 11,  1,  1],
       [10, 11, 12,  2]])

>>> a = np.array([[0, 1, 2],
...               [0, 2, 4],
...               [0, 3, 6]])
>>> np.where(a < 4, a, -1)  # -1 is broadcast
array([[ 0,  1,  2],
       [ 0,  2, -1],
       [ 0,  3, -1]])

以上就是官方文档中给出的例子


下面我说下另外一种特殊情况
例子如下:

import numpy as np
>>> 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],
       dtype=int64),
 array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2],
       dtype=int64),
 array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       dtype=int64))

乍一看,看不懂这个输出说明了啥,用np.where的话经常会遇到这个问题。
其实这个输出表达是直接按列向来看的,比如第一列0,2,0 是大于5的(就是where中的条件),第二列0,2,1是大于5的…。可以把列的这个数据看成是对value的索引(index)

`numpy.where` 是 NumPy 中一个非常强大的函数,用于根据条件从数组中选择元素或执行操作。它的基本用法有三种形式: ### 1. 基本形式:`numpy.where(condition)` 返回满足条件 `condition` 的索引。 ### 2. 选择形式:`numpy.where(condition, x, y)` 返回一个数组,其中根据条件 `condition` 选择 `x` 或 `y` 的值: - 如果条件为 `True`,选择 `x`。 - 如果条件为 `False`,选择 `y`。 ### 3. 多维数组支持 `numpy.where` 可以处理多维数组,并返回满足条件的坐标。 --- ### 示例代码 #### 示例 1: 返回满足条件的索引 ```python import numpy as np # 创建一个数组 arr = np.array([1, 2, 3, 4, 5]) # 找出大于 2 的元素的索引 indices = np.where(arr > 2) print("Indices where condition is True:", indices) # 输出: (array([3, 4]),) ``` #### 示例 2: 根据条件选择元素 ```python import numpy as np # 创建一个数组 arr = np.array([1, 2, 3, 4, 5]) # 条件:如果元素大于 3,则返回该元素;否则返回 -1 result = np.where(arr > 3, arr, -1) print("Result array:", result) # 输出: [-1 -1 -1 -1 5] ``` #### 示例 3: 在二维数组中使用 ```python import numpy as np # 创建一个二维数组 arr = np.array([[1, 2], [3, 4]]) # 找出大于 2 的元素的坐标 coordinates = np.where(arr > 2) print("Coordinates where condition is True:", coordinates) # 输出: (array([1, 1]), array([0, 1])) # 提取满足条件的元素 elements = arr[coordinates] print("Elements where condition is True:", elements) # 输出: [3 4] ``` --- ### 解释 1. **`numpy.where(condition)`**: - 这种形式只接受一个参数 `condition`,返回的是一个元组,包含满足条件的索引(对于多维数组,返回的是每个维度上的索引)。 - 在示例 1 中,`arr > 2` 生成了一个布尔数组 `[False, False, True, True, True]`,`numpy.where` 返回了所有 `True` 对应的索引。 2. **`numpy.where(condition, x, y)`**: - 这种形式接受三个参数:`condition`、`x` 和 `y`。 - 它会根据 `condition` 的布尔值来选择 `x` 或 `y` 的值。 - 在示例 2 中,`arr > 3` 生成了一个布尔数组 `[False, False, False, True, True]`,因此结果数组在对应位置选择了 `-1` 或 `arr` 的值。 3. **多维数组支持**: - 在示例 3 中,`np.where(arr > 2)` 返回的是两个数组,分别表示行索引和列索引。 - 使用这些索引来提取满足条件的元素。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值