[英]Python Extracting rows not in another numpy array
Given two numpy matrices 'a' and 'b', I am trying to extract rows in 'a' that are not in 'b'.给定两个 numpy 矩阵“a”和“b”,我试图提取“a”中不在“b”中的行。 The problem is the dimension of 'b' is not fixed.
问题是“b”的尺寸不固定。 If I use.tolist(), then it does not work when 'b' has dimension = 1, since it considers each row with individual elements of 'b' instead of the entire 'b' array.
如果我使用.tolist(),那么当'b' 的维度= 1 时它不起作用,因为它认为每一行都有'b' 的单个元素而不是整个'b' 数组。
Here are results of some functions that I tried: Link to the image以下是我尝试的一些功能的结果:链接到图片
In the image, the first and last result is correct.在图像中,第一个和最后一个结果是正确的。 If 'b' is a matrix, then converting to list works, but if it is an array, then.all().any() works.
如果 'b' 是矩阵,则转换为列表有效,但如果它是数组,则 .all().any() 有效。
np.isin() also does not work since 'a' is multi-dimensional. np.isin() 也不起作用,因为 'a' 是多维的。
What is a general way to achieve this?实现这一目标的一般方法是什么?
np.isin
actually works and the correct way to do is like this: np.isin
实际上有效,正确的做法是这样的:
>>> a = np.arange(1, 10).reshape(3,3)
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> b
array([[1, 2, 3],
[4, 5, 6]])
>>> c = np.arange(1, 4).reshape(1, 3)
>>> c
array([[1, 2, 3]])
Then elements in a
not in b
are:那么
a
not in b
中的元素是:
>>> a[~np.isin(a,b)].reshape(-1, a.shape[1])
array([[7, 8, 9]])
Elements in a
not in c
are (assuming the no. of columns are same in both matrices):不在
c
中a
元素是(假设两个矩阵中的列数相同):
>>> a[~np.isin(a,c)].reshape(-1, a.shape[1])
array([[4, 5, 6],
[7, 8, 9]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.