def test_adjusted_mutual(self):
        g = nx.karate_club_graph()
        leiden_communities = leiden(g)
        louvain_communities = louvain(g)

        score = evaluation.adjusted_mutual_information(louvain_communities, leiden_communities)

        self.assertLessEqual(score, 1)
        self.assertGreaterEqual(score, 0)
Exemple #2
0
    def adjusted_mutual_information(
            self, clustering: Clustering) -> evaluation.MatchingResult:
        """
        Adjusted Mutual Information between two clusterings.

        Adjusted Mutual Information (AMI) is an adjustment of the Mutual
        Information (MI) score to account for chance. It accounts for the fact that
        the MI is generally higher for two clusterings with a larger number of
        clusters, regardless of whether there is actually more information shared.
        For two clusterings :math:`U` and :math:`V`, the AMI is given as::

            AMI(U, V) = [MI(U, V) - E(MI(U, V))] / [max(H(U), H(V)) - E(MI(U, V))]

        This metric is independent of the absolute values of the labels:
        a permutation of the class or cluster label values won't change the
        score value in any way.

        This metric is furthermore symmetric: switching ``label_true`` with
        ``label_pred`` will return the same score value. This can be useful to
        measure the agreement of two independent label assignments strategies
        on the same dataset when the real ground truth is not known.

        Be mindful that this function is an order of magnitude slower than other
        metrics, such as the Adjusted Rand Index.

        :param clustering: NodeClustering object
        :return: AMI score

        :Example:

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

        :Reference:

        1. Vinh, N. X., Epps, J., & Bailey, J. (2010). **Information theoretic measures for clusterings comparison: Variants, properties, normalization and correction for chance.** Journal of Machine Learning Research, 11(Oct), 2837-2854.
        """
        return evaluation.adjusted_mutual_information(self, clustering)