np.where()函数的详细使用介绍

2023-11-17

看了很多博客,见到的没有一个给说清楚了,这里做个记录。这是python提供的函数说明:
在这里插入图片描述

Help on function where in module numpy:

where(...)
    where(condition, [x, y])
    
    Return elements chosen from `x` or `y` depending on `condition`.
    
    .. note::
        When only `condition` is provided, this function is a shorthand for
        ``np.asarray(condition).nonzero()``. Using `nonzero` directly should be
        preferred, as it behaves correctly for subclasses. The rest of this
        documentation covers only the case where all three arguments are
        provided.
    
    Parameters
    ----------
    condition : array_like, bool
        Where True, yield `x`, otherwise yield `y`.
    x, y : array_like
        Values from which to choose. `x`, `y` and `condition` need to be
        broadcastable to some shape.
    
    Returns
    -------
    out : ndarray
        An array with elements from `x` where `condition` is True, and elements
        from `y` elsewhere.
    
    See Also
    --------
    choose
    nonzero : The function that is called when x and y are omitted
    
    Notes
    -----
    If all the arrays are 1-D, `where` is equivalent to::
    
        [xv if c else yv
         for c, xv, yv in zip(condition, x, y)]
    
    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]])

就是说,当只传入condition参数时,等价于np.nonzero()

Help on function nonzero in module numpy:

nonzero(a)
    Return the indices of the elements that are non-zero.
    
    Returns a tuple of arrays, one for each dimension of `a`,
    containing the indices of the non-zero elements in that
    dimension. The values in `a` are always tested and returned in
    row-major, C-style order.
    
    To group the indices by element, rather than dimension, use `argwhere`,
    which returns a row for each non-zero element.
    
    .. note::
    
       When called on a zero-d array or scalar, ``nonzero(a)`` is treated
       as ``nonzero(atleast1d(a))``.
    
       .. deprecated:: 1.17.0
    
          Use `atleast1d` explicitly if this behavior is deliberate.
    
    Parameters
    ----------
    a : array_like
        Input array.
    
    Returns
    -------
    tuple_of_arrays : tuple
        Indices of elements that are non-zero.
    
    See Also
    --------
    flatnonzero :
        Return indices that are non-zero in the flattened version of the input
        array.
    ndarray.nonzero :
        Equivalent ndarray method.
    count_nonzero :
        Counts the number of non-zero elements in the input array.
    
    Notes
    -----
    While the nonzero values can be obtained with ``a[nonzero(a)]``, it is
    recommended to use ``x[x.astype(bool)]`` or ``x[x != 0]`` instead, which
    will correctly handle 0-d arrays.
    
    Examples
    --------
    >>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
    >>> x
    array([[3, 0, 0],
           [0, 4, 0],
           [5, 6, 0]])
    >>> np.nonzero(x)
    (array([0, 1, 2, 2]), array([0, 1, 0, 1]))
    
    >>> x[np.nonzero(x)]
    array([3, 4, 5, 6])
    >>> np.transpose(np.nonzero(x))
    array([[0, 0],
           [1, 1],
           [2, 0],
           [2, 1]])
    
    A common use for ``nonzero`` is to find the indices of an array, where
    a condition is True.  Given an array `a`, the condition `a` > 3 is a
    boolean array and since False is interpreted as 0, np.nonzero(a > 3)
    yields the indices of the `a` where the condition is true.
    
    >>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    >>> a > 3
    array([[False, False, False],
           [ True,  True,  True],
           [ True,  True,  True]])
    >>> np.nonzero(a > 3)
    (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
    
    Using this result to index `a` is equivalent to using the mask directly:
    
    >>> a[np.nonzero(a > 3)]
    array([4, 5, 6, 7, 8, 9])
    >>> a[a > 3]  # prefer this spelling
    array([4, 5, 6, 7, 8, 9])
    
    ``nonzero`` can also be called as a method of the array.
    
    >>> (a > 3).nonzero()
    (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))

在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

np.where()函数的详细使用介绍 的相关文章

