開始使用免費開始

計算鄰接矩陣

現在,你要練習使用矩陣與稀疏矩陣相乘來計算投影!在這個練習中,你會用到 Python 3.5 引進的矩陣乘法運算子 @

你將延續使用美國獨立革命的圖。這裡關注的兩個分割是 'people''clubs'

本練習屬於課程

Python 網路分析進階

檢視課程

練習說明

  • 使用你在前一章定義的 get_nodes_from_partition() 函式,從圖 G 取得人員清單與社團清單。這個函式接受兩個參數:圖與分割。
  • 使用 nx.bipartite.biadjacency_matrix() 計算雙部鄰接矩陣,並將參數 row_order 設為 people_nodescolumn_order 設為 clubs_nodes。記得同時傳入圖 G
  • 計算使用者對使用者的投影:以 @ 運算子將雙部鄰接矩陣 bi_matrix 與其轉置 bi_matrix.T 相乘。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Get the list of people and list of clubs from the graph: people_nodes, clubs_nodes
people_nodes = ____
clubs_nodes = ____

# Compute the biadjacency matrix: bi_matrix
bi_matrix = ____(____, row_order=____, column_order=____)

# Compute the user-user projection: user_matrix
user_matrix = ____

print(user_matrix)
編輯並執行程式碼