使用numpy有效地测试矩阵行和列 [英] Efficiently test matrix rows and columns with numpy
问题描述
当第i行和第列都包含全0时,我试图同时删除第i行和第i列.例如,在这种情况下,我们可以看到第0行全为零,第0列全为零,因此删除了第0行和第0列.与第2列和第4列的行相同.第1行全为零,但第1列全为零,因此都不会删除.
I am trying to remove both the row i and column i when both the row i and column i contains all 0s. For example in this case we can see that row 0 is all zeros and column 0 is all zeros and thus row and column 0 is removed. Same with row column pair 2 and 4. Row 1 is all zeros but column 1 is not so neither are removed.
[0,0,0,0,0]
[0,1,0,1,0]
[0,0,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
将成为
[1,1]
[0,0]
另一个例子:
[0,0,1,0,0,1]
[0,0,0,0,0,0]
[0,0,0,0,0,0]
[0,0,0,0,0,0]
[0,0,0,0,0,0]
[0,0,1,0,1,0]
将更改为:
[0,1,0,1]
[0,0,0,0]
[0,0,0,0]
[0,1,1,0]
这是我用来计算的代码:
This is the code that I am using to compute:
def remove(matrix):
for i, x in reversed(list(enumerate(matrix))):
if np.all(matrix == 0, axis=0)[i] and np.all(matrix == 0, axis=1)[i]:
matrix = np.delete(matrix,i,axis=0)
matrix = np.delete(matrix,i,axis=1)
return matrix
经过测试,这条线是迄今为止最多的时间:
After testing this line is taking the most time by far:
if np.all(matrix == 0, axis=0)[i] and np.all(matrix == 0, axis=1)[i]:
是否有更合适的方法以这种方式测试行和列?我使用的矩阵是一个稀疏的二进制矩阵.我不使用任何稀疏矩阵类,只是ndarray.
Is there a more appropriate way to test a row and column in this way? The matrix that I am using is a sparse binary matrix. I am not using any sparse matrix classes just ndarray.
推荐答案
带遮罩的矢量化方法-
def remove_vectorized(a):
mask = a==0
m_row = ~mask.all(1)
m_col = ~mask.all(0)
comb_mask = m_row | m_col
return a[comb_mask][:,comb_mask] #or a[np.ix_(comb_mask, comb_mask)]
示例运行
案例1:
In [485]: a
Out[485]:
array([[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
In [486]: remove_vectorized(a)
Out[486]:
array([[1, 1],
[0, 0]])
案例2:
In [489]: a
Out[489]:
array([[0, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0]])
In [490]: remove_vectorized(a)
Out[490]:
array([[0, 1, 0, 1],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 1, 0]])
这篇关于使用numpy有效地测试矩阵行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!