Scipy:稀疏布尔矩阵的并集

时间:2019-02-17 11:37:27

标签: merge scipy boolean sparse-matrix

在Scipy中,最有效的方法是获得具有相同形状的多个布尔稀疏(Option Explicit Const MONITOR_FILE = "c:\temp\test.txt" Const MONITOR_LIMIT = 10 Dim wmi Set wmi = GetObject( "winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2" ) Dim query query = "SELECT * FROM __InstanceOperationEvent WITHIN 1 " _ & " WHERE TargetInstance ISA 'CIM_DataFile' " _ & " AND TargetInstance.Name='" & Replace(MONITOR_FILE, "\", "\\") & "'" Dim colEvents Set colEvents = wmi.ExecNotificationQuery( query ) Dim currentEvent Do ' Flag value Set currentEvent = Nothing ' Try to get the next event with a timeout limit ' If a timeout happens we need to catch raised error On Error Resume Next Set currentEvent = colEvents.NextEvent( MONITOR_LIMIT * 1000 ) On Error GoTo 0 ' If there is not an event there was a timeout If currentEvent Is Nothing Then Exit Do End If Loop WScript.Echo "File has not been changed for " & MONITOR_LIMIT & " seconds." )矩阵A+B+C的并集csr

联合意味着:

  • 稀疏度变化
  • 可能会重叠

1 个答案:

答案 0 :(得分:2)

只需添加它们:

import scipy.sparse as sparse
x = sparse.csr_matrix([[True, True, False], [False, False, False], [True, False, False]] , dtype=bool)
y = sparse.csr_matrix([[False, True, False], [False, True, False], [False, True, False]], dtype=bool)
print((x + y).todense())
>>[[ True  True False]
 [False  True False]
 [ True  True False]]

编辑

如果您想直接访问索引,则可以使用coo格式(允许检索行和列索引),堆叠索引并使用np.unique(免责声明:我没有检查效率比较):

import scipy.sparse as sparse
c2=sparse.eye(5, k=1, dtype=bool, format='coo')
c1=sparse.eye(5, dtype=bool, format='coo')
c3 = c1.copy()
c3.row, c3.col = np.unique(np.hstack((np.vstack((c1.col, c1.row)),np.vstack((c2.col, c2.row)))), axis=1)
c3.data = np.ones(c3.row.size, dtype=bool)
c3.todense()
>> matrix([[ True, False, False, False, False],
    [ True,  True, False, False, False],
    [False,  True,  True, False, False],
    [False, False,  True,  True, False],
    [False, False, False,  True,  True]])