def analyze_cluster_run_diff(year, graphfile):

    with multiprocessing.Pool(4, initializer=init_worker, initargs=(graphfile,)) as p:
        clusterings = p.map(_cluster, range(100))

    init_worker(graphfile)

    scores_nmi = [
        evaluation.normalized_mutual_information(
            NodeClustering(c1, g, None), NodeClustering(c2, g, None)
        ).score
        for c1, c2 in itertools.combinations(clusterings, 2)
    ]

    scores_rand = [
        evaluation.adjusted_rand_index(
            NodeClustering(c1, g, None), NodeClustering(c2, g, None)
        ).score
        for c1, c2 in itertools.combinations(clusterings, 2)
    ]

    #     scores_pairs = [
    #         evaluation.adjusted_mutual_information(
    #             NodeClustering(c1, g, None),
    #             NodeClustering(c2, g, None)
    #         ).score
    #         for c1, c2 in zip(clusterings[::2],clusterings[1::2])
    #     ]

    return {
        "NMI": scores_nmi,
        "Rand": scores_rand,
    }
    def test_nmi(self):

        g = nx.karate_club_graph()
        louvain_communities = louvain(g)
        leiden_communities = leiden(g)

        score = evaluation.normalized_mutual_information(louvain_communities, leiden_communities)

        self.assertLessEqual(score, 1)
        self.assertGreaterEqual(score, 0)
def get_scores(idx1, idx2):
    c1 = significant_clusterings[idx1]
    c2 = significant_clusterings[idx2]

    score_nmi = evaluation.normalized_mutual_information(
        NodeClustering(c1, None, None), NodeClustering(c2, None, None)
    ).score

    score_rand = evaluation.adjusted_rand_index(
        NodeClustering(c1, None, None), NodeClustering(c2, None, None)
    ).score
    return score_nmi, score_rand
Example #4
0
    def normalized_mutual_information(self, clustering):
        """
        Normalized Mutual Information between two clusterings.

        Normalized Mutual Information (NMI) is an normalization of the Mutual
        Information (MI) score to scale the results between 0 (no mutual
        information) and 1 (perfect correlation). In this function, mutual
        information is normalized by ``sqrt(H(labels_true) * H(labels_pred))``

        :param clustering: NodeClustering object
        :return: normalized mutual information score

        :Example:

        >>> from cdlib.algorithms import louvain
        >>> g = nx.karate_club_graph()
        >>> communities = louvain(g)
        >>> mod = communities.normalized_mutual_information([[1,2], [3,4]])

        """

        return evaluation.normalized_mutual_information(self, clustering)