投影的概念

Python 網路分析進階

Eric Ma

Data Carpentry instructor and author of nxviz package

投影(Projection)

  • 用來分析同一分割中的節點關係
    • 以前往另一分割的連結作為條件
Python 網路分析進階

投影(Projection)

  • 將二分網路連結轉為單分網路表示

ch2-1.006.png

Python 網路分析進階

投影(Projection)

  • 將二分網路連結轉為單分網路表示

ch2-1.007.png

Python 網路分析進階

投影(Projection)

  • 將二分網路連結轉為單分網路表示

ch2-1.008.png

Python 網路分析進階

投影(Projection)

  • 將二分網路連結轉為單分網路表示

ch2-1.009.png

Python 網路分析進階

磁碟上的圖形資料

  • 扁平邊列表
  • CSV 檔:nodelist + 中介資料,edgelist + 中介資料
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 網路分析進階

一起來練習吧!

Python 網路分析進階

Preparing Video For Download...