##单机版Python##社团划分——有向图的Label Propagation算法

2024-05-07 14:58

本文主要是介绍##单机版Python##社团划分——有向图的Label Propagation算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在博文社区划分——Label Propagation中,介绍了Label Propagation社区划分算法的基本原理,基本的Label Propagation算法是针对无向图的社区划分算法。

一、基本Label Propagation算法原理

对于网络中的每一个节点,在初始阶段,Label Propagation算法对每一个节点一个唯一的标签,在每一个迭代的过程中,每一个节点根据与其相连的节点所属的标签改变自己的标签,更改的原则是选择与其相连的节点中所属标签最多的社区标签为自己的社区标签,这便是标签传播的含义。随着社区标签的不断传播,最终紧密连接的节点将有共同的标签。

其中,标签的异步更新方式如下:

                                       Cx(t)=f(Cxi1(t),,Cxim(t),Cxi(m+1)(t1),,     Cxik(t1))

Label Propagation算法的过程如下:

  • 对网络中的每一节点初始化其所属社区标签,如对于节点x,初始化其社区标签为Cx(0)=x
  • 设置代数t
  • 对于网络中的节点设置其遍历顺序和节点的集合X
  • 对于每一个节点xX,令Cx(t)=f(Cxi1(t),,Cxim(t),Cxi(m+1)(t1),,Cxik(t1))
  • 判断是否可以迭代结束,如果否,则设置t=t+1,重新遍历。

二、有向图的Label Propagation算法

1、有向图

有向图是指图中的边是带有方向的图。对于有向图,每两个节点之间的边的条数是两条,分别为流出的边和流入的边,其流出边的总数为出度,流入边的总数为入度,如下图的有向图:

这里写图片描述
(图片来自百度百科)

对于节点5,其出度为2,入度也为2。对于更多的有向图的知识,可参阅相关图论的书。

2、对于Label Propagation算法的修正

要使得Label Propagation算法能够求解有向图的社区划分,问题即变为如何将有向图转换成无向图。即如何定义有向图中两个节点之间的边的权重。对于这个问题,设计了如下的公式:

wi,j=αλi,j+βλj,i

其中wi,j表示的是节点j对于节点i的权重,λi,j表示的是节点i到节点j的权重,λj,i表示的是节点j到节点i的权重。通过参数α和参数β可以调节不同的权重比例。

通过如上的办法将有向图的Label Propagation算法转换成无向图的Label Propagation算法进行求解。

三、实验

对于如下的数据:

0   2   1
2   0   2
0   3   2
3   0   1
0   4   3
4   0   1
0   5   2
5   0   1
1   2   3
2   1   1
1   4   5
4   1   2
1   7   1
7   1   4
2   4   2
4   2   2
2   5   9
5   2   7
2   6   1
6   2   4
3   7   1
7   3   5
4   10  1
10  4   4
5   7   1
7   5   2
5   11  1
11  5   2
6   7   3
7   6   7
6   11  5
11  6   2
8   9   1
9   8   6
8   10  4
10  8   2
8   11  2
11  8   1
8   14  5
14  8   3
8   15  8
15  8   5
9   12  2
12  9   1
9   14  1
14  9   2
10  11  10
11  10  1
10  12  2
12  10  3
10  13  9
13  10  8
10  14  8
14  10  7
11  13  1
13  11  4

程序源码如下:

