python scipy/numpy中按行过滤矩阵元素

python scipy/numpy中按行过滤矩阵元素,python,numpy,scipy,Python,Numpy,Scipy,在Python中,如何根据行上的某些条件过滤scipy/numpy中NxM矩阵的元素 例如,您可以在where(my_matrix!=3)中按行处理矩阵,这样您就可以询问where(my_matrix!=some_other_row),过滤掉所有不等于some other_row的行。如何做到这一点?假设您有一个矩阵 a = numpy.array([[0, 1, 2], [3, 4, 5], [0, 1, 2]]) 你想得到

在Python中,如何根据行上的某些条件过滤
scipy/numpy
NxM
矩阵的元素


例如,您可以在where(
my_matrix!=3
)中按行处理矩阵,这样您就可以询问where
(my_matrix!=some_other_row)
,过滤掉所有不等于
some other_row
的行。如何做到这一点?

假设您有一个矩阵

a = numpy.array([[0, 1, 2],
                 [3, 4, 5],
                 [0, 1, 2]])
你想得到不等于的行的索引

row = numpy.array([0, 1, 2])
你可以通过

indices, = (a != row).any(1).nonzero()
a!=行
a
的每一行与
元素进行比较,返回与
a
形状相同的布尔数组。然后,我们沿着第一个轴使用
any()
,查找任何元素与
行中相应元素不同的行。最后,
nonzero()。