簡單的社交網絡
阿新 • • 發佈:2019-05-07
探測 class from .py xlsx klist layout int .fig
1.四個文件
cat A.關系圖.xlsx Source Target Weight A B 6 A C 6 A D 7 A E 7 A F 6 A G 5 A H 5 A I 3 A J 6 cat B.關系圖.xlsx Source Target Weight B C 7 B D 7 B E 5 B J 6 C D 8 cat C.關系圖.xlsx Source Target Weight C B7 C E 8 C A 8 C K 5 C N 5 C D 4 C F 2 C Z 5 C L 5 cat D.關系圖.xlsx Source Target Weight D H 3 D J 4 D K 4 D C 4 D B 6 D A 6 D F 6
2.
import pandas as pd import networkx as nxfrom networkx.algorithms.approximation import clique import matplotlib.pyplot as plt data1 = pd.read_excel("./數據集/3.4.簡單社交網絡/A關系圖.xlsx") data2 = pd.read_excel("./數據集/3.4.簡單社交網絡/B關系圖.xlsx") data3 = pd.read_excel("./數據集/3.4.簡單社交網絡/C關系圖.xlsx") data4 = pd.read_excel("./數據集/3.4.簡單社交網絡/D關系圖.xlsx") data = pd.concat([data1,data2,data3,data4])print (data) # from_pandas_edgelist 函數使用數據集創建邊和頂點 graph = nx.from_pandas_edgelist(data,‘Source‘,‘Target‘,edge_attr=[‘Weight‘]) #頂點 #print(graph.nodes()) #邊 #print(graph.edges()) #頂點權重 #print(graph.degree()) #創建圖像實例 plt.figure(figsize = (20,10)) #節點的顏色由節點的度決定 node_color = [graph.degree[v] for v in graph] #邊的寬度由權重決定 edge_size = [0.2*graph[u][v][‘Weight‘] for u,v in graph.edges()] pos = nx.spring_layout(graph) #畫圖 nx.draw_networkx(graph,pos=pos,with_labels=False,node_color=node_color) plt.show() #社區探測 #help(clique) klist = list(clique.clique_removal(graph)) print(len(klist)) plt.figure(figsize=(20,10)) nx.draw_networkx(graph,pos=pos,nodelist=klist[0],node_color= ‘r‘) nx.draw_networkx(graph,pos=pos,node_list=klist[1],node_color=‘y‘) plt.savefig("./a.jpg")
簡單的社交網絡