新西兰服务器

numpy.where怎么用


numpy.where怎么用

发布时间:2021-12-15 15:17:10 来源:高防服务器网 阅读:72 作者:小新 栏目:大数据

这篇文章给大家分享的是有关numpy.where怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

函数功能简单说明:

首先condition、x和y三个多维数组的维度必须可以广播.然后返回一个多维数组,这个多维数组的值根据condition来确定,如果condition的值是True,那么从x中取值,否则从y中取值.

实验代码展示:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32  Type "help", "copyright", "credits" or "license()" for more information.>>> import numpy as np>>> a = np.arange(10)>>> a  array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> np.where(a < 5, a, 20200910*a)array([        0,         1,         2,         3,         4, 101004550,   121205460, 141406370, 161607280, 181808190])>>> np.where(a < 5, a, 10*a)array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])>>> a  array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> >>> >>> np.where(  [[True, False],[True, True]],   [[1, 2],[3, 4]],   [[9, 8], [7, 6]])array([[1, 8],   [3, 4]])>>> >>> >>> x, y = np.ogrid[:3, :4]>>> x  array([[0],   [1],   [2]])>>> y  array([[0, 1, 2, 3]])>>> np.where(x < y, x, 10 + y)  # both x and 10+y are broadcastarray([[10,  0,  0,  0],   [10, 11,  1,  1],   [10, 11, 12,  2]])>>> >>> >>> >>> a = np.array([[0, 1, 2],  [0, 2, 4],  [0, 3, 6]])>>> a  array([[0, 1, 2],   [0, 2, 4],   [0, 3, 6]])>>> np.where(a < 4, a, -1)  # -1 is broadcastarray([[ 0,  1,  2],   [ 0,  2, -1],   [ 0,  3, -1]])>>> >>> >>>

感谢各位的阅读!关于“numpy.where怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[