掩模numpy阵列并提取符合条件的值
如何正确掩盖两个Numpy阵列?我想找到不等于255
的pe
值。我还希望我所需的输出数组的大小与pd
和pe
,ie,(7,7)
,并用填充0是
。
实现这一目标的最有效方法是什么?
import numpy as np
pd = np.random.randint(254,256, size=(7,7))
pe = np.random.randint(0,7, size=(7,7))
所需的输出
[[6 6 0 0 6 6 1]
[2 6 1 1 5 6 3]
[3 4 6 6 3 5 6]
[3 5 0 3 2 0 0]
[0 3 6 1 3 6 1]
[6 3 4 1 0 3 1]
[6 0 4 2 2 6 4]]
非常感谢
How can I mask two NumPy arrays properly? I want find pe
values that are not equal to 255
, for example. I also want my desired output array be the same size aspd
and pe
, i.e., (7, 7)
and filled with 0's
.
What is the most efficient way to achieve this?
import numpy as np
pd = np.random.randint(254,256, size=(7,7))
pe = np.random.randint(0,7, size=(7,7))
Desired output
[[6 6 0 0 6 6 1]
[2 6 1 1 5 6 3]
[3 4 6 6 3 5 6]
[3 5 0 3 2 0 0]
[0 3 6 1 3 6 1]
[6 3 4 1 0 3 1]
[6 0 4 2 2 6 4]]
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
逻辑索引似乎是所有选项中最简单的。
根据您的数据大小,您可以尝试其他选项:
但是我猜索引仍然很简单快捷。
Logical indexing seems the simplest of all options.
Based on your data size, you may try other options:
but I guess indexing is still simple and fast.