コード例 #1
0
ファイル: node_clustering.py プロジェクト: rparrapy/cdlib
    def significance(self):
        """
        Significance estimates how likely a partition of dense communities appear in a random graph.

        :return: the significance score

        :Example:

        >>> from cdlib.algorithms import louvain
        >>> g = nx.karate_club_graph()
        >>> communities = louvain(g)
        >>> mod = communities.significance()


        :References:

        Traag, V. A., Aldecoa, R., & Delvenne, J. C. (2015). **Detecting communities using asymptotical surprise.** Physical Review E, 92(2), 022816.
        """

        if self.__check_graph():
            return evaluation.significance(self.graph, self)
        else:
            raise ValueError("Graph instance not specified")
コード例 #2
0
    def test_significance(self):
        g = nx.karate_club_graph()
        communities = louvain(g)

        mod = evaluation.significance(g, communities)
        self.assertGreaterEqual(mod.score, 0)
コード例 #3
0
ファイル: exp_CDLIB.py プロジェクト: aponom84/lpam-clustering
def getAllScoresDict(g, _reference, _communities, executionTime):
    scores = {}
    scores['time'] = executionTime
    reference = copy.deepcopy(_reference)
    reference.communities = complete_partition(reference.communities, g, mode='new_cluster')
    communities = copy.deepcopy(_communities)
    communities.communities = complete_partition(communities.communities, g, mode='new_cluster')



    # scores['adjusted_mutual_information'] = evaluation.adjusted_mutual_information(reference,communities).score

    # returns MatchingResult object
    # scores['adjusted_rand_index'] = evaluation.adjusted_rand_index(reference,communities).score
    # Compute the average F1 score of the optimal algorithms matches among the partitions in input.
    try:
        scores['f1'] = evaluation.f1(reference, communities).score
    except:
        scores['f1'] = np.nan
    # Compute the Normalized F1 score of the optimal algorithms matches among the partitions in input.
    try:
        scores['nf1'] = evaluation.nf1(reference, communities).score
    except:
        scores['nf1'] = np.nan
    # Normalized Mutual Information between two clusterings.
    # scores['normalized_mutual_information'] = evaluation.normalized_mutual_information(reference, communities)[0]
    # Index of resemblance for overlapping, complete coverage, network clusterings.
    try:
        scores['omega'] = evaluation.omega(reference, communities).score
    except:
        scores['omega'] = np.nan
    # Overlapping Normalized Mutual Information between two clusterings.
    try:
        scores['overlapping_normalized_mutual_information_LFK'] = evaluation.overlapping_normalized_mutual_information_LFK(reference, communities)[0]
    except:
        scores['overlapping_normalized_mutual_information_LFK']  = np.nan
    # Overlapping Normalized Mutual Information between two clusterings.
    # scores['overlapping_normalized_mutual_information_MGH'] =  evaluation.overlapping_normalized_mutual_information_MGH(reference, communities)[0]
    # Variation of Information among two nodes partitions.
    # scores['variation_of_information'] =  evaluation.variation_of_information(reference, communities)[0]

    # scores['avg_distance'] = evaluation.avg_distance(g,communities, summary=True)
    try:
        scores['avg_embeddedness'] = evaluation.avg_embeddedness(g,communities, summary=True).score
    except:
        scores['avg_embeddedness'] = np.nan
    try:
        scores['average_internal_degree'] = evaluation.average_internal_degree(g,communities, summary=True).score
    except:
        scores['average_internal_degree'] = np.nan
    # scores['avg_transitivity']  = evaluation.avg_transitivity(g,communities, summary=True)
    # Fraction of total edge volume that points outside the community.
    try:
        scores['conductance']  = evaluation.conductance(g,communities, summary=True).score
    except:
        scores['conductance'] = np.nan
    # Fraction of existing edges (out of all possible edges) leaving the community.
    try:
        scores['cut_ratio']  = evaluation.cut_ratio(g,communities, summary=True).score
    except:
        scores['cut_ratio'] = np.nan

    # Number of edges internal to the community
    try:
        scores['edges_inside']  = evaluation.edges_inside(g,communities, summary=True).score
    except:
        scores['edges_inside'] = np.nan
    # Number of edges per community node that point outside the cluster
    try:
        scores['expansion']  = evaluation.expansion(g,communities, summary=True).score
    except:
        scores['expansion'] = np.nan
    # Fraction of community nodes of having internal degree higher than the median degree value.
    try:
        scores['fraction_over_median_degree']  = evaluation.fraction_over_median_degree(g,communities, summary=True).score
    except:
        scores['fraction_over_median_degree'] = np.nan
    # The hub dominance of a community is defined as the ratio of the degree of its most connected node w.r.t. the theoretically maximal degree within the community.
    # scores['hub_dominance']  = evaluation.hub_dominance(g,communities, summary=True)
    # The internal density of the community set.
    try:
        scores['internal_edge_density'] = evaluation.internal_edge_density(g,communities, summary=True).score
    except:
        scores['internal_edge_density'] = np.nan
    # Normalized variant of the Cut-Ratio
    try:
        scores['normalized_cut']  = evaluation.normalized_cut(g,communities, summary=True).score
    except:
        scores['normalized_cut'] = np.nan
    # Maximum fraction of edges of a node of a community that point outside the community itself.
    # scores['max_odf']  = evaluation.max_odf(g,communities, summary=True)
    # Average fraction of edges of a node of a community that point outside the community itself.
    # scores['avg_odf']  = evaluation.avg_odf(g,communities, summary=True)
    # Fraction of nodes in S that have fewer edges pointing inside than to the outside of the community.
    # scores['flake_odf']  = evaluation.flake_odf(g,communities, summary=True)
    # The scaled density of a community is defined as the ratio of the community density w.r.t. the complete graph density.
    try:
        scores['scaled_density']  = evaluation.scaled_density(g,communities, summary=True).score
    except:
        scores['scaled_density'] = np.nan
    # Significance estimates how likely a partition of dense communities appear in a random graph.
    try:
        scores['significance'] = evaluation.significance(g,communities).score
    except:
        scores['significance'] = np.nan
    # Size is the number of nodes in the community
    try:
        scores['size']  = evaluation.size(g,communities, summary=True).score
    except:
        scores['size'] = np.nan
    # Surprise is statistical approach proposes a quality metric assuming that edges between vertices emerge randomly according to a hyper-geometric distribution.
    # According to the Surprise metric, the higher the score of a partition, the less likely it is resulted from a random realization, the better the quality of the community structure.
    try:
        scores['surprise'] = evaluation.surprise(g,communities).score
    except:
        scores['surprise'] = np.nan

    try:
        scores['modularity_density'] = evaluation.modularity_density(g,communities).score
    except:
        scores['modularity_density'] = np.nan

    # Fraction of community nodes that belong to a triad.
    # scores['triangle_participation_ratio']  = evaluation.triangle_participation_ratio(g,communities, summary=True)
    # Purity is the product of the frequencies of the most frequent labels carried by the nodes within the communities
    # scores['purity'] = evaluation.purity(communities)
    return scores
