简体   繁体   English

numpy.where的Output

[英]Output of numpy.where

I noticed a strange thing concerning the output of numpy.where.我注意到关于 numpy.where 的 output 的一件奇怪的事情。 According to the documentation the output should be of type ndarray.根据文档,output 应该是 ndarray 类型。 If I provide all arguments (including also the optional ones) this is true, eg如果我提供所有 arguments (包括可选的),这是真的,例如

np.where(foo == 2, foo, -1)
>>> array([-1,  2, -1, -1])

However, if I insert an argument like但是,如果我插入一个像

np.where(foo == 2)
>>> (array([1], dtype=int64),)

I get an array encapsulated in a tuple.我得到一个封装在元组中的数组。 Does anyone know why that would make any sense?有谁知道为什么这会有意义?

The answer is in the documentation you provided :答案在您提供的文档中:

When only condition is provided, this function is a shorthand for np.asarray(condition).nonzero() .当仅提供条件时,此 function 是np.asarray(condition).nonzero()的简写。

Looking at np.nonzero() 's documentation , we can see that it returns a tuple of arrays.查看np.nonzero()文档,我们可以看到它返回 arrays 的元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM