Coder Social home page Coder Social logo

communitydetection's Introduction

CommunityDetection

Python field
△.这些内容是我研究社区发现时的一些实验代码,主要基于networkx*部分代码借鉴了网友的分享,已给出参考来源。
△.将这些代码分享出来,一是为了给刚刚进行复杂网络社区发现相关研究的朋友带来一些便利,二是以备日后不时之需。
△.受限于学术水平与编程能力,若代码有错误或不足之处,欢迎朋友们的指正!

使用示例

以美国大学橄榄球联盟的比赛数据集(football)为例,将该网络划分为12个社区,并可视化

from algorithm import SpectralClustering
from matplotlib import pyplot as plt
import networkx as nx

filepath = r'./data/football.gml'
G = nx.read_gml(filepath)

# 获取社区划分
k = 12
sc_com = algorithm.SpectralClustering.partition(G, k)  # 谱聚类

# 可视化(原图布局)
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=False, node_size=70, width=0.5, node_color=sc_com)
plt.show()

可得到原图布局的可视化结果,如下图所示
SpectralClustering_ori
上图所展示的是在原Graph的拓扑结构下,利用颜色对不同社区的节点加以区分。但如果想达到同社区节点联系紧密,不同社区节点联系稀疏的效果,则在获取社区划分(以谱聚类结果sc_com为例)后,还需要进行以下操作:

# 获取每个社区所包含的节点
V = [node for node in G.nodes()]
com_dict = {node:com for node, com in zip(V, sc_com)}
com = [[V[i] for i in range(G.number_of_nodes()) if sc_com[i] == j] for j in range(k)]

# 构造可视化所需要的图
G_graph = nx.Graph()
for each in com:
  G_graph.update(nx.subgraph(G, each))
color = [com_dict[node] for node in G_graph.nodes()]

# 可视化(社区布局)
pos = nx.spring_layout(G_graph, seed=4, k=0.33)
nx.draw(G, pos, with_labels=False, node_size=1, width=0.1, alpha=0.2)
nx.draw(G_graph, pos, with_labels=True, node_color=color, node_size=70, width=0.5, font_size=5, font_color='#000000')
plt.show()

结果得到下图
SpectralClustering

需要注意的是,对于GN这类图划分算法,原图$G$在经过社区发现之后其结构已经被破坏,要对此类算法的结果进行可视化,需要先保存原图结构,如下所示

from algorithm import GN
from matplotlib import pyplot as plt
import networkx as nx
import copy

filepath = r'./data/football.gml'
G = nx.read_gml(filepath)
G_copy = copy.deepcopy(G)  # 复制一个图来进行社区发现

# 获取社区划分
gn_com = GN.partition(G_copy)

# 可视化(原图布局)
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=False, node_size=70, width=0.5, node_color=gn_com)
plt.show()

V = [node for node in G.nodes()]
com_dict = {node:com for node, com in zip(V, gn_com)}
k = max(com_dict.values()) + 1
com = [[V[i] for i in range(G.number_of_nodes()) if gn_com[i] == j] for j in range(k)]

# 构造可视化所需要的图
G_graph = nx.Graph()
for each in com:
  G_graph.update(nx.subgraph(G, each))  #
color = [com_dict[node] for node in G_graph.nodes()]

# 可视化(社区布局)
pos = nx.spring_layout(G_graph, seed=4, k=0.33)
nx.draw(G, pos, with_labels=False, node_size=1, width=0.1, alpha=0.2)
nx.draw(G_graph, pos, with_labels=True, node_color=color, node_size=70, width=0.5, font_size=5, font_color='#000000')
plt.show()

内容

社区发现算法

算法名称 参考文献 代码参考链接 相关说明
GN(Girvan&Newman) 《Community structure in social and biological networks》 zzz24512653 -
Spectral Clustering 《A tutorial on spectral clustering》 waleking 推导

社区发现评价指标

1. 模块度(Modularity)

modularity
相关博客: 模块度发展历程

2. 标准化互信息(Normalized Mutual Information(NMI))

NMI代码参考链接:bethansy NMI

communitydetection's People

Contributors

qinyuenlp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

communitydetection's Issues

data issue

求教

一些gml 无法读取,read_gml会报错:
networkx.exception.NetworkXError: node #0 has no 'label' attribute

power,netscience都会报错,这个怎么修改呢?

community包中没有modurity属性

运行GN算法的时候,出现如下报错
Traceback (most recent call last): File "<input>", line 1, in <module> File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/fanjunlei/PycharmProjects/CommunityDetection/demo_GN.py", line 12, in <module> gn_com = GN.partition(G_copy) File "/Users/fanjunlei/PycharmProjects/CommunityDetection/algorithm/GN.py", line 35, in partition modularities = [cm.modularity(results[i], G_copy) for i in range(len(results))] File "/Users/fanjunlei/PycharmProjects/CommunityDetection/algorithm/GN.py", line 35, in <listcomp> modularities = [cm.modularity(results[i], G_copy) for i in range(len(results))] AttributeError: module 'community' has no attribute 'modularity'

尝试换用networkx.algorithm.community还是不行

程序import 错误

Traceback (most recent call last):
  File "d:\OneDrive - Office365\complex network\projects\community_detection_2\demo_SpectralClustering.py", line 2, in <module>
    from algorithm import SpectralClustering
  File "d:\OneDrive - Office365\complex network\projects\community_detection_2\algorithm\SpectralClustering.py", line 22, in <module>
    import numpy as np
  File "D:\Applications\anacoda\lib\site-packages\numpy\__init__.py", line 142, in <module>
    from . import core
  File "D:\Applications\anacoda\lib\site-packages\numpy\core\__init__.py", line 54, in <module>
    raise ImportError(msg)
ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
  1. Check that you expected to use Python3.7 from "D:\Applications\anacoda\python.exe",
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy version "1.18.1" you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.

Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.

我注意到_init_.py中是空的,但是commit comment说是修正了networkx版本不兼容的错误

可视化报错

ValueError: 'c' argument must be a color, a sequence of colors, or a sequence of numbers, not ([0, 1, 2, 3, 0, 3, 2, 0, 0, 0, 3, 4, 5, 2, 5, 2, 0, 6, 5, 7, 6, 0, 0, 0, 4, 1, 5, 6, 4, 7, 7, 5, 2, 1, 5, 7, 5, 1, 5, 2, 3, 0, 5, 5, 8, 1, 9, 2, 8, 9, 4, 0, 3, 9, 5, 7, 6, 8, 6, 6, 2, 5, 6, 6, 2, 6, 8, 9, 0, 4, 6, 5, 3, 9, 3, 8, 6, 0, 0, 7, 7, 3, 3, 9, 3, 5, 8, 6, 9, 1, 4, 8, 8, 0, 7, 6, 6, 6, 3, 5, 2, 7, 3, 1, 0, 1, 2, 3, 0, 1, 9, 0, 8, 6, 9], 0.5996290274077957)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.