当前位置:网站首页>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]])边栏推荐
- 论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
- Calculate the midpoint between two points
- Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu
- 解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”
- SQL Server (design database--stored procedure--trigger)
- D - Linear Probing- 并查集
- 解决端口占用
- 2022/7/31
- 加载字体时避免隐藏文本
- Loading configuration of Nacos configuration center
猜你喜欢

程序员如何优雅地解决线上问题?

Making a Simple 3D Renderer

数据分析04

分享10套开源免费的高品质源码,免费源码下载平台

论文理解【RL - Exp Replay】—— Experience Replay with Likelihood-free Importance Weights

chrome复制一张图片的base64数据

chrome copies the base64 data of an image

cmd command

华为无线设备配置全局双链路冷备份(AC全局配置方式)
Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu
随机推荐
简单3D渲染器的制作
添加大量元素时使用 DocumentFragments
perspectiveTransform warpPerspective getPerspectiveTransform findHomography
【数据分析03】
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution
Calculate the distance between two points
[C language advanced] file operation (2)
Check if point is inside rectangle
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
System availability: 3 9s, 4 9s in SRE's mouth... What is it?
从0到100:招生报名小程序开发笔记
下载安装 vscode(含汉化、插件的推荐和安装)
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
避免使用 <b>、<i>、<s> 和 <u> 标签
Three, mysql storage engine - building database and table operation
TCP 可靠吗?为什么?
excel edit a cell without double clicking
Building a cloud-native DevOps environment
D - Linear Probing- 并查集