Python 網路分析
網路是由節點和邊組成的集合,這些節點和邊表示這些節點之間的關係或連線。節點可以表示各種實體,例如個人、組織、基因或網站,而邊則表示它們之間的連線或互動。
網路分析是對這些實體之間關係的研究,節點表示為網路。在這篇文章中,我們將瞭解如何使用 python 實現網路分析。它涉及使用許多數學、統計和計算技術。網路分析可以提供對複雜系統行為的見解,並幫助在各個領域做出明智的決策。
Python 為我們提供了一個名為 networkx 的包,它對建立、操作和分析複雜網路非常有幫助。在繼續本文之前,我們將使用以下命令在終端中安裝 networkx 用於 python 中的網路分析
pip install networkx
建立簡單圖
在這裡,我們將使用 networkx 庫建立一個簡單圖。它將包含 3 個節點和 3 條邊。然後,我們將計算每個節點的度數和聚類係數,最後繪製圖形。
節點的度數:節點在圖中擁有的鄰居數。可以透過計算連線到節點的邊的數量來計算。
聚類係數:衡量節點的鄰居彼此連線程度的指標。換句話說,它是衡量圖中圍繞特定節點的區域性連線密度的指標。它是透過將節點的鄰居之間存在的邊的數量除以此類邊的最大可能數量來計算的。
示例
import networkx as nx
import matplotlib.pyplot as plt
# create an empty graph
G = nx.Graph()
# add nodes
G.add_node(1)
G.add_node(2)
G.add_node(3)
# add edges
G.add_edge(1, 2)
G.add_edge(2, 3)
G.add_edge(3, 1)
print("Node degree:")
for node in G.nodes():
print(f"{node}: {G.degree(node)}")
# clustering coefficient
print("Node clustering coefficient:")
for node in G.nodes():
print(f"{node}: {nx.clustering(G, node)}")
# draw the graph
nx.draw(G, with_labels=True)
plt.show()
輸出
Node degree: 1: 2 2: 2 3: 2 Node clustering coefficient: 1: 1.0 2: 1.0 3: 1.0
識別社群
識別圖中的社群是根據相似特徵將圖的節點劃分為組或叢集的過程。為此,使用 Louvain 演算法。它是一種迭代演算法,透過最佳化衡量給定社群結構模組性的質量函式來工作。模組性衡量社群內邊的數量與隨機圖中預期邊的數量相比的高低程度。
Louvain 演算法分兩個階段工作,分別是
該演算法將每個節點分配到自己的社群。然後迭代地在社群之間移動節點,以便可以提高模組性。此過程不斷重複,直到沒有改進模組性的空間。
然後,該演算法構建新的圖,每個節點代表第一階段中的一個社群。邊表示社群之間邊的總權重。最後,將第一階段應用於此新圖,以幫助識別更粗粒度的社群。
Louvain 演算法非常高效,可用於檢測具有數百萬個節點和邊的龐大圖中的社群。
示例
import networkx as nx
import matplotlib.pyplot as plt
G = nx.gnm_random_graph(7,10)
# draw the graph
print("Original graph:")
nx.draw(G,with_labels=True)
plt.show()
print("Node degree:")
for node in G.nodes():
print(f"{node}: {G.degree(node)}")
print("Node betweenness centrality:")
bc = nx.betweenness_centrality(G)
for node in bc:
print(f"{node}: {bc[node]}")
# community identification using Louvain algorithm
communities = nx.algorithms.community.modularity_max.greedy_modularity_communities(G)
# print the communities and the number of nodes in each community
i = 1
for c in communities:
print(f"Community {i}: {c}")
i += 1
print(f"Number of nodes: {len(c)}")
color_map = []
for node in G.nodes():
for i in range(len(communities)):
if node in communities[i]:
color_map.append(i)
print("Graph with communities marked:")
nx.draw(G, node_color=color_map, with_labels=True)
plt.show()
輸出
Node degree:
0: 5
1: 3
2: 2
3: 2
4: 2
5: 4
6: 2
Node betweenness centrality:
0: 0.5666666666666667
1: 0.1
2: 0.0
3: 0.0
4: 0.0
5: 0.2
6: 0.0
Community 1: frozenset({0, 2, 3, 4})
Number of nodes: 4
Community 2: frozenset({1, 5, 6})
Number of nodes: 3
分析同質性
同質性是個人與擁有與自己相似特徵或傾向的其他個人(如信仰、價值觀或人口統計資訊,如年齡、性別、種族等)交往的趨勢。
這是一個有據可查的社會現象,有助於網路分析。
我們將研究同質性在塑造網路結構中的作用,包括相似節點彼此連線的趨勢。
圖的同質性係數衡量這種相似節點連線的趨勢,它是藉助 nx.attribute_assortativity_coefficient() 函式計算的,範圍從 -1 到 1。正值表示可能性更大,而負值表示可能性更小。
在下面的程式碼中,我們不僅計算了同質性係數,還透過為每個節點分配一個二元屬性“型別”來標記節點,以指示它屬於 A 組還是 B 組。我們還根據其型別繪製了節點顏色的圖形,以視覺化任何同質性模式。
示例
import networkx as nx
import matplotlib.pyplot as plt
G = nx.gnm_random_graph(50, 100)
# binary attributes for each node for indication of it’s type
for node in G.nodes():
G.nodes[node]['type'] = 'A' if node < 25 else 'B'
# draw the graph and colour the nodes with their corresponding types A or B
color_map = ['red' if G.nodes[node]['type'] == 'A' else 'blue' for node in G.nodes()]
nx.draw(G, node_color=color_map, with_labels=True)
plt.show()
homophily_coeff = nx.attribute_assortativity_coefficient(G, 'type')
print(f"Homophily coefficient: {homophily_coeff}")
輸出
Homophily coefficient: -0.0843989769820972
結論
網路分析對於研究複雜系統的結構和動態(包括社交網路)非常有用。Python 提供了各種庫來做到這一點,但是 networkx 是最常用的庫。透過使用 python 中的網路分析,研究人員和分析師可以回答各種研究問題,例如識別關鍵節點和社群、衡量網路的穩健性和彈性、檢測同質性和社會影響的模式。雖然網路分析可能是一個複雜的專業領域,但如果仔細關注資料準備和清理,它可以幫助解決大量現實世界的問題,並幫助企業發展。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP