LFR简单使用入门

来源:互联网 发布:淘宝账号销号 编辑:程序博客网 时间:2024/06/06 06:53

1. 运行环境

LFR程序下载链接, 运行环境为Linux、g++

1.1 先安装build-essential:(可省略)

  1. $ sudo apt-get install build-essential

1.2 查看 gcc 版本

  1. $ gcc --version
  2. gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
  3. Copyright (C) 2011 Free Software Foundation, Inc.
  4. This is free software; see the source for copying conditions. There is NO
  5. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

1.3 安装 统一版本的 g++

  1. $ sudo apt-get install g++

1.4 查看安装版本

  1. $ g++ --version

2. 用LFR生成网络

  • 首先跳转到 LFR 的 binary_network 目录中compile一下。
  1. $ make

这样会生成一个 benchmark 文件,它就是我们构建网络的工具。

  • 使用 benchmark 生成一个网络数据的简单的例子:
  1. $ ./benchmark -f flags.dat -t1 3

这里的 flags.dat 文件在解压缩包后就有的,它表示参数的设置。benchmark工具会根据这个flag.dat文件生成网络数据。如果要生成其他的网络,要修改这个文件的参数。

  1. $ ./benchmark [FLAG] [P]
  2. [FLAG] [P]
  3. -N number of nodes
  4. -k average degree
  5. -maxk maximum degree
  6. -mu mixing parameter
  7. -t1 minus exponent for the degree sequence
  8. -t2 minus exponent for the community size distribution
  9. -minc minimum for the community sizes
  10. -maxc maximum for the community sizes
  11. -on number of overlapping nodes
  12. -om number of memberships of the overlapping nodes
  13. -C [average clustering coefficient]
  • 关于参数mu 
    1-mu是节点与其同一个社区内的节点之间的共享比例,mu是节点与其它社区节点的共享比例。mu设置的小一点,社区内部更加紧凑。

  • 一个flag.dat文件的例子:

  1. -N 2000
  2. -k 16
  3. -maxk 60
  4. -mu 0.3
  5. -t1 2
  6. -t2 1
  7. -minc 32
  8. -maxc 40
  9. -on 0
  10. -om 0

3. 转化为gml文件

network.dat文件是得到的网络结构,每一行表示一条边。文件community.dat是该网络的真实社区结构其每一行表示 (节点、节点所在的社区)。

network.dat 和 community.dat 转化为我们需要的 LFR-1.gml 文件:

  1. #coding=utf-8
  2. import networkx as nx
  3. from collections import defaultdict
  4. def transfer_2_gml():
  5. """--------------------------------------------------------------------------
  6. function: 将LFR的network.dat和community.dat转化为.gml文件
  7. parameter:
  8. return:
  9. -------------------------------------------------------------------------------"""
  10. nodes_labels={}
  11. k=0
  12. with open("community.dat","r") as f:
  13. for line in f.readlines():
  14. items=line.strip("\r\n").split("\t")
  15. v=items[0]
  16. label=items[1]
  17. nodes_labels[v]=label
  18. #end-for
  19. #end-with
  20. G=nx.Graph()
  21. for v in nodes_labels.keys(): #添加所有的节点,并且记录其真实标签
  22. G.add_node(v, value=nodes_labels[v])
  23. edges=set()
  24. with open("network.dat","r") as f:
  25. for line in f.readlines():
  26. items=line.strip("\r\n").split("\t")
  27. a=items[0]
  28. b=items[1]
  29. if (a,b) not in edges or (b,a) not in edges:
  30. edges.add( (a,b) )
  31. #end-for
  32. #end-with
  33. for e in edges:
  34. a,b=e
  35. G.add_edge(a,b,type="Undirected")
  36. nx.write_gml(G,"LFR-1.gml")
  37. print ("transfer LFR(*.dat) data to *.gml")
  38. communities=defaultdict(lambda :[])
  39. for v in nodes_labels.keys():
  40. label=nodes_labels[v]
  41. communities[label].append(v)
  42. for c in communities.keys():
  43. print ("community ", c ,": \n", communities[c], "\n")
  44. def main():
  45. transfer_2_gml()
  46. if __name__ == '__main__':
  47. main()

4. 参考

[1] LFR程序下载

[2] LFR使用示例–github链接

Pre: sklearn决策树及可视化

Next: python旁门左道

原创粉丝点击