簡體   English   中英

如何獲取 numpy.ndarrays 的集合補碼?

[英]How to take set complement of numpy.ndarrays?

我有 2 張 numpy arrays 圖像,

X.shape =(76,224,224,3)
X_1.shape =(15,224,224,3)

X_1 中的 15 個圖像也在 X 中。我怎樣才能得到 Z = X \ X_1? 其中 Z 可能有 shape=(61,224,224,3)。

謝謝大家。

X_1 中的 15 個圖像在 X 中。

這不意味着X∩X_1=X_1因為整個X_1包含在X中,所以你只想要Z = X-X_1

我認為您應該查看numpy.delete


這可能(絕對)不是最快或最好的方法,但我通過查看這個答案讓它工作,

>>> X = np.random.randint(255, size=(76,224,224,3))
>>> X_1 = X[:15]
>>> X.shape
(76, 224, 224, 3)
>>> X_1.shape
(15, 224, 224, 3)
>>> ind_to_del = np.unique((X[:, None] == X_1).all(-1).argmax(0))
>>> ind_to_del
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> Z = np.delete(X, ind_to_del, axis=0)
>>> Z.shape
(61, 224, 224, 3)

所以你要,

ind_to_del = np.unique((X[:, None] == X_1).all(-1).argmax(0))
Z = np.delete(X, ind_to_del, axis=0)

暫無
暫無

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

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