python – 二维矩阵:查找和删除作为其他列的子集的列
发布时间:2020-12-20 13:12:27 所属栏目:Python 来源:网络整理
导读:我有一个问题,我想识别和删除逻辑矩阵中的列是其他列的子集.即[1,1]是[1,1,1]的子集;但[1,0]和[0,1]都不是彼此的子集.我写了一段快速代码来识别作为子集的列,它使用一对嵌套for循环进行(n ^ 2-n)/ 2次检查. import numpy as npA = np.array([[1,1],[0,0],[1,
我有一个问题,我想识别和删除逻辑矩阵中的列是其他列的子集.即[1,1]是[1,1,1]的子集;但[1,0]和[0,1]都不是彼此的子集.我写了一段快速代码来识别作为子集的列,它使用一对嵌套for循环进行(n ^ 2-n)/ 2次检查.
import numpy as np A = np.array([[1,1],[0,0],[1,0]]) rows,cols = A.shape columns = [True]*cols for i in range(cols): for j in range(i+1,cols): diff = A[:,i]-A[:,j] if all(diff >= 0): print "%d is a subset of %d" % (j,i) columns[j] = False elif all(diff <= 0): print "%d is a subset of %d" % (i,j) columns[i] = False B = A[:,columns] 解决方案应该是 >>> print B [[1 0 0] [0 1 1] [1 1 0] [1 0 1] [1 0 1] [1 0 0] [0 1 1] [0 1 0]] 对于大型矩阵,我确信有一种方法可以更快地完成这项工作.一个想法就是在我去的时候消除子列,所以我不会检查已知为子集的列.另一个想法是对此进行矢量化,因此不要进行O(n ^ 2)运算.谢谢. 解决方法
这是使用
NumPy broadcasting 的另一种方法 –
A[:,~((np.triu(((A[:,:,None] - A[:,None,:])>=0).all(0),1)).any(0))] 下面列出了详细的评论说明 – # Perform elementwise subtractions keeping the alignment along the columns sub = A[:,:] # Look for >=0 subtractions as they indicate non-subset criteria mask3D = sub>=0 # Check if all elements along each column satisfy that criteria giving us a 2D # mask which represent the relationship between all columns against each other # for the non subset criteria mask2D = mask3D.all(0) # Finally get the valid column mask by checking for all columns in the 2D mas # that have at least one element in a column san the diagonal elements. # Index into input array with it for the final output. colmask = ~(np.triu(mask2D,1).any(0)) out = A[:,colmask] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |