Numpy.where 使用 [英] Numpy.where uses

查看:65
本文介绍了Numpy.where 使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 numpy.where 获取 numpy.array 中的所有 (R, G,B) 并具有确定的 R、G 和 B 值

问题是我不确定我可以使用 numpy.where 来获得我想要的:

我尝试了以下代码:

L = numpy.array([[1,2,3],[1,1,1],[1,1,1]])打印(numpy.where(L==(1,1,1)))

<块引用><块引用>

(数组([0, 1, 1, 1, 2, 2, 2], dtype=int64), 数组([0, 0, 1, 2, 0, 1, 2], dtype=int64))

我知道它返回给我每个元素的坐标 == 1但我希望它返回 L 中等于 (1,1,1) 的元素的索引:

<块引用><块引用>

数组([1,2])

解决方案

您正在寻找 numpy.nonzeronp.all(确保每个 RGB 匹配):

<预><代码>>>>numpy.nonzero(numpy.all(L == (1, 1, 1),axis=1))[0]数组([1, 2])

Use numpy.where to get all (R, G,B) in a numpy.array with a definite value of R, G and B

The problem is i'm not sure i can use numpy.where to get what i want :

i tried the following code :

L = numpy.array([[1,2,3],[1,1,1],[1,1,1]])
print(numpy.where(L==(1,1,1)))

(array([0, 1, 1, 1, 2, 2, 2], dtype=int64), array([0, 0, 1, 2, 0, 1, 2], dtype=int64))

and i understand it's returning me the coordinates of every element == 1 but i would like it to return the index in L of the element equal to (1,1,1) :

array([1,2])

解决方案

You are looking for numpy.nonzero together with np.all (to ensure that each of RGB matches):

>>> numpy.nonzero(numpy.all(L == (1, 1, 1), axis=1))[0]
array([1, 2])

这篇关于Numpy.where 使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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