如果我想為多個值的 numpy 陣列創建一個過濾器,我將如何撰寫它。
例如說我想這樣做......
clusters = np.array([4, 570, 56, 2, 5, 1, 1, 570, 32, 1])
fiveseventy_idx = np.where((clusters == 1) | (clusters == 570 ))
clusters = clusters[ fiveseventy_idx ]
在上述情況下,我只想要 2 個專案,但說我有一個更大的陣列,我想過濾n個專案,如果我說 300 個我想要的專案,我不知道如何使用這種語法來完成的原始陣列。
uj5u.com熱心網友回復:
如果您要在 中搜索一系列值clusters,則可以使用np.isin:
>>> targets = [1, 570] # Also could be: np.array([1, 570])
>>> np.isin(clusters, targets)
array([False, True, False, False, False, True, True, True, False, True])
>>> clusters[np.isin(clusters, targets)]
array([570, 1, 1, 570, 1])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/433821.html
標籤:Python 麻木的 过滤 numpy-ndarray numpy 切片
下一篇:如何在numpy陣列中搜索網格?
