Esempio n. 1
0
    def friedman_ranking(self):

        rp = {
            a: []
            for score, value in self.rankings.items()
            for a, vals in value.items()
        }
        for score, value in self.rankings.items():
            for a, vals in value.items():
                rp[a].append(vals[1])
        ranks = [x for x in rp.values()]

        for ids, alg in enumerate(rp):
            rp[alg] = ranks[ids]

        statistic, p_value, ranking, rank_cmp = friedman_test(*rp.values())

        self.rnk = {key: rank_cmp[i] for i, key in enumerate(rp.keys())}

        s_ranks = sorted(self.rnk.items(), key=lambda x: -x[1])
        self.ranks = [
            elem(rk=x + 1,
                 alg=c[0].split("_")[0],
                 param=c[0].split("_")[1],
                 score=c[1]) for x, c in enumerate(s_ranks)
        ]

        return self.ranks, p_value
Esempio n. 2
0
    def friedman_ranking(self) -> [list, float]:
        """
        Performs a Friedman ranking test.
        Tests the hypothesis that in a set of k dependent samples groups (where k >= 2) at least two of the groups represent populations with different median values.


        :return: a tuple whose first element is a dictionary assigning a rank to each Clustering object, while the second is the p-value associated to the ranking.

        :Example:

        >>> import networkx as nx
        >>> from cdlib import evaluation
        >>> from cdlib import algorithms
        >>> g = nx.karate_club_graph()
        >>> coms = algorithms.louvain(g)
        >>> coms2 = algorithms.demon(g, 0.25)
        >>> coms3 = algorithms.label_propagation(g)
        >>> coms4 = algorithms.angel(g, 0.6)
        >>> rk = evaluation.FitnessRanking(g, [coms2, coms, coms3, coms4])
        >>> rk.rank(evaluation.fraction_over_median_degree)
        >>> rk.rank(evaluation.edges_inside)
        >>> rk.rank(evaluation.cut_ratio)
        >>> rk.rank(evaluation.erdos_renyi_modularity)
        >>> rk.rank(evaluation.newman_girvan_modularity)
        >>> rk.rank(evaluation.modularity_density)
        >>> rnk, p_value = rk.friedman_ranking()

        :References:

        1. M. Friedman, The use of ranks to avoid the assumption of normality implicit in the analysis of variance, Journal of the American Statistical Association 32 (1937) 674–701.
        2. D.J. Sheskin, Handbook of parametric and nonparametric statistical procedures. crc Press, 2003, Test 25: The Friedman Two-Way Analysis of Variance by Ranks
        """

        rp = {
            a: []
            for score, value in self.rankings.items()
            for a, vals in value.items()
        }
        for score, value in self.rankings.items():
            for a, vals in value.items():
                rp[a].append(vals[1])
        ranks = [x for x in rp.values()]

        for ids, alg in enumerate(rp):
            rp[alg] = ranks[ids]

        statistic, p_value, ranking, rank_cmp = friedman_test(*rp.values())

        self.rnk = {key: rank_cmp[i] for i, key in enumerate(rp.keys())}

        s_ranks = sorted(self.rnk.items(), key=lambda x: -x[1])
        self.ranks = [
            elem(rk=x + 1,
                 alg=c[0].split("_")[0],
                 param=c[0].split("_")[1],
                 score=c[1]) for x, c in enumerate(s_ranks)
        ]

        return self.ranks, p_value