建立轉推網路
社群媒體天生就是網路型資料。Twitter 的網路會以多種方式呈現,其中最重要的一種就是轉推(retweet)網路。我們可以把它表示成「有向」圖,把進行轉推的使用者視為來源,把被轉推的人視為目標。有了已攤平的 DataFrame 格式 Twitter 資料後,我們就能匯入 networkx 並建立一個轉推網路。
在這個練習以及本課程其餘部分中,我們會使用一個以唐納・川普發表的 2018 年國情咨文(State of the Union)演說 為基礎的資料集。這些推文已經替你載入為 sotu_retweets。
本練習屬於課程
使用 Python 分析社群媒體資料
練習說明
- 將
networkx以nx的別名匯入。 - 在
source參數中使用使用者的帳號名稱(screen name)。 - 在
target參數中使用被轉推者的帳號名稱。 - 在
create_using參數中指定為有向圖,使用nx.DiGraph()。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import networkx
import ____ as ____
# Create retweet network from edgelist
G_rt = nx.from_pandas_edgelist(
sotu_retweets,
source = ____,
target = ____,
create_using = ____)
# Print the number of nodes
print('Nodes in RT network:', len(G_rt.nodes()))
# Print the number of edges
print('Edges in RT network:', len(G_rt.edges()))