社区发现(Community Detection)通常用于对图进行群组划分,将具有特定关联关系的顶点和关系划分到同一个社区中,在风控反欺诈领域,往往会用来进行恶意团伙识别或不同团伙的定性以识别各种类型的团伙。下文主要简单介绍一下network中提供的一些常用的社区发现算法的实现。列表如下:
方法名称 | 用途描述 |
---|---|
kernighan_lin_bisection (G[, partition, ...]) |
使用Kernighan–Lin算法将图分成两部分 |
k_clique_communities (G, k[, cliques]) |
使用渗透法在图中发现k-clique社区 |
greedy_modularity_communities (G[, weight, ...]) |
在图G中使用贪心式最大化模块度的方式发现社区 |
naive_greedy_modularity_communities (G[, ...]) |
在图G中使用贪心式最大化模块度的方式发现社区 |
lukes_partitioning (G, max_size[, ...]) |
使用Lukes算法对一棵加权树进行最优划分 |
asyn_lpa_communities (G[, weight, seed]) |
使用异步的标签传播算法进行社区划分 |
label_propagation_communities (G) |
使用标签传播算法进行社区生成 |
louvain_communities (G[, weight, resolution, ...]) |
使用Louvain算法在图中进行最优社区划分 |
louvain_partitions (G[, weight, resolution, ...]) |
为Louvain算法的每层生成划分 |
asyn_fluidc (G, k[, max_iter, seed]) |
使用Fluid 算法在图中进行社区划分 |
girvan_newman (G[, most_valuable_edge]) |
使用GN算法在图中进行社区划分 |
coverage (G, partition) |
社区度量——返回一个社区的覆盖度 |
modularity (G, communities[, weight, resolution]) |
社区度量——返回某个社区的模块度 |
partition_quality (G, partition) |
返回一个社区的覆盖度和性能 |
performance (G, partition) |
返回一个社区的性能 |
下面代码实战~~~~
import networkx as nx
import networkx.algorithms as algos
import matplotlib.pyplot as plt
#创建karate_club图
G = nx.karate_club_graph()
nx.draw(G, with_labels = True)
#使用kernighan_lin算法进行
algos.community.kernighan_lin_bisection(G)
#result: ({0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 16, 17, 19, 21},
# {8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33})
#进行k_clique划分, k = 4
list(algos.community.k_clique_communities(G, 4))
#result : [frozenset({0, 1, 2, 3, 7, 13}),
# frozenset({8, 30, 32, 33}),
# frozenset({23, 29, 32, 33})]
#进行k_clique划分, k = 5
list(algos.community.k_clique_communities(G, 5))
#result: [frozenset({0, 1, 2, 3, 7, 13})]
#使用greedy_modularity_communities进行社区发现,返回结果根据community大小倒序
list(algos.community.greedy_modularity_communities(G))
#result : [frozenset({8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}),
# frozenset({1, 2, 3, 7, 9, 12, 13, 17, 21}),
# frozenset({0, 4, 5, 6, 10, 11, 16, 19})]
#使用naive_greedy_modularity_communities进行社区发现,返回结果根据community大小倒序
list(algos.community.naive_greedy_modularity_communities(G))
#result : [frozenset({8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}),
# frozenset({1, 2, 3, 7, 9, 12, 13, 17, 21}),
# frozenset({0, 4, 5, 6, 10, 11, 16, 19})]
#使用asyn_lpa_communities和label_propagation_communities进行社区发现
list(algos.community.asyn_lpa_communities(G))
#result : [{0, 1, 11, 17, 19, 21},
# {2, 3, 7, 9, 12, 13},
# {4, 10},
# {5, 6, 16},
# {8, 14, 15, 18, 20, 22, 23, 26, 27, 29, 30, 32, 33},
# {24, 25, 28, 31}]
list(algos.community.label_propagation_communities(G))
#result : [{0, 1, 3, 4, 7, 10, 11, 12, 13, 17, 19, 21, 24, 25, 31},
# {2, 8, 9, 14, 15, 18, 20, 22, 23, 26, 27, 28, 29, 30, 32, 33},
# {5, 6, 16}]
#使用louvain_communities进行社区发现
list(algos.community.louvain_communities(G))
#result : [{0, 1, 2, 3, 7, 11, 12, 13, 17, 19, 21},
# {4, 5, 6, 10, 16},
# {8, 9, 14, 15, 18, 20, 22, 23, 26, 27, 29, 30, 32, 33},
# {24, 25, 28, 31}]
#使用louvain_communities进行社区发现,调整seed
list(algos.community.louvain_communities(G, seed=123))
#result : [{1, 2, 3, 7, 12, 13},
# {0, 4, 5, 6, 10, 11, 16, 17, 19, 21},
# {23, 24, 25, 27, 28, 31},
# {8, 9, 14, 15, 18, 20, 22, 26, 29, 30, 32, 33}]
#使用asyn_fluidc进行社区发现
list(algos.community.asyn_fluidc(G, 4))
#result : [{0, 1, 2, 3, 7, 9, 12, 13, 17, 19, 21},
# {4, 5, 6, 10, 16},
# {8, 14, 15, 18, 20, 22, 23, 26, 27, 29, 30, 32, 33},
# {11, 24, 25, 28, 31}]
#使用asyn_fluidc进行社区发现,调整k
list(algos.community.asyn_fluidc(G, 5))
#result : [{0, 1, 7, 17, 19, 21},
# {8, 14, 15, 18, 20, 22, 23, 26, 29, 30, 32, 33},
# {4, 5, 6, 10, 16},
# {2, 9, 24, 25, 27, 28, 31},
# {3, 11, 12, 13}]
常用的社区发现算法的简单用法就简单介绍到这里~~~