def gen_network_matrix(num_nodes, net_df, node1, node2, weight, node2index):
"""Generates network adjacency matrix and normalizes it"""
# Transform the first two columns of the DataFrame -- the nodes -- to their indexes
net_df[node1] = net_df[node1].apply(lambda x: node2index[x])
net_df[node2] = net_df[node2].apply(lambda x: node2index[x])
# Create the sparse matrix
network_matrix = sparse.csr_matrix((net_df[weight].values, (net_df[node1].values, net_df[node2].values)),
shape=(num_nodes, num_nodes), dtype=float)
# Make the ajdacency matrix symmetric
network_matrix = (network_matrix + network_matrix.T)
network_matrix.setdiag(0)
# Normalize the rows of network_matrix because we are multiplying vector by matrix (from left)
network_matrix = normalize(network_matrix, norm='l1', axis=1)
return(net_df, network_matrix)
###############################################################################
评论列表
文章目录