def overlapping_normalized_mutual_information_MGH(self, clustering, normalization="max"): """ Overlapping Normalized Mutual Information between two clusterings. Extension of the Normalized Mutual Information (NMI) score to cope with overlapping partitions. This is the version proposed by McDaid et al. using a different normalization than the original LFR one. See ref. for more details. :param clustering: NodeClustering object :param normalization: one of "max" or "LFK". Default "max" (corresponds to the main method described in the article) :return: onmi score :Example: >>> from cdlib import evaluation, algorithms >>> g = nx.karate_club_graph() >>> louvain_communities = algorithms.louvain(g) >>> leiden_communities = algorithms.leiden(g) >>> evaluation.overlapping_normalized_mutual_information_MGH(louvain_communities,leiden_communities) :Reference: 1. McDaid, A. F., Greene, D., & Hurley, N. (2011). Normalized mutual information to evaluate overlapping community finding algorithms. arXiv preprint arXiv:1110.2515. Chicago """ return evaluation.overlapping_normalized_mutual_information_MGH( self, clustering, normalization=normalization)
def test_onmi(self): g = nx.karate_club_graph() lp_communities = label_propagation(g) lp2_communities = label_propagation(g) score = evaluation.overlapping_normalized_mutual_information_MGH(lp2_communities, lp_communities) self.assertLessEqual(score.score, 1) self.assertGreaterEqual(score.score, 0) score = evaluation.overlapping_normalized_mutual_information_LFK(lp2_communities, lp_communities) self.assertLessEqual(score.score, 1) self.assertGreaterEqual(score.score, 0)
NMI_result[N] = dict() Omega_result[N] = dict() for mu in [0.1, 0.3]: NMI_result[N][mu] = {'NMI': [], 'NMI_max': []} Omega_result[N][mu] = {'omega': []} for om in range(2, 9): name = 'LFR/N-{}-mu{:1.1f}-om{}'.format(N, mu, om) communities = get_communities( os.path.join("../../data/", name, "result.dat"), " ", " ", False) gt_communities = get_communities( os.path.join("../../data/", name, "community.dat"), "\t", " ", True) G = nx.read_adjlist(os.path.join("../../data/", name, "network.dat"), nodetype=int) coms = NodeClustering(communities, G, "", overlap=True) gt_coms = NodeClustering(gt_communities, G, "", overlap=True) nmi = evaluation.overlapping_normalized_mutual_information_LFK( coms, gt_coms)[0] nmi_max = evaluation.overlapping_normalized_mutual_information_MGH( coms, gt_coms)[0] omega = evaluation.omega(coms, gt_coms)[0] NMI_result[N][mu]['NMI'].append(nmi) NMI_result[N][mu]['NMI_max'].append(nmi_max) Omega_result[N][mu]['omega'].append(omega) with open('LFR_nmi.json', 'w') as f: json.dump(NMI_result, f) with open('LFR_omega.json', 'w') as f: json.dump(Omega_result, f)