計算鄰接矩陣
現在,你要練習使用矩陣與稀疏矩陣相乘來計算投影!在這個練習中,你會用到 Python 3.5 引進的矩陣乘法運算子 @。
你將延續使用美國獨立革命的圖。這裡關注的兩個分割是 'people' 與 'clubs'。
本練習屬於課程
Python 網路分析進階
練習說明
- 使用你在前一章定義的
get_nodes_from_partition()函式,從圖G取得人員清單與社團清單。這個函式接受兩個參數:圖與分割。 - 使用
nx.bipartite.biadjacency_matrix()計算雙部鄰接矩陣,並將參數row_order設為people_nodes、column_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)