def __init__(self, graph, merges, membership=None, modularity=None, params={}): """Creates a dendrogram object for a given graph. @param graph: the graph that will be associated to the clustering @param merges: the merges performed given in matrix form. @param membership: the membership list. The length of the list must be equal to the number of vertices in the graph. If C{None}, the dendrogram will be cut at the level where the modularity is maximized and the membership list will represent this state. @param modularity: the modularity score of the clustering on each level of the dendrogram starting from the fully decomposed state. If C{None}, it will be calculated. @param params: additional parameters to be stored in this object. """ if modularity is None: # TODO: this is a fairly simple way to calculate the modularity ms = range(graph.vcount()) communities = range(graph.vcount()) modularity = [] n = graph.vcount() for step in xrange(min(n - 1, len(merges))): ms = community_to_membership(merges, graph.vcount(), step) modularity.append(graph.modularity(ms)) if membership is None: maxmod = max(modularity) maxidx = modularity.index(maxmod) membership = community_to_membership(merges, graph.vcount(), maxidx) recoding, n = {}, 0 for idx, m in enumerate(membership): try: membership[idx] = recoding[m] except KeyError: recoding[m], membership[idx] = n, n n += 1 else: maxmod = None Dendrogram.__init__(self, merges) VertexClustering.__init__(self, graph, membership, maxmod, params)
def __init__(self, graph, merges, membership = None, modularity = None, params = {}): """Creates a dendrogram object for a given graph. @param graph: the graph that will be associated to the clustering @param merges: the merges performed given in matrix form. @param membership: the membership list. The length of the list must be equal to the number of vertices in the graph. If C{None}, the dendrogram will be cut at the level where the modularity is maximized and the membership list will represent this state. @param modularity: the modularity score of the clustering on each level of the dendrogram starting from the fully decomposed state. If C{None}, it will be calculated. @param params: additional parameters to be stored in this object. """ if modularity is None: # TODO: this is a fairly simple way to calculate the modularity ms = range(graph.vcount()) communities = range(graph.vcount()) modularity = [] n = graph.vcount() for step in xrange(min(n-1, len(merges))): ms = community_to_membership(merges, graph.vcount(), step) modularity.append(graph.modularity(ms)) if membership is None: maxmod = max(modularity) maxidx = modularity.index(maxmod) membership = community_to_membership(merges, graph.vcount(), maxidx) recoding, n = {}, 0 for idx, m in enumerate(membership): try: membership[idx] = recoding[m] except KeyError: recoding[m], membership[idx] = n, n n += 1 else: maxmod = None Dendrogram.__init__(self, merges) VertexClustering.__init__(self, graph, membership, maxmod, params)
def cut(self, n): """Cuts the dendrogram at a given level. @param n: the desired number of clusters. Merges are replayed from the beginning until the membership vector has exactly M{n} distinct elements or until there are no more recorded merges, whichever happens first. @return: the membership vector """ num_elts = self._graph.vcount() membership = community_to_membership(self._merges, num_elts, num_elts-n) idgen = UniqueIdGenerator() self._membership = [idgen[m] for m in membership] self._len = max(self._membership) + 1 return self._membership[:]
def cut(self, n): """Cuts the dendrogram at a given level. @param n: the desired number of clusters. Merges are replayed from the beginning until the membership vector has exactly M{n} distinct elements or until there are no more recorded merges, whichever happens first. @return: the membership vector """ num_elts = self._graph.vcount() membership = community_to_membership(self._merges, num_elts, num_elts - n) idgen = UniqueIdGenerator() self._membership = [idgen[m] for m in membership] self._len = max(self._membership) + 1 return self._membership[:]
def as_clustering(self, n=None): """Cuts the dendrogram at the given level and returns a corresponding L{VertexClustering} object. @param n: the desired number of clusters. Merges are replayed from the beginning until the membership vector has exactly M{n} distinct elements or until there are no more recorded merges, whichever happens first. If C{None}, the current membership vector will be used. @return: a new L{VertexClustering} object. """ if n is not None: num_elts = self._graph.vcount() membership = community_to_membership(self._merges, num_elts, num_elts-n) idgen = UniqueIdGenerator() membership = [idgen[m] for m in membership] else: membership = self.membership return VertexClustering(self._graph, membership)
def as_clustering(self, n=None): """Cuts the dendrogram at the given level and returns a corresponding L{VertexClustering} object. @param n: the desired number of clusters. Merges are replayed from the beginning until the membership vector has exactly M{n} distinct elements or until there are no more recorded merges, whichever happens first. If C{None}, the current membership vector will be used. @return: a new L{VertexClustering} object. """ if n is not None: num_elts = self._graph.vcount() membership = community_to_membership(self._merges, num_elts, num_elts - n) idgen = UniqueIdGenerator() membership = [idgen[m] for m in membership] else: membership = self.membership return VertexClustering(self._graph, membership)
def comm_detection(g, algorithm, weights_input): if algorithm == 1: # (1)社区发现,newman快速算法,VertexDendrogram comm = list(g.community_fastgreedy(weights=weights_input).as_clustering()) comm.sort(key=lambda i: len(i), reverse=True) num = len(comm) elif algorithm == 2: # (2)社区发现,louvain算法,VertexClustering comm = list(g.community_multilevel(weights=weights_input)) comm.sort(key=lambda i: len(i), reverse=True) num = len(comm) elif algorithm == 3: # (3)社区发现,leading_eigenvector算法,VertexClustering comm = list(g.community_leading_eigenvector(weights=weights_input)) comm.sort(key=lambda i: len(i), reverse=True) num = len(comm) elif algorithm == 4: # (4)社区发现,label_propagation算法,VertexClustering comm = list(g.community_label_propagation(weights=weights_input, initial=None, fixed=None)) comm.sort(key=lambda i: len(i), reverse=True) num = len(comm) elif algorithm == 5: # (5)社区发现,walktrap算法,VertexDendrogram comm = list(g.community_walktrap(weights=weights_input, steps=4).as_clustering()) comm.sort(key=lambda i: len(i), reverse=True) num = len(comm) elif algorithm == 6: # (6)社区发现,GN算法,VertexDendrogram(时间久,不推荐大网络) se = g.community_edge_betweenness(clusters=None, directed=False, weights=weights_input) n = se._graph.vcount() max_q, optimal_count = 0, 1 for step in range(min(n - 1, len(se._merges))): membs = ig.community_to_membership(se._merges, n, step) q = se._graph.modularity(membs, **se._modularity_params) if q > max_q: optimal_count = n - step max_q = q se._optimal_count = optimal_count comm = list(se.as_clustering()) comm.sort(key=lambda i: len(i), reverse=True) num = len(comm) elif algorithm == 7: # (7)社区发现,spinglass算法,VertexClustering(时间久,不推荐大网络) clusters = g.clusters() giant = clusters.giant() comm = list(giant.community_spinglass()) comm.sort(key=lambda i: len(i), reverse=True) num = len(comm) elif algorithm == 8: # (8)社区发现,infomap算法,VertexClustering # 本算法需要给定簇团划分数量(trials=10),初始值为10 comm = list(g.community_infomap(edge_weights=weights_input, vertex_weights=None, trials=10)) comm.sort(key=lambda i: len(i), reverse=True) num = len(comm) else: print('算法指定错误') return comm, num