generic_weighted_projected_graph#

generic_weighted_projected_graph(B, nodes, weight_function=None)[源代码]#

使用用户指定的权重函数对b进行加权投影。

将二部网络B投影到由用户指定函数计算权重的指定节点上。此函数必须接受两个节点的邻域集作为参数,并返回一个整数或浮点。

如果节点与原始图中的公共节点有边,则这些节点保留其属性并连接到结果图中。

参数
B网络X图表

输入图应该是二部图。

nodes列表或可迭代

要投影到的节点(“底部”节点)。

weight_function功能

此函数必须接受与此函数相同的输入图形和两个节点作为参数;并返回一个整数或浮点数。默认函数计算共享邻居的数量。

返回
Graph网络X图表

是在给定节点上的投影的图。

笔记

未尝试验证输入图B是否为二部分。图形和节点属性(浅)复制到投影图形。

bipartite documentation 有关如何在NetworkX中处理二部图的详细信息。

实例

>>> from networkx.algorithms import bipartite
>>> # Define some custom weight functions
>>> def jaccard(G, u, v):
...     unbrs = set(G[u])
...     vnbrs = set(G[v])
...     return float(len(unbrs & vnbrs)) / len(unbrs | vnbrs)
...
>>> def my_weight(G, u, v, weight="weight"):
...     w = 0
...     for nbr in set(G[u]) & set(G[v]):
...         w += G[u][nbr].get(weight, 1) + G[v][nbr].get(weight, 1)
...     return w
...
>>> # A complete bipartite graph with 4 nodes and 4 edges
>>> B = nx.complete_bipartite_graph(2, 2)
>>> # Add some arbitrary weight to the edges
>>> for i, (u, v) in enumerate(B.edges()):
...     B.edges[u, v]["weight"] = i + 1
...
>>> for edge in B.edges(data=True):
...     print(edge)
...
(0, 2, {'weight': 1})
(0, 3, {'weight': 2})
(1, 2, {'weight': 3})
(1, 3, {'weight': 4})
>>> # By default, the weight is the number of shared neighbors
>>> G = bipartite.generic_weighted_projected_graph(B, [0, 1])
>>> print(list(G.edges(data=True)))
[(0, 1, {'weight': 2})]
>>> # To specify a custom weight function use the weight_function parameter
>>> G = bipartite.generic_weighted_projected_graph(
...     B, [0, 1], weight_function=jaccard
... )
>>> print(list(G.edges(data=True)))
[(0, 1, {'weight': 1.0})]
>>> G = bipartite.generic_weighted_projected_graph(
...     B, [0, 1], weight_function=my_weight
... )
>>> print(list(G.edges(data=True)))
[(0, 1, {'weight': 10})]