二分图

Python 网络分析中级

Eric Ma

Data Carpentry instructor and author of nxviz package

二分图

  • 被划分为两组的图
  • 节点只与另一组的节点相连
  • 对比:"单分图"(unipartite)
Python 网络分析中级

二分图:示例

ch1-2.007.png

Python 网络分析中级

NetworkX 中的二分图

import networkx as nx
G = nx.Graph()

numbers = range(3) G.add_nodes_from(numbers, bipartite='customers')
letters = ['a', 'b'] G.add_nodes_from(letters, bipartite='products')
Python 网络分析中级

NetworkX 中的二分图

list(G.nodes(data=True))
[(0, {'bipartite': 'customers'}),
 (1, {'bipartite': 'customers'}),
 (2, {'bipartite': 'customers'}),
 ('b', {'bipartite': 'products'}),
 ('a', {'bipartite': 'products'})]
Python 网络分析中级

度中心性

  • 定义:$$\frac{\text{邻居数}}{\text{可能的邻居数}}$$
  • 可能的邻居数取决于图类型
Python 网络分析中级

二分图的中心性指标

  • 分母:对侧分区的节点数,而非所有其他节点

ch1-2.021.png

Python 网络分析中级

筛选图

cust_nodes = [n for n, d in G.nodes(data=True) if  
                  d['bipartite'] == 'customers']
cust_nodes
[(0, {'bipartite': 'customers'}),
 (1, {'bipartite': 'customers'}),
 (2, {'bipartite': 'customers'})]
nx.bipartite.degree_centrality(G, cust_nodes)
{0: 0.5,
 1: 0.5,
 2: 1.0,
 'a': 0.333,
 'b': 1.0}
Python 网络分析中级

让我们练习!

Python 网络分析中级

Preparing Video For Download...