检查Numpy中2D数组是否包含特定的1D数组

问题描述:

有没有一种方法可以检查Numpy中的矩阵是否包含特定向量?

Is there a way I can check to see whether a matrix in Numpy contains a specific vector?

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

v = np.array([1, 1, 1])

我希望能够测试: X 中的 bool = v .我知道这不适用于Numpy,并且想知道是否有ay可以在没有令人讨厌的循环的情况下对此进行测试吗?感谢您的帮助!

I want to be able to test: bool = v in X. I know this doesn't work with Numpy and wonder if there is a ay to test this without obnoxious loops? Thanks for any help!

您可以使用 all 汇总行,然后查看是否有所有行都匹配的行.

You can aggregate the rows with all then see if there's any row where all the columns have matched.

np.any(np.all(np.isin(X,v,True),axis=1))

我应该提到,这是假设您的行是独特且唯一的.

I should mention, this is assuming that your rows are distinct and unique.