開始使用免費開始

讀取圖形

在這個練習中,在計算投影之前,你會先練習使用 NetworkX 的一個磁碟 I/O 函式 read_edgelist()read_edgelist() 會從 edgelist 檔案建立圖形。你將要處理的是一個描述美國獨立革命的二部圖。圖中有兩個節點分區——'people''clubs',邊表示某個人是某個社團的成員。

本練習屬於課程

Python 網路分析進階

檢視課程

練習說明

  • networkxnx 匯入。
  • 使用 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(____)
編輯並執行程式碼