zoukankan      html  css  js  c++  java
  • COMMUNITY DETECTION_python-louvain

    Python-louvain Package

    pip install python-louvain

    import community
    #first compute the best partition partition
    = community.best_partition(G)

    #Drawing partition

    Method 1: 

    #drawing
    size = float(len(set(partition.values())))
    pos = nx.spring_layout(G)
    count = 0.
    for com in set(partition.values()) :
        count = 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()

    Method 2: 

    pos = nx.spring_layout(G)
    values = [partition.get(node) for node in G.nodes()]
    nx.draw_networkx(G, pos, cmap=plt.get_cmap('magma'), node_color=values, node_size=50, with_labels=False)

    Supplementary knowledge:

    1. what is the partition of graphs.

    partition: dict; {key (nodes_id): values(community_id)}

    2.  function : community.best_partition(G)

    Returns
        -------
        partition : dictionnary
           The partition, with communities numbered from 0 to number of communities
    def best_partition(graph,
                       partition=None,
                       weight='weight',
                       resolution=1.,
                       randomize=None,
                       random_state=None):
        """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
        random_state : int, RandomState instance or None, optional (default=None)
            If int, random_state is the seed used by the random number generator;
            If RandomState instance, random_state is the random number generator;
            If None, the random number generator is the RandomState instance used
            by `np.random`.
    
        Returns
        -------
        partition : dictionnary
           The partition, with communities numbered from 0 to number of communities
  • 相关阅读:
    决策树算法之ID3
    MSE与MAE的区别与选择
    (九)关键变量发掘技术
    (八)数据集切割
    (七)数据精简之(数据值精简和字段的精简)
    (六)数据精简之(数据记录精简)
    (五)数据编码是干什么
    (四)数据清洗主要工作
    pip-window安装
    CMDB学习之六 --客户端请求测试,服务端api优化
  • 原文地址:https://www.cnblogs.com/dulun/p/12219254.html
Copyright © 2011-2022 走看看