#####################################
# Author:zhaozhiyong
# Date:20160602
# Fun:Label Propagation
#####################################
import stringdef loadData(filePath):f = open(filePath)vector_dict = {}edge_dict_out = {}#outedge_dict_in = {}#infor line in f.readlines():lines = line.strip().split("\t")if lines[0] not in vector_dict:vector_dict[lines[0]] = string.atoi(lines[0])if lines[1] not in vector_dict:vector_dict[lines[1]] = string.atoi(lines[1])if lines[0] not in edge_dict_out:edge_list = []if len(lines) == 3:edge_list.append(lines[1] + ":" + lines[2])edge_dict_out[lines[0]] = edge_listelse:edge_list = edge_dict_out[lines[0]]if len(lines) == 3:edge_list.append(lines[1] + ":" + lines[2])edge_dict_out[lines[0]] = edge_listif lines[1] not in edge_dict_in:edge_list = []if len(lines) == 3:edge_list.append(lines[0] + ":" + lines[2])edge_dict_in[lines[1]] = edge_listelse:edge_list = edge_dict_in[lines[1]]if len(lines) == 3:edge_list.append(lines[0] + ":" + lines[2])edge_dict_in[lines[1]] = edge_listf.close()return vector_dict, edge_dict_out, edge_dict_indef get_max_community_label(vector_dict, adjacency_node_list):label_dict = {}# generate the label_dictfor node in adjacency_node_list:node_id_weight = node.strip().split(":")node_id = node_id_weight[0]node_weight = float(node_id_weight[1])if vector_dict[node_id] not in label_dict:label_dict[vector_dict[node_id]] = node_weightelse:label_dict[vector_dict[node_id]] += node_weight# find the max labelsort_list = sorted(label_dict.items(), key = lambda d: d[1], reverse=True)return sort_list[0][0]def check(vector_dict, edge_dict):#for every nodefor node in vector_dict.keys():adjacency_node_list = edge_dict[node]node_label = vector_dict[node]#suject to label_check = {}for ad_node in adjacency_node_list:node_id_weight = ad_node.strip().split(":")node_id = node_id_weight[0]node_weight = node_id_weight[1]if vector_dict[node_id] not in label_check:label_check[vector_dict[node_id]] = float(node_weight)else:label_check[vector_dict[node_id]] += float(node_weight)#print label_checksort_list = sorted(label_check.items(), key = lambda d: d[1], reverse=True)if node_label == sort_list[0][0]:continueelse:return 0return 1    def label_propagation(vector_dict, edge_dict_out, edge_dict_in):#rebuild edge_dictedge_dict = {}for node in vector_dict.iterkeys():out_list = edge_dict_out[node]in_list = edge_dict_in[node]#print "node:", node#print "out_list:", out_list#print "in_list:", in_list#print "------------------------------------------------"out_dict = {}for out_x in out_list:out_xs = out_x.strip().split(":")if out_xs[0] not in out_dict:out_dict[out_xs[0]] = float(out_xs[1])in_dict = {}for in_x in in_list:in_xs = in_x.strip().split(":")if in_xs[0] not in in_dict:in_dict[in_xs[0]] = float(in_xs[1])#print "out_dict:", out_dict#print "in_dict:", in_dictlast_list = []for x in out_dict.iterkeys():out_x = out_dict[x]in_x = 0.0if x in in_dict:in_x = in_dict.pop(x)result = out_x + 0.5 * in_xlast_list.append(x + ":" + str(result))if not in_dict:for x in in_dict.iterkeys():in_x = in_dict[x]result = 0.5 * in_xlast_list.append(x + ":" + str(result))#print "last_list:", last_listif node not in edge_dict:edge_dict[node] = last_list#initial, let every vector belongs to a communityt = 0#for every node in a random orderwhile True:if (check(vector_dict, edge_dict) == 0):t = t+1print "----------------------------------------"print "iteration: ", tfor node in vector_dict.keys():adjacency_node_list = edge_dict[node]vector_dict[node] = get_max_community_label(vector_dict, adjacency_node_list)print vector_dictelse:breakreturn vector_dictif __name__ == "__main__":vector_dict, edge_dict_out, edge_dict_in = loadData("./cd_data.txt")print vector_dictprint edge_dict_outprint edge_dict_in#print "original community: ", vector_dictvec_new = label_propagation(vector_dict, edge_dict_out, edge_dict_in)print "---------------------------------------------------------"print "the final result: "for key in vec_new.keys():print str(key) + " ---> " + str(vec_new[key])

最终的结果:

这里写图片描述

程序和数据的github地址

这篇关于##单机版Python##社团划分——有向图的Label Propagation算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/967676

相关文章

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文