numpy.where()详细的分步说明/示例 [英] numpy.where() detailed, step-by-step explanation / examples

查看:156
本文介绍了numpy.where()详细的分步说明/示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管阅读这篇文章有人可以提供有关1D和2D阵列的分步注释示例吗?

Can someone provide step-by-step commented examples with 1D and 2D arrays?

推荐答案

摆弄了一会儿之后,我发现了问题,并将它们发布在这里,希望对其他人有帮助.

After fiddling around for a while, I figured things out, and am posting them here hoping it will help others.

直观地,np.where就像问"告诉我该数组中的位置满足给定条件".

>>> a = np.arange(5,10)
>>> np.where(a < 8)       # tell me where in a, entries are < 8
(array([0, 1, 2]),)       # answer: entries indexed by 0, 1, 2

它也可以用于获取满足以下条件的数组中的条目:

It can also be used to get entries in array that satisfy the condition:

>>> a[np.where(a < 8)] 
array([5, 6, 7])          # selects from a entries 0, 1, 2


a是2d数组时,np.where()返回行idx的数组和col idx的数组:


When a is a 2d array, np.where() returns an array of row idx's, and an array of col idx's:

>>> a = np.arange(4,10).reshape(2,3)
array([[4, 5, 6],
       [7, 8, 9]])
>>> np.where(a > 8)
(array(1), array(2))

与一维情况一样,我们可以使用np.where()在2d数组中获取满足以下条件的条目:

As in the 1d case, we can use np.where() to get entries in the 2d array that satisfy the condition:

>>> a[np.where(a > 8)] # selects from a entries 0, 1, 2

array([9])

array([9])

请注意,当a为1d时,np.where()仍返回行idx的数组和col idx的数组,但列的长度为1,因此后者为空数组.

Note, when a is 1d, np.where() still returns an array of row idx's and an array of col idx's, but columns are of length 1, so latter is empty array.

这篇关于numpy.where()详细的分步说明/示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