Python NetworkX +Gephi利用PageRank和K-Clique分析并可视化社会网络

在柯布伦兹兰道大学社交网络数据集网页下载数据集Physicians,地址:http://konect.uni-koblenz.de/networks/moreno_innovation

数据集简介:This directed network captures innovation spread among 246 physicians in for towns in Illinois, Peoria, Bloomington, Quincy and Galesburg. The data was collected in 1966. A node represents a physician and an edge between two physicians shows that the left physician told that the righ physician is his friend or that he turns to the right physician if he needs advice or is interested in a discussion. There always only exists one edge between two nodes even if more than one of the listed conditions are true.

大概意思是:这是一个包含有246个医师分布在4个城镇的有向社交网络

先导入即将用到的包

import matplotlib.pyplot as plt
import os
import networkx as nx
import pandas as pd
import numpy as np
from networkx.algorithms.community import k_clique_communities

该数据集为有向图,创建一个有向图并导入数据,规范的数据集产生后即可调用Full PageRank算法,设定damping parameter alpha α=0.85

os.chdir('.\\')
filename = 'physician.txt'
G=nx.DiGraph()
with open(filename) as file:
    for line in file:
        head, tail = [int(x) for x in line.split()]
        G.add_edge(head,tail)

pr=nx.pagerank(G,alpha=0.85)

PageRank以字典的形式返回出来,后面可以用NetworkX的绘图工具简单做个可视化

layout = nx.spring_layout(G)
plt.figure(1)
nx.draw(G, pos=layout, node_color='y', node_size=30)

#添加标签
plt.figure(2)
nx.draw(G, pos=layout, node_size=[i * 6000 for i in pr.values()],node_color='g',with_labels=True)

得出两幅图
在这里插入图片描述
在这里插入图片描述
效果不是很好,后面用Gephi进行可视化。现在先调用NetworkX的算法包,使用K-Clique进行社区探测,K-Clique支持无向图,那么把原数据的有向图转变为无向图即可(需要注意的是无向图是不能轻易转化为有向图,而有向图能够转化为无向图)

#from networkx.algorithms.community import k_clique_communities
def find_community(graph,k):
    return list(k_clique_communities(graph,k))

G2 = nx.Graph()
with open(filename) as file:
    for line in file:
        head, tail = [int(x) for x in line.split()]
        G2.add_edge(head,tail)

for k in range(5,10):
        print ("############# k-Clique: %d ################" % k)
        rst_com = find_community(G2,k)
        print ("Count of Community being found:%d" % len(rst_com))
        print(rst_com)

打印的结果为
############# k-Clique: 5 ################
Count of Community being found:7
[frozenset({18, 74, 13, 14, 15}), frozenset({37, 54, 23, 40, 15}), frozenset({20, 117, 21, 25, 95}), frozenset({128, 129, 132, 121, 127}), frozenset({132, 121, 125, 142, 127}), frozenset({194, 179, 197, 168, 203}), frozenset({207, 208, 209, 210, 211, 216, 217, 219, 220, 221, 223, 224, 225, 226, 227, 228})]
############# k-Clique: 6 ################
Count of Community being found:1
[frozenset({225, 228, 208, 209, 210, 216, 217, 220, 221})]
############# k-Clique: 7 ################
Count of Community being found:1
[frozenset({208, 225, 209, 210, 228, 220, 221})]
############# k-Clique: 8 ################
Count of Community being found:0
[]
############# k-Clique: 9 ################
Count of Community being found:0
[]

可以看到当k为5,6,7的时候是能够探测出各个社区的,当k大于7时就找不到符合条件的社区。

针对NetworkX可视化效果不太好,这里使用Gephi进行可视化,当然Gephi有很多功能,也包括PageRank等网络中心度的计算,还有社区探测,聚类系数等等。这里操作步骤:

  1. 设置布局算法Force Atlas(最常见的算法之一)
  2. 计算模块化,以探测社区,解析度设为2.0
  3. 计算PageRank
  4. 根据模块化侦测的社区进行颜色分类
  5. 根据PageRank进行大小的Ranking
    在这里插入图片描述
    这里能够清晰的看出4个城镇的医师都以本城镇的医师网络关系为主,没有一个医师有进行跨城镇交际,粉色城镇的网络最大,绿色城镇的网络最小,且绿色城镇内医师间的交际情况较差。在下表的数据也能支持这个结论
    在这里插入图片描述
  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
抱歉,我并不具备绘制图形的能力,但我可以提供一些思路和工具。 要画人物关系图,首先需要有人物关系数据。可以从小说、电视剧等作品中搜集得到。这里以《人民的名义》为例,假设已经有了以下人物关系数据: ``` 赵德汉,李达康 赵德汉,李书记 李达康,李书记 李达康,白岩松 李书记,吕同芳 吕同芳,侯亮平 侯亮平,高育良 高育良,陆亦可 陆亦可,祁同伟 祁同伟,高小琴 高小琴,孙连城 孙连城,赵立春 ``` 接下来可以使用 Python 处理这些数据,生成 Gephi 软件可以识别的格式。 ```python import networkx as nx # 读取人物关系数据 data = [] with open('data.txt', 'r') as f: for line in f.readlines(): nodes = line.strip().split(',') data.append((nodes[0], nodes[1])) # 创建有向图 G = nx.DiGraph() # 添加人物节点和边 for d in data: G.add_edge(d[0], d[1]) # 输出为 Gephi 格式 nx.write_gexf(G, 'out.gexf') ``` 上述代码使用了 `networkx` 库,可以方便地创建有向图,并将结果输出为 Gephi 格式。要使用 Gephi 软件来绘制图形,可以按照以下步骤: 1. 下载并安装 Gephi 软件; 2. 打开 Gephi 软件,选择“Open a File”菜单,打开上述 Python 代码生成的 `out.gexf` 文件; 3. 在左侧的“Overview”窗格中,可以看到节点和边的信息。可以使用“Layout”菜单来对节点进行布局,如使用“Force Atlas”布局; 4. 在右侧的“Preview”窗格中预览图形,并按需调整颜色、大小等参数; 5. 将结果保存为图片或 PDF 等格式。 希望这些思路和工具能够帮助你绘制出理想的人物关系图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值