当前位置:网站首页>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]])
边栏推荐
- npm npm
- 添加大量元素时使用 DocumentFragments
- 6132. All the elements in the array is equal to zero - quick sort method
- Building a cloud-native DevOps environment
- 美赞臣EDI 940仓库装运订单详解
- perspectiveTransform warpPerspective getPerspectiveTransform findHomography
- 牛客多校4 A.Task Computing 思维
- 从0到1:图文投票小程序设计与研发笔记
- JAX-based activation function, softmax function and cross entropy function
- 深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
猜你喜欢
随机推荐
excel edit a cell without double clicking
SQL Server (design database--stored procedure--trigger)
chrome copies the base64 data of an image
Additional Features for Scripting
JAX-based activation function, softmax function and cross entropy function
ELK日志采集
还在纠结报表工具的选型么?来看看这个
excel remove all carriage return from a cell
萍不回答
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
杭电多校3 1012. Two Permutations dp*
隔离和降级
添加大量元素时使用 DocumentFragments
Additional Features for Scripting
Quarantine and downgrade
问题解决方式了
DRF generating serialization class code
浅析多服务在分布式系统下多事务通信处理机制方案
Check if point is inside rectangle
y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)