讀取圖形
在這個練習中,在計算投影之前,你會先練習使用 NetworkX 的一個磁碟 I/O 函式 read_edgelist()。read_edgelist() 會從 edgelist 檔案建立圖形。你將要處理的是一個描述美國獨立革命的二部圖。圖中有兩個節點分區——'people' 與 'clubs',邊表示某個人是某個社團的成員。
本練習屬於課程
Python 網路分析進階
練習說明
- 將
networkx以nx匯入。 - 使用
nx.read_edgelist()讀取'american-revolution.edgelist'。 - 在這個資料集中,
'clubs'的節點名稱中不包含.符號。利用這個資訊把節點分配到'clubs'或'people'分區。別忘了使用'bipartite'關鍵字! - 列印圖形的邊。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import networkx
import networkx as nx
# Read in the data: g
G = ____
# Assign nodes to 'clubs' or 'people' partitions
for n, d in G.nodes(data=True):
if '.' in n:
G.nodes[n]['____'] = '____'
else:
____ = '____'
# Print the edges of the graph
print(____)