随机推荐

  • (10)stata的基本使用--短面板数据处理

    面板数据处理 数据描述 数据预览 告诉计算机这是面板数据 描述变量 查看其他变量 绘图 混合回归 聚类稳健标准误 cluster后的变量表示聚类标准 表示使用以state变量聚类的聚类稳健标准误 普通稳健标准误 对比普通稳健标准误与聚类稳健
  • 树05--二叉搜索树的后序遍历序列

    树05 二叉搜索树的后序遍历序列 jz23 题目概述 解析 参考答案 注意事项 说明 题目概述 算法说明 输入一个整数数组 判断该数组是不是某二叉搜索树的后序遍历的结果 如果是则返回true 否则返回false 假设输入的数组的任意两个数字
  • 数字钟实训经历

    第一次写博客 多多关照 先说一点自己的感悟吧 我是电气工程及其自动化专业的大三学生 大一时加入了学校的电子技术协会 转眼一晃两年就这样过去了 这不暑假了还在学校准备今年的全国电子设计竞赛 在这自学单片机的两年时间里 遇到了许多疑难困惑 通过
  • linux下MMC/SD/SDIO驱动系列之四 ---- SDIO的识别与操作

    从上篇文章的最后 我们知道host在扫描卡的过程中 其识别的顺序为SDIO SD MMC 并且从它的注释可以看出 这个顺序是很重要的 那这篇文章 我们就看看SDIO的识别过程 它对应的函数就是mmc attach sdio host 函数位
  • C++笔记一(C语言基础)

    1 变量命名规则 1 1 标识符可由三类字符 字母 下划线 数字组成 标识符只能由字母或下划线开头 标识符不能具有二义性 标识符有长度要求 在起定的名字中 超出长度规定的部分将被截掉 2 部分基础数据类型 2 1 常用数据类型长度 bool
  • EXE文件打不开的解决方法

    EXE文件打不开 打开 我的电脑 或随便一个文件夹 点击菜单 工具 选择 文件夹选项 选择 文件类型 中的 新建 新建扩展名 EXE 单击 高级 关联的文件类型 中选择 应用程序 在命令提示符 cmd 在 开始 菜单 所有程序 的 附件 中
  • C和C++打印指针值和地址

    1 C 中指针变量的地址和指针变量的值是两个不同的概念 指针变量的地址 这是指针变量这个变量在内存中的存储地址 如图所示0x1211 指针的值 里面存放的是一个地址 此地址即为指向的内存单元的地址 如图所示0x1101 2 假如要输出指针变
  • IntelliJ IDEA安装教程,三分钟手把手教会,非常简单!

    使用IntelliJ IDEA写java程序需要配置jdk 链接 JDK安装教程 一 IntelliJ IDEA下载 1 进入官网 官网地址 https www jetbrains com 2 点击 Developer Tools 开发者工
  • jQuery遍历之next()、nextAll()方法使用实例

    jquery遍历 next 和nextAll 方法 实例如下 复制代码 代码如下
  • element踩坑之el-select中的placeholder属性不显示

    直接上图 咱想要这种效果 但现实却给了这种效果 明明ui代码一模一样
  • 编译freeRTOS “error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token”错误解决

    今日编译ESP8266 RTOS SDK的时候有个头文件声明了extern 结构体 结果一旦加入这个头文件编译就各种报错 提示error expected asm or attribute before token 一通搜索之后并未解决我的
  • IDEA的好用小工具Test RESTful web Service

    Test RESTful web Service 一 2021版IDEA界面 二 2019版 我安了个插件叫Old REST Client来还原这个样子 三 代码demo示例 补充 好处 可以减少postman的使用 简单的可以用这个 脚本
  • 【C语言】详解getchar函数该如何使用

    目录 getchar函数 getchar函数的声明 getchar函数返回值问题 getchar函数的无法返回字符串的情况 输出通过getchar函数获得的一个字符 getchar函数的进一步使用 最后这里给大家推荐一个库函数的网站 Ref
  • 浪潮nf5180m5服务器安装系统,NF5180M5-IPMI设置

    登录 默认用户名需注意 用户名 admin 密码 admin 主页面 Web 界面分为四个部分 如下图所示 界面左上角 表示 Web 界面的名称 界面右上角各按钮含义 点击系统摘要按钮 返回系统摘要页面 2 点击刷新按钮 进行页面刷新 3
  • 【codeforces】 ZeptoLab Code Rush 2015 A,B,C,D,E题解

    D E统统FST 差一点就飞升了 A King of Thieves 给你一张地图 让你从某个 开始跳等步长的四次 如果均在 则输出yes 否则输出no 枚举起始点和步长直接做就可以了
  • python兼职:10个python接私活的平台!兼职也能月薪过万

    接私活一定要注意的个点 1 没有第三方担保的个人单子尽量少接 2 无需求文档 没具体需求的不接 3没有预付不做 尽量用442的分步骤方式 然后就是正题的网站推荐了 1 码市 https codemart com 2 开源众包 https z
  • python使用pygraphdb连接graphdb图数据库

    文章目录 前言 一 GraphDB是什么 二 使用pygraphdb连接graphdb 1 安装pygraphdb 2 功能介绍 数据管理 数据库管理 3 快速上手 4 使用with连接 5 数据管理 6 数据库管理 前言 本文主要介绍如何
  • 计算机组成原理定点源码一位乘,计算机组成原理课程设计-定点原码一位乘法器的设计.doc...

    计算机组成原理课程设计 定点原码一位乘法器的设计 课 程 设 计 报 告 课程设计名称 计算机组成原理课程设计 课程设计题目 定点原码一位乘法器的设计 院 系 计算机学院 专 业 班 级 4401102 学 号 208 姓 名 指导教师 完
  • mysql创建带密码的用户使用密码无法登录,但是不输入密码可以登录

    在cmd窗口 直接使用 mysql usa 可以登录 但是创建sa时候设置密码为123456 原因可能有两个 设置密码后没有强制刷新权限或者重启mysql服务 Mysql数据库的user表中有名字为空的用户名或root的密码为空 解决方法
  • np.where()函数的详细使用介绍

    看了很多博客 见到的没有一个给说清楚了 这里做个记录 这是python提供的函数说明 Help on function where in module numpy where where condition x y Return eleme