91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

基于Python3的社區(qū)發(fā)現(xiàn)算法fast_unfolding,已經(jīng)對其中的bug進行修改

資源截圖

代碼片段和文件信息

‘‘‘
第一階段稱為Modularity?Optimization,主要是將每個節(jié)點劃分到與其鄰接的節(jié)點所在的社區(qū)中,以使得模塊度的值不斷變大;
第二階段稱為Community?Aggregation,主要是將第一步劃分出來的社區(qū)聚合成為一個點,即根據(jù)上一步生成的社區(qū)結(jié)構(gòu)重新構(gòu)造網(wǎng)絡(luò)。
重復(fù)以上的過程,直到網(wǎng)絡(luò)中的結(jié)構(gòu)不再改變?yōu)橹埂?br/>因為要帶權(quán)重所以讀數(shù)據(jù)才那么慢?
‘‘‘
import?numpy?as?np

class?FastUnfolding:
????‘‘‘
????????從一個csv文件路徑中創(chuàng)建一個圖.
????????path:?文件中包含?“node_from?node_to“?edges?(一對一行)
????‘‘‘
????#classmethod?修飾符對應(yīng)的函數(shù)不需要實例化,不需要?self?參數(shù),
????#?但第一個參數(shù)需要是表示自身類的?cls?參數(shù),可以來調(diào)用類的屬性,類的方法,實例化對象等

????@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))
????????#?用連續(xù)的點重編碼圖中的點
????????nodes_?edges_d?=?in_order(nodes?edges)
????????return?cls(nodes_?edges_)d

????‘‘‘
????????從一個txt文件路徑中創(chuàng)建一個圖.
????????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#生成一個字典,記錄原始圖中出現(xiàn)的點
????????????nodes[n[1]]?=?1
????????????w?=?1
????????????if?len(n)?==?3:#有權(quán)重是權(quán)重,沒權(quán)重是1
????????????????w?=?int(n[2])
????????????edges.append(((n[0]?n[1])?w))
????????#?用連續(xù)的點重編碼圖中的點
????????nodes_?edges_d?=?in_order(nodes?edges)
????????print(“%d?nodes?%d?edges“?%?(len(nodes_)?len(edges_)))
????????return?cls(nodes_?edges_)d


????‘‘‘
????????從一個gml文件路徑中創(chuàng)建一個圖.

????‘‘‘

????@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)

評論

共有 條評論