相关疑难解决方法(0)

python numpy.where()如何工作?

我正在玩numpy和挖掘文档,我遇到了一些魔法.即我在说numpy.where():

>>> x = np.arange(9.).reshape(3, 3)
>>> np.where( x > 5 )
(array([2, 2, 2]), array([0, 1, 2]))
Run Code Online (Sandbox Code Playgroud)

他们如何在内部实现您能够将类似的东西传递x > 5给方法?我想这与某些事情有关,__gt__但我正在寻找详细的解释.

python numpy magic-methods

90
推荐指数
2
解决办法
18万
查看次数

Numpy在2D矩阵上的where()

我有一个像这样的矩阵

t = np.array([[1,2,3,'foo'],
 [2,3,4,'bar'],
 [5,6,7,'hello'],
 [8,9,1,'bar']])
Run Code Online (Sandbox Code Playgroud)

我想得到行包含字符串'bar'的索引

在1d数组中

rows = np.where(t == 'bar')
Run Code Online (Sandbox Code Playgroud)

应该给我指数[0,3]然后广播: -

results = t[rows]
Run Code Online (Sandbox Code Playgroud)

应该给我正确的行

但我无法弄清楚如何让它与2d数组一起工作.

python arrays numpy matrix

17
推荐指数
2
解决办法
3万
查看次数

我在哪里可以找到 numpy.where() 源代码?

我已经找到了numpy.ma.where()函数的源代码,但它似乎在调用numpy.where()函数,为了更好地理解它,如果可能的话,我想看看。

python numpy

6
推荐指数
2
解决办法
2653
查看次数

标签 统计

numpy ×3

python ×3

arrays ×1

magic-methods ×1

matrix ×1