当前位置:网站首页>numpy.where
numpy.where
2022-08-01 23:21:00 【Wanderer001】
numpy.
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
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]])
边栏推荐
- 如何使用pywinauto和pyautogui将动漫小姐姐链接请回家
- Oracle 数据库设置为只读及读写
- How to better understand and do a good job?
- bat 之 特殊字符&转义
- 欧拉路径与欧拉回路
- TCP 可靠吗?为什么?
- visual studio code multiple editing
- The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
- Loading configuration of Nacos configuration center
- ELK log collection
猜你喜欢
6134. Find the closest node to the given two nodes - force double hundred code
测试岗月薪5-9k,如何实现涨薪到25k?
域名重定向工具 —— SwitchHosts 实用教程
chrome copies the base64 data of an image
What is CICD excuse me
chrome复制一张图片的base64数据
PDF转Word有那么难吗?做一个文件转换器,都解决了
解决端口占用
From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program
请问什么是 CICD
随机推荐
Is TCP reliable?Why?
6134. Find the closest node to the given two nodes - force double hundred code
中职网络安全竞赛B7比赛部署流程
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解
Oracle database is set to read-only and read-write
E - Integer Sequence Fair
y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)
cmd command
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
欧拉路径与欧拉回路
Chapter 11 Working with Dates and Times
杭电多校3 1012. Two Permutations dp*
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
Chapter 11 Working with Dates and Times
How do programmers solve online problems gracefully?
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights
深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
用virtualenv和Virtualenvwrapper虚拟环境管理工具创建虚拟环境
毫秒级!千万人脸库快速比对,上亿商品图片检索,背后的极速检索用了什么神器?
【SeaTunnel】从一个数据集成组件演化成企业级的服务