igraph包里面常用的社区检测算法--以模块度为优化目标
igraph包⾥⾯常⽤的社区检测算法--以模块度为优化⽬标        本⽂所罗列的社区检测算法主要是igraph包⾥⾯的社区检测算法及对⽹络进⾏社区划分的应⽤,便于对社区检测⽅向有研究兴趣的⼩伙伴来研究。
1、社区检测算法⽬录:算法后⾯的[]⾥⾯的内容是提出该算法的⼈和年份
#(1) nx.k_clique_communities(G, 3)    [Newman 2005]  应⽤较多,性能⼀般
#(2) fastgreedymunity          [Clauset et al., 2004] (modularity optimization method)  性能相对较好
#(3) edge.betweennessmunity          [Newman and Girvan, 2004]  性能⽐(1)好
#(4) label.propagationmunity        [Raghavan et al., 2007]  和GN算法性能差不多
#(5) multilevelmunity              [Blondel et al., 2008] (the Louvain method)  性能是这⼏个算法⾥⾯最好的
#(6) walktrapmunity          [Pons and Latapy, 2005]
#(7) leading.eigenvectormunity        [Newman, 2006]
#(8) infomapmunity          [Rosvall and Bergstrom, 2008]  性能适⽤于⼤⽹络,对⼩⽹络效果较差
#(9) spinglassmunity          [Reichardt and Bornholdt, 2006]
#(10) optimalmunity              [Brandes et al., 2008]
#(11) GSA                [Karaboga, 2009]
这⾥关于性能的⽐较主要是通过个⼈对⽹络进⾏社区检测过程中得出的个⼈结论,⽽且对于不同规模的⽹络,不同算法的性能是不同的,对其感兴趣的可以⾃⼰去测试⼀下。
2、算法的实际应⽤(本⽂采⽤的测试⽹络主要是karate⽹络,即空⼿道俱乐部⽹络)
(1)K派系算法import networkx as nx
from igraph import*
import matplotlib.pyplot as plt
import igraph as ig
karate=open('C:\\Users\\Lenovo\\Desktop\\')
ad()
G = nx.Graph()
liebiao=dolphins.split("\n") # ⽤回车符分裂字符串
for i in range(0,len(liebiao)-1):
liebiao1=liebiao[i].split()  # ⽤空格符分裂‘0’和‘1’
node1=int(liebiao1[0])  #节点转化为整数
node2=int(liebiao1[1])
G.add_edge(node1,node2)
c=list(nx.k_clique_communities(G, 3)) # k=3
#若只想得到社区划分结果,到这⾥就可以了,后⾯的计算只是为了计算模块度值,其他算法中的步骤类似
membership=[]
for i in range(0,len(G)):
桑塔纳2000配件价格
membership.append(0)
for i in range(0,len(c)):
nodes=c[i]
for j in nodes:
membership[j]=i
origin__edgelist(G)
new_list=[]
for edge_i in range(0,len(origin_edges)):
node_dict=origin_edges[edge_i]
new_list.append([node_dict[0],node_dict[1]])
g1=Graph(new_list)
dularity(g1, membership)
print Q
(2)fastgreedy算法CNM算法
import networkx as nx
import networkx as nx
from igraph import*
import matplotlib.pyplot as plt
karate=open('C:\\Users\\Lenovo\\Desktop\\')
ad()
G = nx.Graph()
liebiao=karate.split("\n")  #⽤回车符分裂字符串
for i in range(0,len(liebiao)-1):
liebiao1=liebiao[i].split()    #⽤空格符分裂0和1
node1=int(liebiao1[0])          #节点转化为整数
node2=int(liebiao1[1])
G.add_edge(node1,node2)
origin__edgelist(G)
new_list=[]
for edge_i in range(0,len(origin_edges)):
node_dict=origin_edges[edge_i]
new_list.append([node_dict[0],node_dict[1]])
g1=Graph(new_list)  #  ⽤变向量列表创建图形
h1=g1munity_fastgreedy(weights=None)
c=list(h1.as_clustering())
membership=[]
for i in range(0,len(G)):
membership.append(0)
#根据社团划分对membership赋值
for i in range(0,len(c)):
nodes=c[i]
for j in nodes:
membership[j]=i
#根据membership计算模块度
dularity(g1,membership)
print Q
(3)GN算法(分列式层次聚类算法)
import networkx as nx
芜湖景点
from igraph import*
import igraph as ig
import matplotlib.pyplot as plt
karate=open('D:\\python\\networks\\')
ad()
G = nx.Graph()
liebiao=dolphins.split("\n") # ⽤回车符分裂字符串
for i in range(0,len(liebiao)-1):
liebiao1=liebiao[i].split()  # ⽤空格符分裂‘0’和‘1’
node1=int(liebiao1[0])  #节点转化为整数
node2=int(liebiao1[1])
G.add_edge(node1,node2)
origin__edgelist(G)
new_list=[]
for edge_i in range(0,len(origin_edges)):
node_dict=origin_edges[edge_i]
new_list.append([node_dict[0],node_dict[1]])
g1=Graph(new_list)  #  ⽤变向量列表创建图形
h1=g1munity_edge_betweenness(clusters=None, directed=False, weights=None) c=list(h1.as_clustering())  # 对系统树图进⾏切割,按照Q值最⼤的标准
医保卡和社保卡是同一张卡吗membership=[]
for i in range(0,len(G)):
for i in range(0,len(G)):
membership.append(0)
for i in range(0,len(c)):
nodes=c[i]
for j in nodes:
membership[j]=i
dularity(g1, membership)
print Q
(4)标签传播label propagation算法
import networkx as nx
import random
import numpy as np
import igraph as ig
from igraph import*
import matplotlib.pyplot as plt
karate=open('D:\\python\\networks\\')
ad()
G = nx.Graph()
liebiao=dolphins.split("\n") # ⽤回车符分裂字符串
for i in range(0,len(liebiao)-1):
liebiao1=liebiao[i].split()  # ⽤空格符分裂‘0’和‘1’
node1=int(liebiao1[0])  #节点转化为整数
node2=int(liebiao1[1])
G.add_edge(node1,node2)
community_list1=Gimunity_label_propagation(weights=None, initial=None, fixed=None)
community_list=[]
for item in community_list1:
community_list.append(item)
membership=[]
for i in range(0,len(G)):
membership.append(0)
for i in range(0,len(community_list)):
nodes=community_list[i]
for j in nodes:
membership[j]=i
Q=dularity
print Q
(5)层次聚类multilevel算法
import networkx as nx
import random
import numpy as np
import igraph as ig
from igraph import*
import matplotlib.pyplot as plt
karate=open('D:\\python\\networks\\')
ad()
G = nx.Graph()
liebiao=dolphins.split("\n") # ⽤回车符分裂字符串
for i in range(0,len(liebiao)-1):
liebiao1=liebiao[i].split()  # ⽤空格符分裂‘0’和‘1’
node1=int(liebiao1[0])  #节点转化为整数
2022年元宵节怎么放假
node2=int(liebiao1[1])
G.add_edge(node1,node2)
Gi_d=ig.Graph.Read_Edgelist("D:\\python\\networks\\")              #基于这些连边使⽤igraph创建⼀个新⽹络
Gi_d=ig.Graph.Read_Edgelist("D:\\python\\networks\\")              #基于这些连边使⽤igraph创建⼀个新⽹络Gi=Gi_d.as_undirected()
community_list1=Gimunity_multilevel(weights=None, return_levels=False)
community_list=[]
for item in community_list1:
community_list.append(item)
membership=[]
for i in range(0,len(G)):
membership.append(0)
for i in range(0,len(community_list)):
nodes=community_list[i]
for j in nodes:
membership[j]=i
Q=dularity
print Q
(6)随机游⾛walktrap算法
import networkx as nx
from igraph import*
import matplotlib.pyplot as plt
import igraph as ig
karate=open('D:\\python\\networks\\')
ad()
G = nx.Graph()
liebiao=dolphins.split("\n") # ⽤回车符分裂字符串
for i in range(0,len(liebiao)-1):
liebiao1=liebiao[i].split()  # ⽤空格符分裂‘0’和‘1’
node1=int(liebiao1[0])  #节点转化为整数
node2=int(liebiao1[1])
G.add_edge(node1,node2)
origin__edgelist(G)
new_list=[]
for edge_i in range(0,len(origin_edges)):
node_dict=origin_edges[edge_i]
new_list.append([node_dict[0],node_dict[1]])
g1=Graph(new_list)  #  ⽤变向量列表创建图形
h1=g1munity_walktrap(weights=None, steps=4)
c=list(h1.as_clustering())  # 对系统树图进⾏切割,按照Q值最⼤的标准
membership=[]
for i in range(0,len(G)):
membership.append(0)
for i in range(0,len(c)):
nodes=c[i]
for j in nodes:
membership[j]=i
dularity(g1, membership)
print Q
(7)拉普拉斯矩阵Leading eigenvector算法
import networkx as nx
import random
import numpy as np
import igraph as ig
from igraph import*
import matplotlib.pyplot as plt
dolphins=open('D:\\python\\networks\\')关于月亮的诗句
dolphins=open('D:\\python\\networks\\')
ad()
G = nx.Graph()
liebiao=dolphins.split("\n") # ⽤回车符分裂字符串
for i in range(0,len(liebiao)-1):
liebiao1=liebiao[i].split()  # ⽤空格符分裂‘0’和‘1’
node1=int(liebiao1[0])  #节点转化为整数
node2=int(liebiao1[1])
G.add_edge(node1,node2)
Gi_d=ig.Graph.Read_Edgelist("D:\\python\\networks\\")    Gi=Gi_d.as_undirected()
community_list1=Gimunity_leading_eigenvector(clusters=None) community_list=[]
for item in community_list1:
community_list.append(item)
membership=[]
for i in range(0,len(G)):
membership.append(0)
for i in range(0,len(community_list)):
nodes=community_list[i]
for j in nodes:
membership[j]=i
Q=dularity
print Q
(8)info map算法
import networkx as nx
import random
import numpy as np
import igraph as ig
from igraph import*
import matplotlib.pyplot as plt
dolphins=open('D:\\python\\networks\\')
ad()
G = nx.Graph()张璇陈思诚
liebiao=dolphins.split("\n") # ⽤回车符分裂字符串
for i in range(0,len(liebiao)-1):
liebiao1=liebiao[i].split()  # ⽤空格符分裂‘0’和‘1’
node1=int(liebiao1[0])  #节点转化为整数
node2=int(liebiao1[1])
G.add_edge(node1,node2)
Gi_d=ig.Graph.Read_Edgelist("D:\\python\\networks\\")      Gi=Gi_d.as_undirected()
community_list1=Gimunity_infomap()
community_list=[]
for item in community_list1:
community_list.append(item)
membership=[]
for i in range(0,len(G)):
membership.append(0)
for i in range(0,len(community_list)):
nodes=community_list[i]
for j in nodes:
membership[j]=i
Q=dularity
print Q

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。