首页 > 其他 > 详细

简单的社交网络

时间:2019-05-06 21:32:53      阅读:153      评论:0      收藏:0      [点我收藏+]

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    B    7
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 nx
from 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")

 

简单的社交网络

原文:https://www.cnblogs.com/students/p/10822092.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!