检查Numpy中2D数组是否包含特定的1D数组 [英] Check whether 2D array contains a specific 1D array in Numpy

查看:63
本文介绍了检查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.

这篇关于检查Numpy中2D数组是否包含特定的1D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