我正在尝试通过预定义的节点标签将大型网络粗粒化到较小的网络。说:
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))
决定了这两个区域之间边缘的权重。
一种方法是循环遍历每个节点的边缘,如果边缘的两个端点位于两个区域的成员资格列表中,则将其添加到加权和。 我做对了吗? 有没有更好的直截了当的方法?
答案 0 :(得分:0)
您可以coo_matrix
scipy.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