1.提交。
This commit is contained in:
parent
eac90241ef
commit
f2f8aefd71
38
web/service/louvain_service.py
Normal file
38
web/service/louvain_service.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import community as community_louvain
|
||||||
|
import networkx as nx
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from web.manager.log_manager import LogManager
|
||||||
|
from web.service.base_service import BaseService
|
||||||
|
|
||||||
|
Logger = LogManager.get_logger(__name__)
|
||||||
|
|
||||||
|
"""
|
||||||
|
Louvain算法
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class LouvainService(BaseService):
|
||||||
|
|
||||||
|
def test(self):
|
||||||
|
"""
|
||||||
|
Louvain算法
|
||||||
|
"""
|
||||||
|
|
||||||
|
Logger.info("Louvain算法")
|
||||||
|
|
||||||
|
# 加载karate俱乐部图
|
||||||
|
G = nx.karate_club_graph()
|
||||||
|
|
||||||
|
# 计算最优分区
|
||||||
|
partition = community_louvain.best_partition(G)
|
||||||
|
|
||||||
|
# 绘制结果(可选)
|
||||||
|
pos = nx.spring_layout(G)
|
||||||
|
plt.figure(figsize=(6, 6))
|
||||||
|
nx.draw_networkx_nodes(G, pos, cmap=plt.cm.coolwarm, 电压=partition.values())
|
||||||
|
nx.draw_networkx_edges(G, pos, alpha=0.5)
|
||||||
|
plt.title('Louvain communities')
|
||||||
|
plt.show()
|
Loading…
Reference in New Issue
Block a user