This seems like a simple question, but I haven't been able to find a good answer.
这似乎是一个简单的问题,但我一直没能找到一个好的答案。
I'm looking for a pythonic way to test whether a 2d numpy array contains a given row. For example:
我正在寻找一种python方法来测试2d numpy数组是否包含给定的行。例如:
myarray = numpy.array([[0,1],
[2,3],
[4,5]])
myrow1 = numpy.array([2,3])
myrow2 = numpy.array([2,5])
myrow3 = numpy.array([0,3])
myrow4 = numpy.array([6,7])
Given myarray, I want to write a function that returns True if I test myrow1, and False if I test myrow2, myrow3 and myrow4.
给定myarray,我想编写一个函数,如果我测试myrow1,返回True;如果我测试myrow2、myrow3和myrow4,返回False。
I tried the "in" keyword, and it didn't give me the results I expected:
我尝试了“in”关键字,但没有得到我期望的结果:
>>> myrow1 in myarray
True
>>> myrow2 in myarray
True
>>> myrow3 in myarray
True
>>> myrow4 in myarray
False
It seems to only check if one or more of the elements are the same, not if all elements are the same. Can someone explain why that's happening?
它似乎只检查一个或多个元素是否相同,而不检查所有元素是否相同。有人能解释一下为什么会这样吗?
I can do this test element by element, something like this:
我可以按元素来做这个测试元素,像这样:
def test_for_row(array,row):
numpy.any(numpy.logical_and(array[:,0]==row[0],array[:,1]==row[1]))
But that's not very pythonic, and becomes problematic if the rows have many elements. There must be a more elegant solution. Any help is appreciated!
但这不是很复杂,如果行有很多元素,就会有问题。一定有更好的解决方案。任何帮助都是赞赏!
5 个解决方案
#1
3
You can just simply subtract your test row from the array. Then find out the zero elements, and sum over column wise. Then those are matches where the sum equals the number of columns.
只需从数组中减去测试行即可。然后找出零元素,并对列求和。然后它们的和等于列数。
For example:
例如:
In []: A= arange(12).reshape(4, 3)
In []: A
Out[]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
In []: 3== (0== (A- [3, 4, 5])).sum(1)
Out[]: array([False, True, False, False], dtype=bool)
Update: based on comments and other answers:Paul
's suggestion seems indeed to be able to streamline code:
更新:基于评论和其他答案:Paul的建议似乎确实能够简化代码:
In []: ~np.all(A- [3, 4, 5], 1)
Out[]: array([False, True, False, False], dtype=bool)
JoshAdel
's answer emphasis more generally the problem related to determine 100% reliable manner the equality. So, obviously my answer is valid only in the situations where equality can be determined unambiguous manner.
约沙德尔的回答更一般地强调了与确定100%可靠的平等方式有关的问题。所以,显然,我的回答只适用于可以明确地确定平等的情况下。
Update 2: But as Emma
figured it out, there exists corner cases where Paul
's solution will not produce correct results.
更新2:但是,正如Emma发现的那样,有一些角落的案例,保罗的解决方案不会产生正确的结果。
#2
4
The SO question below should help you out, but basically you can use:
下面的SO问题应该能帮到你,但是基本上你可以用:
any((myrow1 == x).all() for x in myarray)
Numpy。在Python列表数组吗?
#3
1
This is a generalization of @maz's solution that handles floats more elegantly, where strict equality is going to fail:
这是对@maz解决方案的概括,该解决方案更优雅地处理浮点数,在这种情况下,严格的平等将会失败:
import numpy as np
def test_for_row(myarray,row):
return any(np.allclose(row,x) for x in myarray)
See http://docs.scipy.org/doc/numpy/reference/generated/numpy.allclose.html for details. Also as a side note, be careful that you haven't done something like from numpy import *
since np.any
and python's built-in any
will result in different answers, the former being incorrect.
有关详细信息,请参阅http://docs.scipy.org/doc/numpy/reference/generated/numpy.allclose.html。另外,要注意,从np开始,你就没有做过numpy导入*之类的事情。any和python的内置any将导致不同的答案,前者是不正确的。
#4
0
I ran into the same problem, and the following approach works for me
我遇到了同样的问题,以下方法对我很有效
def is_row_in_matrix(row, matrix):
return sum(np.prod(matrix == row, axis = 1))
Basically, test if each element of the row is in the corresponding column of the matrix, then multiply along the column (axis = 1)
, and sum the result.
基本上,测试行中的每个元素是否在矩阵的对应列中,然后沿着列(轴= 1)相乘,并求和结果。
#5
0
How about:
如何:
def row_in_array(myarray, myrow):
return (myarray == myrow).all(-1).any()
This is what it looks like for your test cases:
这就是您的测试用例的外观:
myarray = numpy.array([[0,1],
[2,3],
[4,5]])
row_in_array(myarray, [2, 3])
# True
row_in_array(myarray, [2, 5])
# False
row_in_array(myarray, [0, 3])
# False
row_in_array(myarray, [6, 7])
# False
#1
3
You can just simply subtract your test row from the array. Then find out the zero elements, and sum over column wise. Then those are matches where the sum equals the number of columns.
只需从数组中减去测试行即可。然后找出零元素,并对列求和。然后它们的和等于列数。
For example:
例如:
In []: A= arange(12).reshape(4, 3)
In []: A
Out[]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
In []: 3== (0== (A- [3, 4, 5])).sum(1)
Out[]: array([False, True, False, False], dtype=bool)
Update: based on comments and other answers:Paul
's suggestion seems indeed to be able to streamline code:
更新:基于评论和其他答案:Paul的建议似乎确实能够简化代码:
In []: ~np.all(A- [3, 4, 5], 1)
Out[]: array([False, True, False, False], dtype=bool)
JoshAdel
's answer emphasis more generally the problem related to determine 100% reliable manner the equality. So, obviously my answer is valid only in the situations where equality can be determined unambiguous manner.
约沙德尔的回答更一般地强调了与确定100%可靠的平等方式有关的问题。所以,显然,我的回答只适用于可以明确地确定平等的情况下。
Update 2: But as Emma
figured it out, there exists corner cases where Paul
's solution will not produce correct results.
更新2:但是,正如Emma发现的那样,有一些角落的案例,保罗的解决方案不会产生正确的结果。
#2
4
The SO question below should help you out, but basically you can use:
下面的SO问题应该能帮到你,但是基本上你可以用:
any((myrow1 == x).all() for x in myarray)
Numpy。在Python列表数组吗?
#3
1
This is a generalization of @maz's solution that handles floats more elegantly, where strict equality is going to fail:
这是对@maz解决方案的概括,该解决方案更优雅地处理浮点数,在这种情况下,严格的平等将会失败:
import numpy as np
def test_for_row(myarray,row):
return any(np.allclose(row,x) for x in myarray)
See http://docs.scipy.org/doc/numpy/reference/generated/numpy.allclose.html for details. Also as a side note, be careful that you haven't done something like from numpy import *
since np.any
and python's built-in any
will result in different answers, the former being incorrect.
有关详细信息,请参阅http://docs.scipy.org/doc/numpy/reference/generated/numpy.allclose.html。另外,要注意,从np开始,你就没有做过numpy导入*之类的事情。any和python的内置any将导致不同的答案,前者是不正确的。
#4
0
I ran into the same problem, and the following approach works for me
我遇到了同样的问题,以下方法对我很有效
def is_row_in_matrix(row, matrix):
return sum(np.prod(matrix == row, axis = 1))
Basically, test if each element of the row is in the corresponding column of the matrix, then multiply along the column (axis = 1)
, and sum the result.
基本上,测试行中的每个元素是否在矩阵的对应列中,然后沿着列(轴= 1)相乘,并求和结果。
#5
0
How about:
如何:
def row_in_array(myarray, myrow):
return (myarray == myrow).all(-1).any()
This is what it looks like for your test cases:
这就是您的测试用例的外观:
myarray = numpy.array([[0,1],
[2,3],
[4,5]])
row_in_array(myarray, [2, 3])
# True
row_in_array(myarray, [2, 5])
# False
row_in_array(myarray, [0, 3])
# False
row_in_array(myarray, [6, 7])
# False