[英]find the number of output edges of each node in weighted graph
I load text file of weighted graph. 我加载加权图的文本文件。 The text file contains three columns named "FromNodeId", "ToNodeId" and "Sign".
文本文件包含名为“ FromNodeId”,“ ToNodeId”和“ Sign”的三列。 Sign is weight of edge.
标志是边缘的重量。 value of Sign is -1 or 1. I want to find the number of output edges (output degree) with the "Sign=1" of each node.
Sign的值是-1或1。我想找到每个节点的“ Sign = 1”的输出边数(输出度)。 Please suggest me a way to solve this problem.
请为我提出解决此问题的方法。
import networkx as nx
G= nx.read_edgelist("soc-sign-epinions.txt",data = [('Sign', int)], create_using=nx.DiGraph())
nodes = G.nodes()
edges = G.edges()
You can do this directly using pandas. 您可以直接使用熊猫来做到这一点。 You can read in your data using pd.read_csv('path_to_file'), then filter for edges with positive sign, then groupby origin node and sum up the remaining signs.
您可以使用pd.read_csv('path_to_file')读入数据,然后过滤具有正号的边,然后对groupby原点节点进行归并并总结剩余的号。 Here is an example with fake data:
这是伪造数据的示例:
import pandas as pd
data=pd.DataFrame([['a','b',1],
['a','c',-1],
['a','d',1],
['b','a',1],
['b','d',-1],
['c','a',1],
['d','b',1]],
columns = ["FromNodeId", "ToNodeId","Sign"])
data[data['Sign']==1].groupby('FromNodeId')['Sign'].sum()
returns: 返回:
FromNodeId
a 2
b 1
c 1
d 1
Name: Sign, dtype: int64
So we'll break this down into two observations. 因此,我们将其分为两个观察值。 First we can access all the edges out of a node (and the associated data) with
G.edges(node, data = True)
. 首先,我们可以使用
G.edges(node, data = True)
访问节点(以及关联的数据)之外的所有边缘。 Second, there are ways to efficiently loop through these edges just counting those with positive sign. 其次,有一些方法可以有效地循环通过仅计算带正号的那些边。 More generally, this approach can be used to count the number of edges out of a node that have any particular property.
更一般而言,此方法可用于计算具有任何特定属性的节点边缘的数量。
import networkx as nx
G = nx.DiGraph()
G.add_edge(0,2,sign=-1)
G.add_edge(0,1, sign = 1)
G.add_edge(2,3,sign = 1)
G.add_edge(3,0, sign=-1)
print(G.edges(0, data=True))
>[(0, 2, {'sign': -1}), (0, 1, {'sign': 1})]
Note that the edge (3,0) did not appear here. 请注意,边(3,0)并未出现在此处。 So
G.edges(0, data=True)
results in the edges that start from 0
and includes the data you've attached to the edge. 因此,
G.edges(0, data=True)
导致边缘从0
开始,并包含您已附加到边缘的数据。 (in your final code, obviously you don't actually want this print statement). (在最终代码中,显然您实际上并不需要此打印语句)。
Now we'll use that in a generator and sum up the number of elements. 现在,我们将在生成器中使用它并汇总元素数量。
s = sum(1 for (u,v,d) in G.edges(0, data=True) if d['sign']==1)
print(s)
> 1
What I've done is create a generator that goes through all edges out of 0
and if the sign is 1
, it adds 1 to the output. 我所做的是创建一个生成器,该生成器的所有边都超出
0
并且如果符号为1
,则它会将1加到输出中。
If that last sentence doesn't make sense, look at this answer: https://stackoverflow.com/a/7223557/2966723 to given an idea of what is going on, and for more about generators, start with Understanding generators in Python . 如果最后一句话没有意义,请查看以下答案: https : //stackoverflow.com/a/7223557/2966723 ,以了解发生的事情,有关生成器的更多信息,请从了解Python中的生成器开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.