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_adjusted_rand(self):
        g = nx.karate_club_graph()
        lp_communities = label_propagation(g)
        louvain_communities = louvain(g)

        score = evaluation.adjusted_rand_index(louvain_communities, lp_communities)

        self.assertLessEqual(score.score, 1)
        self.assertGreaterEqual(score.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
Ejemplo n.º 4
0
    def adjusted_rand_index(
            self, clustering: Clustering) -> evaluation.MatchingResult:
        """
        Rand index adjusted for chance.

        The Rand Index computes a similarity measure between two clusterings
        by considering all pairs of samples and counting pairs that are
        assigned in the same or different clusters in the predicted and
        true clusterings.

        The raw RI score is then "adjusted for chance" into the ARI score
        using the following scheme::

            ARI = (RI - Expected_RI) / (max(RI) - Expected_RI)

        The adjusted Rand index is thus ensured to have a value close to
        0.0 for random labeling independently of the number of clusters and
        samples and exactly 1.0 when the clusterings are identical (up to
        a permutation).

        ARI is a symmetric measure::

            adjusted_rand_index(a, b) == adjusted_rand_index(b, a)

        :param clustering: NodeClustering object
        :return: ARI score

        :Example:

        >>> from cdlib.algorithms import louvain
        >>> g = nx.karate_club_graph()
        >>> communities = louvain(g)
        >>> leiden_communities = algorithms.leiden(g)
        >>> mod = communities.adjusted_rand_index(leiden_communities)


        :Reference:

        1. Hubert, L., & Arabie, P. (1985). **Comparing partitions**. Journal of classification, 2(1), 193-218.
        """
        return evaluation.adjusted_rand_index(self, clustering)
Ejemplo n.º 5
0
    print(testg)

    fluid = nx.algorithms.community.asyn_fluidc(testg, 10, seed=5)
    fluid2 = [list(x) for x in fluid]
    fluid3 = cdlib.NodeClustering(fluid2, testg, "FluidWeight")

    wcom = wf1.weight_fluid(testg, 10, seed=3)
    wcoms = [list(x) for x in wcom]
    wcoms2 = cdlib.NodeClustering(wcoms, testg, "FluidWeight")

    w2com = wf2.asyn_fluidcWeight(testg, 10, seed=3)
    w2coms = [list(x) for x in w2com]
    w2coms2 = cdlib.NodeClustering(w2coms, testg, "FluidWeight")

    print(evaluation.adjusted_rand_index(wcoms2, w2coms2))
    # print("Alg1")
    # print(evaluation.newman_girvan_modularity(testg, wcoms2).score)

    # louvain = algorithms.louvain(testg, weight='weight', resolution=1.5)

    reso = [14, 3.5, 1.5, 1, 0.7, 0.6, 0.4]
    comNumb = [5, 7, 10, 13, 15, 17, 20]

#     with open('modularityV3.csv', 'w', newline='') as file:
#         writer = csv.writer(file)
#         print("Louvain")
#         writer.writerow(["05/06"])
#         writer.writerow(["Louvain"])
#         writer.writerow(["Communities", "Modularity"])
#         for r, c in zip(reso, comNumb):