-
大小: 14KB文件類型: .py金幣: 1下載: 0 次發布日期: 2021-06-01
- 語言: Python
- 標簽: 社區發現??fast_unfoldi??
資源簡介
基于Python3的社區發現算法fast_unfolding,已經對其中的bug進行修改
代碼片段和文件信息
‘‘‘
第一階段稱為Modularity?Optimization,主要是將每個節點劃分到與其鄰接的節點所在的社區中,以使得模塊度的值不斷變大;
第二階段稱為Community?Aggregation,主要是將第一步劃分出來的社區聚合成為一個點,即根據上一步生成的社區結構重新構造網絡。
重復以上的過程,直到網絡中的結構不再改變為止。
因為要帶權重所以讀數據才那么慢?
‘‘‘
import?numpy?as?np
class?FastUnfolding:
????‘‘‘
????????從一個csv文件路徑中創建一個圖.
????????path:?文件中包含?“node_from?node_to“?edges?(一對一行)
????‘‘‘
????#classmethod?修飾符對應的函數不需要實例化,不需要?self?參數,
????#?但第一個參數需要是表示自身類的?cls?參數,可以來調用類的屬性,類的方法,實例化對象等
????@classmethod
????def?from_csv(cls?data):
????????dataarray?=?np.asarray(data)
????????nodes?=?{}
????????edges?=?[]
????????for?n?in?dataarray:
????????????nodes[n[0]]?=?1
????????????nodes[n[1]]?=?1
????????????w?=?1
????????????if?len(n)?==?3:
????????????????w?=?int(n[2])
????????????edges.append(((n[0]?n[1])?w))
????????#?用連續的點重編碼圖中的點
????????nodes_?edges_d?=?in_order(nodes?edges)
????????return?cls(nodes_?edges_)d
????‘‘‘
????????從一個txt文件路徑中創建一個圖.
????????path:?文件中包含?“node_from?node_to“?edges?(一對一行)
????‘‘‘
????@classmethod
????def?from_file(cls?path):
????????f?=?open(path?‘r‘)
????????lines?=?f.readlines()
????????f.close()
????????nodes?=?{}
????????edges?=?[]
????????for?line?in?lines:
????????????n?=?line.split()
????????????if?not?n:
????????????????break
????????????nodes[n[0]]?=?1#生成一個字典,記錄原始圖中出現的點
????????????nodes[n[1]]?=?1
????????????w?=?1
????????????if?len(n)?==?3:#有權重是權重,沒權重是1
????????????????w?=?int(n[2])
????????????edges.append(((n[0]?n[1])?w))
????????#?用連續的點重編碼圖中的點
????????nodes_?edges_d?=?in_order(nodes?edges)
????????print(“%d?nodes?%d?edges“?%?(len(nodes_)?len(edges_)))
????????return?cls(nodes_?edges_)d
????‘‘‘
????????從一個gml文件路徑中創建一個圖.
????‘‘‘
????@classmethod
????def?from_gml_file(cls?path):
????????f?=?open(path?‘r‘)
????????lines?=?f.readlines()
????????f.close()
????????nodes?=?{}
????????edges?=?[]
????????current_edge?=?(-1?-1?1)
????????#?dic?={}
????????in_edge?=?0
????????for?line?in?lines:
????????????words?=?line.split()
????????????if?not?words:
????????????????break
????????????if?words[0]?==?‘id‘:
????????????????#?a?=?int(words[1])
????????????????nodes[int(words[1])]?=?1
????????????#?if?words[0]?==?‘label‘:
????????????#?????dic[words[1]]?=?a
????????????elif?words[0]?==?‘source‘:#當讀到source的時候,開始刷新current_edge
????????????????in_edge?=?1
????????????????current_edge?=?(int(words[1])?current_edge[1]?current_edge[2])
????????????elif?words[0]?==?‘target‘?and?in_edge:
????????????????current_edge?=?(current_edge[0]?int(words[1])?current_edge[2])
????????????elif?words[0]?==?‘weight‘?and?in_edge:
????????????????current_edge?=?(current_edge[0]?current_edge[1]?int(words[1]))
????????????elif?words[0]?==?‘]‘?and?in_edge:
????????????????edges.append(((current_edge[0]?current_edge[1])current_edge[2]))
????????????????current_edge?=?(-1?-1?1)
????????????????in_edge?=?0#讀完一個邊,添加到edges中,并刷新current_edge和in_edge
????????nodes?edgesd?=?in_order(nodes?edges)
- 上一篇:python-cwt時頻圖繪制
- 下一篇:fcntl.py模塊
評論
共有 條評論