<s> """<STR_LIT>""" <EOL> from math import log <EOL> from scipy . misc import comb <EOL> from scipy . sparse import coo_matrix <EOL> import numpy as np <EOL> from . expected_mutual_info_fast import expected_mutual_information <EOL> from ... utils . fixes import bincount <EOL> def comb2 ( n ) : <EOL> return comb ( n , <NUM_LIT:2> , exact = <NUM_LIT:1> ) <EOL> def check_clusterings ( labels_true , labels_pred ) : <EOL> """<STR_LIT>""" <EOL> labels_true = np . asarray ( labels_true ) <EOL> labels_pred = np . asarray ( labels_pred ) <EOL> if labels_true . ndim != <NUM_LIT:1> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % ( labels_true . shape , ) ) <EOL> if labels_pred . ndim != <NUM_LIT:1> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % ( labels_pred . shape , ) ) <EOL> if labels_true . shape != labels_pred . shape : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> % ( labels_true . shape [ <NUM_LIT:0> ] , labels_pred . shape [ <NUM_LIT:0> ] ) ) <EOL> return labels_true , labels_pred <EOL> def contingency_matrix ( labels_true , labels_pred , eps = None , max_n_classes = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> classes , class_idx = np . unique ( labels_true , return_inverse = True ) <EOL> clusters , cluster_idx = np . unique ( labels_pred , return_inverse = True ) <EOL> n_classes = classes . shape [ <NUM_LIT:0> ] <EOL> n_clusters = clusters . shape [ <NUM_LIT:0> ] <EOL> if n_classes > max_n_classes : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if n_clusters > max_n_classes : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> contingency = coo_matrix ( ( np . ones ( class_idx . shape [ <NUM_LIT:0> ] ) , <EOL> ( class_idx , cluster_idx ) ) , <EOL> shape = ( n_classes , n_clusters ) , <EOL> dtype = np . int ) . toarray ( ) <EOL> if eps is not None : <EOL> contingency = contingency + eps <EOL> return contingency <EOL> def adjusted_rand_score ( labels_true , labels_pred , max_n_classes = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> labels_true , labels_pred = check_clusterings ( labels_true , labels_pred ) <EOL> n_samples = labels_true . shape [ <NUM_LIT:0> ] <EOL> classes = np . unique ( labels_true ) <EOL> clusters = np . unique ( labels_pred ) <EOL> if ( classes . shape [ <NUM_LIT:0> ] == clusters . shape [ <NUM_LIT:0> ] == <NUM_LIT:1> <EOL> or classes . shape [ <NUM_LIT:0> ] == clusters . shape [ <NUM_LIT:0> ] == <NUM_LIT:0> <EOL> or classes . shape [ <NUM_LIT:0> ] == clusters . shape [ <NUM_LIT:0> ] == len ( labels_true ) ) : <EOL> return <NUM_LIT:1.0> <EOL> contingency = contingency_matrix ( labels_true , labels_pred , <EOL> max_n_classes = max_n_classes ) <EOL> sum_comb_c = sum ( comb2 ( n_c ) for n_c in contingency . sum ( axis = <NUM_LIT:1> ) ) <EOL> sum_comb_k = sum ( comb2 ( n_k ) for n_k in contingency . sum ( axis = <NUM_LIT:0> ) ) <EOL> sum_comb = sum ( comb2 ( n_ij ) for n_ij in contingency . flatten ( ) ) <EOL> prod_comb = ( sum_comb_c * sum_comb_k ) / float ( comb ( n_samples , <NUM_LIT:2> ) ) <EOL> mean_comb = ( sum_comb_k + sum_comb_c ) / <NUM_LIT> <EOL> return ( ( sum_comb - prod_comb ) / ( mean_comb - prod_comb ) ) <EOL> def homogeneity_completeness_v_measure ( labels_true , labels_pred , <EOL> max_n_classes = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> labels_true , labels_pred = check_clusterings ( labels_true , labels_pred ) <EOL> if len ( labels_true ) == <NUM_LIT:0> : <EOL> return <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> <EOL> entropy_C = entropy ( labels_true ) <EOL> entropy_K = entropy ( labels_pred ) <EOL> MI = mutual_info_score ( labels_true , labels_pred , <EOL> max_n_classes = max_n_classes ) <EOL> homogeneity = MI / ( entropy_C ) if entropy_C else <NUM_LIT:1.0> <EOL> completeness = MI / ( entropy_K ) if entropy_K else <NUM_LIT:1.0> <EOL> if homogeneity + completeness == <NUM_LIT:0.0> : <EOL> v_measure_score = <NUM_LIT:0.0> <EOL> else : <EOL> v_measure_score = ( <NUM_LIT> * homogeneity * completeness <EOL> / ( homogeneity + completeness ) ) <EOL> return homogeneity , completeness , v_measure_score <EOL> def homogeneity_score ( labels_true , labels_pred , max_n_classes = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return homogeneity_completeness_v_measure ( labels_true , labels_pred , <EOL> max_n_classes ) [ <NUM_LIT:0> ] <EOL> def completeness_score ( labels_true , labels_pred , max_n_classes = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return homogeneity_completeness_v_measure ( labels_true , labels_pred , <EOL> max_n_classes ) [ <NUM_LIT:1> ] <EOL> def v_measure_score ( labels_true , labels_pred , max_n_classes = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return homogeneity_completeness_v_measure ( labels_true , labels_pred , <EOL> max_n_classes ) [ <NUM_LIT:2> ] <EOL> def mutual_info_score ( labels_true , labels_pred , contingency = None , <EOL> max_n_classes = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if contingency is None : <EOL> labels_true , labels_pred = check_clusterings ( labels_true , labels_pred ) <EOL> contingency = contingency_matrix ( labels_true , labels_pred , <EOL> max_n_classes = max_n_classes ) <EOL> contingency = np . array ( contingency , dtype = '<STR_LIT:float>' ) <EOL> contingency_sum = np . sum ( contingency ) <EOL> pi = np . sum ( contingency , axis = <NUM_LIT:1> ) <EOL> pj = np . sum ( contingency , axis = <NUM_LIT:0> ) <EOL> outer = np . outer ( pi , pj ) <EOL> nnz = contingency != <NUM_LIT:0.0> <EOL> contingency_nm = contingency [ nnz ] <EOL> log_contingency_nm = np . log ( contingency_nm ) <EOL> contingency_nm /= contingency_sum <EOL> log_outer = - np . log ( outer [ nnz ] ) + log ( pi . sum ( ) ) + log ( pj . sum ( ) ) <EOL> mi = ( contingency_nm * ( log_contingency_nm - log ( contingency_sum ) ) <EOL> + contingency_nm * log_outer ) <EOL> return mi . sum ( ) <EOL> def adjusted_mutual_info_score ( labels_true , labels_pred , max_n_classes = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> labels_true , labels_pred = check_clusterings ( labels_true , labels_pred ) <EOL> n_samples = labels_true . shape [ <NUM_LIT:0> ] <EOL> classes = np . unique ( labels_true ) <EOL> clusters = np . unique ( labels_pred ) <EOL> if ( classes . shape [ <NUM_LIT:0> ] == clusters . shape [ <NUM_LIT:0> ] == <NUM_LIT:1> <EOL> or classes . shape [ <NUM_LIT:0> ] == clusters . shape [ <NUM_LIT:0> ] == <NUM_LIT:0> ) : <EOL> return <NUM_LIT:1.0> <EOL> contingency = contingency_matrix ( labels_true , labels_pred , <EOL> max_n_classes = max_n_classes ) <EOL> contingency = np . array ( contingency , dtype = '<STR_LIT:float>' ) <EOL> mi = mutual_info_score ( labels_true , labels_pred , <EOL> contingency = contingency ) <EOL> emi = expected_mutual_information ( contingency , n_samples ) <EOL> h_true , h_pred = entropy ( labels_true ) , entropy ( labels_pred ) <EOL> ami = ( mi - emi ) / ( max ( h_true , h_pred ) - emi ) <EOL> return ami <EOL> def normalized_mutual_info_score ( labels_true , labels_pred , max_n_classes = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> labels_true , labels_pred = check_clusterings ( labels_true , labels_pred ) <EOL> classes = np . unique ( labels_true ) <EOL> clusters = np . unique ( labels_pred ) <EOL> if ( classes . shape [ <NUM_LIT:0> ] == clusters . shape [ <NUM_LIT:0> ] == <NUM_LIT:1> <EOL> or classes . shape [ <NUM_LIT:0> ] == clusters . shape [ <NUM_LIT:0> ] == <NUM_LIT:0> ) : <EOL> return <NUM_LIT:1.0> <EOL> contingency = contingency_matrix ( labels_true , labels_pred , <EOL> max_n_classes = max_n_classes ) <EOL> contingency = np . array ( contingency , dtype = '<STR_LIT:float>' ) <EOL> mi = mutual_info_score ( labels_true , labels_pred , <EOL> contingency = contingency ) <EOL> h_true , h_pred = entropy ( labels_true ) , entropy ( labels_pred ) <EOL> nmi = mi / max ( np . sqrt ( h_true * h_pred ) , <NUM_LIT> ) <EOL> return nmi <EOL> def entropy ( labels ) : <EOL> """<STR_LIT>""" <EOL> if len ( labels ) == <NUM_LIT:0> : <EOL> return <NUM_LIT:1.0> <EOL> label_idx = np . unique ( labels , return_inverse = True ) [ <NUM_LIT:1> ] <EOL> pi = bincount ( label_idx ) . astype ( np . float ) <EOL> pi = pi [ pi > <NUM_LIT:0> ] <EOL> pi_sum = np . sum ( pi ) <EOL> return - np . sum ( ( pi / pi_sum ) * ( np . log ( pi ) - log ( pi_sum ) ) ) </s>
