generic_weighted_projected_graph#
- generic_weighted_projected_graph(B, nodes, weight_function=None)[source]#
使用用户指定的权重函数对 B 进行加权投影。
二分网络 B 根据用户指定的函数计算出的权重投影到指定的节点上。此函数必须接受输入图和两个节点作为参数,并返回一个整数或浮点数。
节点保留其属性,并且如果在原始图中与某个公共节点存在边,则在结果图中连接。
- 参数:
- BNetworkX 图
输入图应为二分图。
- nodes列表或可迭代对象
要投影到的节点(“底部”节点)。
- weight_function函数
此函数必须接受与本函数相同的输入图和两个节点作为参数;并返回一个整数或浮点数。默认函数计算共享邻居的数量。
- 返回:
- GraphNetworkX 图
投影到给定节点上的图。
另请参阅
is_bipartite
is_bipartite_node_set
sets
weighted_projected_graph
collaboration_weighted_projected_graph
overlap_weighted_projected_graph
projected_graph
注意
未尝试验证输入图 B 是否为二分图。图和节点属性(浅层)复制到投影图。
有关 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})]