简体   繁体   English

Networkx中的加权邻接矩阵计算度

[英]calculation of degree from a weighted adjacency matrix in networkx

I have an adjacency matrix with the non zero elements indicating the weights of the link.The weights are decimals. 我有一个邻接矩阵,其中非零元素表示链接的权重。权重是小数。 I would like to obtain the node strength of each of the nodes ie, the sum of the edge weights adjacent to the node and also the weight distribution. 我想获得每个节点的节点强度,即与该节点相邻的边缘权重之和以及权重分布。 I tried the following code: 我尝试了以下代码:

import networkx as nx
G=nx. Graph(a)  # a is the adjacency matrix.
w=G.degree()

But i get the degree of each node as the answer and not the sum of the weights of the links connected to the node. 但是我得到每个节点的度数作为答案,而不是连接到该节点的链接权重之和。 Any help in this regard will be highly appreciable. 在这方面的任何帮助将非常可观。 I am new to networkx. 我是networkx的新手。

If the vertex sum of whose edges' weights need be found is vertex and the NetworkX graph is G , you could do something like 如果需要找到其边缘权重的vertex和为vertex ,而NetworkX图为G ,则可以执行以下操作

s=0
for neighbor in G[vertex]:
    s+=G[vertex][neighbor]['weight']
print(s)

G[vertex] will give all the details of all the vertices connected to the vertex vertex and G[vertex][neighbor] gives the details about the edge between vertex and neighbor vertices from which the weight information is taken using G[vertex][neighbor]['weight'] . G[vertex]会给所有连接到顶点的顶点的所有细节vertexG[vertex][neighbor]给出关于之间的边缘细节vertexneighbor从该重量信息是利用采取顶点G[vertex][neighbor]['weight']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM