粗粒度图(networkx)

问题描述 投票:0回答:1

我试图通过预定义的节点标签将大型网络粗粒化到较小的网络。说:

large_network = np.random.rand(100,100)
labels = [1,1,1,1,
          5,5,5,5,5,5,5,5,
          0,0,0,0,0, ...] #[1x100] 

例如,我们有10个区域,每个区域都有几个节点。像成员列表(在networkx中的网络社区检测算法),它告诉每个节点属于哪个社区,但在这里我手动定义它。然后我需要计算新的减少的邻接矩阵说[10x10]

因此,区域A和B之间的边缘的平均权重w_{AB} = mean(edges(A, B))确定这两个区域之间边缘的权重。

一种方法是遍历每个节点的边缘,如果边缘的两个端点位于两个区域的成员资格列表中,则将其添加到加权和。我做对了吗?有没有更好的直截了当的方法?

python matrix graph igraph networkx
1个回答
0
投票

你可以在coo_matrixscipy.sparse为你做这份工作。好消息是,这种方法可以很容易地扩展到稀疏的网络表示。

import numpy as np
from scipy.sparse import coo_matrix

# set parameters
N = 100 # no of nodes
M = 10  # no of types

# initialise random network and random node labels
weights = np.random.rand(N, N) # a.k.a "large_network"
labels = np.random.randint(0, M, size=N)

# get sum of weights by connection type
indices = np.tile(labels, (N,1)) # create N x N matrix of labels
nominator = coo_matrix((weights.ravel(), (indices.ravel(), indices.transpose().ravel())), shape=(M,M)).todense()

# count number of weights by connection type
adjacency = (weights > 0.).astype(np.int)
denominator = coo_matrix((adjacency.ravel(), (indices.ravel(), indices.transpose().ravel())), shape=(M,M)).todense()

# normalise sum of weights by counts
small_network = nominator / denominator
© www.soinside.com 2019 - 2024. All rights reserved.