コード例 #4
0
ファイル: exp_CDLIB.py プロジェクト: aponom84/lpam-clustering
            forDf['method'].append(method)
            forDf['score'].append(score)

    df = pd.DataFrame(forDf)
    df.to_csv('cdlib_results_legacy_part.csv')

    graphStatistics = {'graph': [], 'statistic': [], 'value':[]}
    statistics = {
        '$|V|$': lambda graph, reference: len(graph.nodes),
        '$|E|$': lambda graph, reference: len(graph.edges),
        '$\\hat{k}$': lambda graph, reference: len(reference.communities),
        '$\\hat{o}$': lambda graph, reference: getOverlappingNumber(reference.communities),
        '$\\hat{c}$': lambda graph, reference: nx.average_clustering(graph),
        '\\shortstack[l]{normalized \\\\ cut}': lambda graph, reference: evaluation.normalized_cut(graph,reference, summary=True).score,
        '\\shortstack[l]{internal \\\\ edge \\\\density}': lambda graph, reference: evaluation.internal_edge_density(graph,reference, summary=True).score,
        'significance': lambda graph, reference: evaluation.significance(graph,reference, summary=True).score,
        '\\shortstack[l]{avarage \\\\ internal \\\\degree}': lambda graph, reference: evaluation.average_internal_degree(graph,reference, summary=True).score,
        '\\shortstack[l]{modularity \\\\ density}': lambda graph, reference: evaluation.modularity_density(graph,reference).score,
    }

    for graph, name, reference in zip(graphs, names, references):
        for statistic_name, statistic in statistics.items():
            graphStatistics['graph'].append(name)
            graphStatistics['statistic'].append(statistic_name)
            graphStatistics['value'].append(statistic(graph, reference) )

    graphStatisticsDF = pd.DataFrame(graphStatistics)
    graphStatisticsDF.to_csv('../Results/graphs_stats_legacy.csv')

    # method that were not used, or don't work by some reason