我使用邻接矩阵来表示可以在视觉上解释为的朋友网络
Mary 0 1 1 1
Joe 1 0 1 1
Bob 1 1 0 1
Susan 1 1 1 0
Mary Joe Bob Susan
Run Code Online (Sandbox Code Playgroud)
使用这个矩阵,我想编译所有可能的友谊三角形的列表,条件是用户1是用户2的朋友,用户2是用户3的朋友.对于我的列表,不要求用户1是朋友用户3.
(joe, mary, bob)
(joe, mary, susan)
(bob, mary, susan)
(bob, joe, susan)
Run Code Online (Sandbox Code Playgroud)
我有一些适用于小三角形的代码,但我需要它来扩展非常大的稀疏矩阵.
from numpy import *
from scipy import *
def buildTriangles(G):
# G is a sparse adjacency matrix
start = time.time()
ctr = 0
G = G + G.T # I do this to make sure it is symmetric
triples = [] …Run Code Online (Sandbox Code Playgroud) 我是Python新手,所以如果这是一个基本问题,请提前原谅我,但我已经四处寻找并且没有找到令人满意的答案.
我正在尝试使用NumPy和SciPy执行以下操作:
I,J = x[:,0], x[:1] # x is a two column array of (r,c) pairs
V = ones(len(I))
G = sparse.coo_matrix((V,(I,J))) # G's dimensions are 1032570x1032570
G = G + transpose(G)
r,c = G.nonzero()
G[r,c] = 1
...
NotImplementedError: Fancy indexing in assignment not supported for csr matrices
Run Code Online (Sandbox Code Playgroud)
差不多,我希望在添加转置后所有非零值都等于1,但我得到了花哨的索引错误消息.
或者,如果我可以证明矩阵G是对称的,则不需要添加转置.
任何方法的任何见解都将非常感激.