投影的概念

Python 网络分析中级

Eric Ma

Data Carpentry instructor and author of nxviz package

投影

  • 将二部连通性表示为单部图
    • 以与另一分区的连接为条件
Python 网络分析中级

投影

  • 将二部连通性表示为单部图

单张图-二部连通示意

Python 网络分析中级

投影

  • 将二部连通性表示为单部图

单张图-二部连通示意

Python 网络分析中级

投影

  • 将二部连通性表示为单部图

单张图-二部连通示意

Python 网络分析中级

投影

  • 将二部连通性表示为单部图

单张图-二部连通示意

Python 网络分析中级

磁盘上的图

  • 扁平边列表
  • CSV 文件:节点列表 + 元数据,边列表 + 元数据
Python 网络分析中级

读取网络数据

import networkx as nx
G = nx.read_edgelist('american-revolution.txt')

list(G.edges(data=True))[0:5]
[('Parkman.Elias', 'LondonEnemies', {'weight': 1}),
 ('Parkman.Elias', 'NorthCaucus', {'weight': 1}),
 ('Inglish.Alexander', 'StAndrewsLodge', {'weight': 1}),
 ('NorthCaucus', 'Chadwell.Mr', {'weight': 1}),
 ('NorthCaucus', 'Pearce.IsaacJun', {'weight': 1})]
  • 文本文件
    Barrett.Samuel LondonEnemies {'weight': 1}
    Barrett.Samuel StAndrewsLodge {'weight': 1}
    Marshall.Thomas LondonEnemies {'weight': 1}
    Eaton.Joseph TeaParty {'weight': 1}
    Bass.Henry LondonEnemies {'weight': 1}
    
Python 网络分析中级

二部图投影

list(G.nodes())
['product2', 'customer3', 'customer1', 'product3',  
    'customer2', 'product1']
list(G.edges())
[('product2', 'customer1'),
 ('product2', 'customer2'),
 ('customer3', 'product1')]
Python 网络分析中级

二部图投影

cust_nodes = [n for n in G.nodes() if G.nodes[n]
                  ['bipartite'] == 'customers']
cust_nodes
['customer3', 'customer1', 'customer2']
Python 网络分析中级

二部图投影

G_cust = nx.bipartite.projected_graph(G, cust_nodes)
list(G_cust.nodes())
['customer1', 'customer3', 'customer2']
list(G_cust.edges())
[('customer1', 'customer2')]
Python 网络分析中级

度中心性

  • 回顾度中心性定义 $$\frac{\text{邻居数}}{\text{可能的邻居数}}$$
  • 分母:对侧分区的节点数
Python 网络分析中级

二部图度中心性

nx.bipartite.degree_centrality(G, cust_nodes)
{'customer1': 0.3333333333333333,
 'customer2': 0.3333333333333333,
 'customer3': 0.3333333333333333,
 'product1': 0.3333333333333333,
 'product2': 0.6666666666666666,
 'product3': 0.0}
Python 网络分析中级

二部图度中心性

nx.degree_centrality(G)
{'customer1': 0.2,
 'customer2': 0.2,
 'customer3': 0.2,
 'product1': 0.2,
 'product2': 0.4,
 'product3': 0.0}
Python 网络分析中级

Vamos praticar!

Python 网络分析中级

Preparing Video For Download...