Python 粗粒化图形(networkx)
我试图通过预定义的节点标签将大型网络粗化为小型网络。说:Python 粗粒化图形(networkx),python,matrix,graph,igraph,networkx,Python,Matrix,Graph,Igraph,Networkx,我试图通过预定义的节点标签将大型网络粗化为小型网络。说: 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中的网络社区检测算法中),它告诉每个节点属于哪个社区,但这里我手动定义它。然后我需要计算新的简化邻接矩阵,比如说[
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]
因此,w_{AB}=平均值(边(A,B))
的区域A和B之间边的平均权重决定了这两个区域之间边的权重
一种方法是在每个节点的边上循环,如果边的两个端点在两个区域的成员列表中,则将其添加到加权和中。
我做得对吗?
有没有更好的strightforward方法?你可以在
scipy.sparse
中coo_matrix
为你做这项工作。好的是,这种方法可以很容易地扩展到稀疏网络表示
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