二分圖的矩陣表示法

Python 網路分析進階

Eric Ma

Data Carpentry instructor and author of nxviz package

矩陣表示

  • 列:一側分區的節點
  • 欄:另一側分區的節點
  • 儲存格:有邊為 1,否則為 0
Python 網路分析進階

矩陣表示

ch2-2.006.png

Python 網路分析進階

範例程式碼

cust_nodes = [n for n in G.nodes() if G.nodes[n]
                 ['bipartite'] == 'customers']
prod_nodes = [n for n in G.nodes() if G.nodes[n]
                 ['bipartite'] == 'products']

mat = nx.bipartite.biadjacency_matrix(G, row_order=cust_nodes, column_order=prod_nodes)
mat
<3x2 sparse matrix of type '<class 'numpy.int64'>'
with 3 stored elements in Compressed Sparse Row format>
Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.014.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.015.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.016.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.017.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.018.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.019.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.020.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.021.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.022.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.023.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.024.png

Python 網路分析進階

矩陣投影

  • 以矩陣乘法計算投影

ch2-2.025.png

Python 網路分析進階

在 Python 中做矩陣乘法

mat @ mat.T
<5x5 sparse matrix of type '<class 'numpy.int64'>'
 with 23 stored elements in Compressed Sparse Row format>
mat.T @ mat
<10x10 sparse matrix of type '<class 'numpy.int64'>'
 with 50 stored elements in Compressed Sparse Column format>
Python 網路分析進階

一起來練習吧!

Python 網路分析進階

Preparing Video For Download...