Exemplo n.º 1
0
    def topsis(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 = np.array([np.array(x) for x in rp.values()])

        agg_rank = topsis(ranks, [1] * len(self.rankings), 'v', 'm')
        for ids, alg in enumerate(rp):
            rp[alg] = agg_rank[ids]

        s_ranks = sorted(rp.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)
        ]

        self.rnk = {f"{i.alg}_{i.param}": i.score for i in self.ranks}

        return self.ranks, None
Exemplo n.º 2
0
    def topsis(self) -> [list, None]:
        """
        The Technique for Order of Preference by Similarity to Ideal Solution (TOPSIS) is a multi-criteria decision analysis method.
        TOPSIS is based on the concept that the chosen alternative should have the shortest geometric distance from the positive ideal solution (PIS) and the longest geometric distance from the negative ideal solution (NIS).

        :return: a tuple whose first element is the ranking dictionary assigning a TOPSIS score to each Clustering object, while the second is None (to maintain coherence with friedman_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, _ = rk.topsis()

        :References:

        1. Hwang, C.L.; Yoon, K. (1981). Multiple Attribute Decision Making: Methods and Applications. New York: Springer-Verlag.
        2. Yoon, K. (1987). "A reconciliation among discrete compromise situations". Journal of the Operational Research Society. 38 (3): 277–286. doi:10.1057/jors.1987.44.
        """
        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 = np.array([np.array(x) for x in rp.values()])

        agg_rank = topsis(ranks, [1] * len(self.rankings), "v", "m")
        for ids, alg in enumerate(rp):
            rp[alg] = agg_rank[ids]

        s_ranks = sorted(rp.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)
        ]

        self.rnk = {f"{i.alg}_{i.param}": i.score for i in self.ranks}

        return self.ranks, None