簡體   English   中英

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

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

我有一個items陣列,其具有的形狀(n, 3)和一個counts陣列,其具有相同的形狀(n, 3) 如何在不使用循環的情況下使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]])

預期產量:

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

你在尋找的是:

counts[items == 'C'] = 0

更明確的表達方式是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM