简体   繁体   English

如何使用另一个ndarray屏蔽一个ndarray

[英]How to mask one ndarray using a another ndarray

I have an items array which has shape (n, 3) , and a counts array which has the same shape (n, 3) . 我有一个items阵列,其具有的形状(n, 3)和一个counts阵列,其具有相同的形状(n, 3) How can I make every C in items to have count = 0 without resorting to loops? 如何在不使用循环的情况下使items每个C都具有count = 0

items = np.array([['B', 'A', 'C'],
    ['B', 'B', 'C'],
    ['A', 'B', 'A'],
    ['C', 'C', 'C'],
    ['B', 'B', 'B']])

counts = np.array([[1, 3, 2],
    [4, 2, 3],
    [2, 2, 1],
    [3, 2, 1],
    [1, 2, 1]])

Expected output: 预期产量:

>>> counts
np.array([[1, 3, 0],
    [4, 2, 0],
    [2, 2, 1],
    [0, 0, 0],
    [1, 2, 1]])

What you're looking for is: 你在寻找的是:

counts[items == 'C'] = 0

A more explicit way to express it is: 更明确的表达方式是:

c_indices = np.where(items == 'C')
counts[c_indices] = 0

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

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