# -*- coding: utf-8 -*-
"""
This module implements community detection.
"""
from __future__ import print_function
import array
import random
import networkx as nx
from community_status import Status
__author__ = """Thomas Aynaud (thomas.aynaud@lip6.fr)"""
# Copyright (C) 2009 by
# Thomas Aynaud <thomas.aynaud@lip6.fr>
# All rights reserved.
# BSD license.
__PASS_MAX = -1
__MIN = 0.0000001
def partition_at_level(dendrogram, level):
"""Return the partition of the nodes at the given level
A dendrogram is a tree and each level is a partition of the graph nodes.
Level 0 is the first partition, which contains the smallest communities,
and the best is len(dendrogram) - 1.
The higher the level is, the bigger are the communities
Parameters
----------
dendrogram : list of dict
a list of partitions, ie dictionnaries where keys of the i+1 are the
values of the i.
level : int
the level which belongs to [0..len(dendrogram)-1]
Returns
-------
partition : dictionnary
A dictionary where keys are the nodes and the values are the set it
belongs to
Raises
------
KeyError
If the dendrogram is not well formed or the level is too high
See Also
--------
best_partition which directly combines partition_at_level and
generate_dendrogram to obtain the partition of highest modularity
Examples
--------
>>> G=nx.erdos_renyi_graph(100, 0.01)
>>> dendrogram = generate_dendrogram(G)
>>> for level in range(len(dendrogram) - 1) :
>>> print("partition at level", level, "is", partition_at_level(dendrogram, level)) # NOQA
"""
partition = dendrogram[0].copy()
for index in range(1, level + 1):
for node, community in partition.items():
partition[node] = dendrogram[index][community]
return partition
def modularity(partition, graph, weight='weight'):
"""Compute the modularity of a partition of a graph
Parameters
----------
partition : dict
the partition of the nodes, i.e a dictionary where keys are their nodes
and values the communities
graph : networkx.Graph
the networkx graph which is decomposed
weight : str, optional
the key in graph to use as weight. Default to 'weight'
Returns
-------
modularity : float
The modularity
Raises
------
KeyError
If the partition is not a partition of all graph nodes
ValueError
If the graph has no link
TypeError
If graph is not a networkx.Graph
References
----------
.. 1. Newman, M.E.J. & Girvan, M. Finding and evaluating community
structure in networks. Physical Review E 69, 26113(2004).
Examples
--------
>>> G=nx.erdos_renyi_graph(100, 0.01)
>>> part = best_partition(G)
>>> modularity(part, G)
"""
if graph.is_directed():
raise TypeError("Bad graph type, use only non directed graph")
inc = dict([])
deg = dict([])
links = graph.size(weight=weight)
if links == 0:
raise ValueError("A graph without link has an undefined modularity")
for node in graph:
com = partition[node]
deg[com] = deg.get(com, 0.) + graph.degree(node, weight=weight)
for neighbor, datas in graph[node].items():
edge_weight = datas.get(weight, 1)
if partition[neighbor] == com:
if neighbor == node:
inc[com] = inc.get(com, 0.) + float(edge_weight)
else:
inc[com] = inc.get(com, 0.) + float(edge_weight) / 2.
res = 0.
for com in set(partition.values()):
res += (inc.get(com, 0.) / links) - \
(deg.get(com, 0.) / (2. * links)) ** 2
return res
def best_partition(graph, partition=None,
weight='weight', resolution=1., randomize=False):
"""Compute the partition of the graph nodes which maximises the modularity
(or try..) using the Louvain heuristices
This is the partition of highest modularity, i.e. the highest partition
of the dendrogram generated by the Louvain algorithm.
Parameters
----------
graph : networkx.Graph
the networkx graph which is decomposed
partition : dict, optional
the algorithm will start using this partition of the nodes.
It's a dictionary where keys are their nodes and values the communities
weight : str, optional
the key in graph to use as weight. Default to 'weight'
resolution : double, optional
Will change the size of the communities, default to 1.
represents the time described in
"Laplacian Dynamics and Multiscale Modular Structure in Networks",
R. Lambiotte, J.-C. Delvenne, M. Barahona
randomize : boolean, optional
Will randomize the node evaluation order and the community evaluation
order to get different partitions at each call
Returns
-------
partition : dictionnary
The partition, with communities numbered from 0 to number of communities
Raises
------
NetworkXError
If the graph is not Eulerian.
See Also
--------
generate_dendrogram to obtain all the decompositions levels
Notes
-----
Uses Louvain algorithm
References
----------
.. 1. Blondel, V.D. et al. Fast unfolding of communities in
large networks. J. Stat. Mech 10008, 1-12(2008).
Examples
--------
>>> #Basic usage
>>> G=nx.erdos_renyi_graph(100, 0.01)
>>> part = best_partition(G)
>>> #other example to display a graph with its community :
>>> #better with karate_graph() as defined in networkx examples
>>> #erdos renyi don't have true community structure
>>> G = nx.erdos_renyi_graph(30, 0.05)
>>> #first compute the best partition
>>> partition = community.best_partition(G)
>>> #drawing
>>> size = float(len(set(partition.values())))
>>> pos = nx.spring_layout(G)
>>> count = 0.
>>> for com in set(partition.values()) :
>>> count += 1.
>>> list_nodes = [nodes for nodes in partition.keys()
>>> if partition[nodes] == com]
>>> nx.draw_networkx_nodes(G, pos, list_nodes, node_size = 20,
node_color = str(count / size))
>>> nx.draw_networkx_edges(G, pos, alpha=0.5)
>>> plt.show()
"""
dendo = generate_dendrogram(graph,
partition,
weight,
resolution,
randomize)
return partition_at_level(dendo, len(dendo) - 1)
def generate_dendrogram(graph,
part_init=None,
weight='weight',
resolution=1.,
randomize=False):
"""Find communities in the graph and return the associated dendrogram
A dendrogram is a tree and each level is a partition of the graph nodes.
Level 0 is the first partition, which contains the smallest communities,
and the best is len(dendrogram) - 1. The higher the level is, the bigger
are the communities
Parameters
----------
graph : networkx.Graph
the networkx graph which will be decomposed
part_init : dict, optional
the algorithm will start using this partition of the nodes. It's a
dictionary where keys are their nodes and values the communities
weight : str, optional
the key in graph to use as weight. Default to 'weight'
resolution : double, optional
Will change the size of the communities, default to 1.
represents the time described in
"Laplacian Dynamics and Multiscale Modular Structure in Networks",
R. Lambiotte, J.-C. Delvenne, M. Barahona
Returns
-------
dendrogram : list of dictionaries
a list of partitions, ie di
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于python实现分析爬取的中国电影票房数据并可视化源码.zip 基于python实现分析爬取的中国电影票房数据并可视化源码.zip 基于python实现分析爬取的中国电影票房数据并可视化源码.zip 利用python分析爬取的中国电影票房数据并聚类和可视化分析 关键词 关键词:大数据可视化 聚类分析 基于python实现分析爬取的中国电影票房数据并可视化源码.zip 利用python分析爬取的中国电影票房数据并聚类和可视化分析 关键词 关键词:大数据可视化 聚类分析 基于python实现分析爬取的中国电影票房数据并可视化源码.zip 利用python分析爬取的中国电影票房数据并聚类和可视化分析 关键词 关键词:大数据可视化 聚类分析
资源推荐
资源详情
资源评论
收起资源包目录
基于python实现分析爬取的中国电影票房数据并可视化源码.zip (12个子文件)
说明介绍.md 149B
mysql_actor_type.py 6KB
cooperation_actor.py 5KB
k_keams_actortype.py 1KB
lesmiserables.gml 17KB
community_status.py 3KB
reference_keams.py 2KB
SSE_k.py 1KB
s.py 1KB
ssss.py 1KB
community_louvain.py 16KB
PyClustering.py 2KB
共 12 条
- 1
onnx
- 粉丝: 9629
- 资源: 5597
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页