def normalized_mutual_info_score(labels_true, labels_pred):
 (labels_true, labels_pred) = check_clusterings(labels_true, labels_pred)
 classes = np.unique(labels_true)
 clusters = np.unique(labels_pred)
 if ((classes.shape[0] == clusters.shape[0] == 1) or (classes.shape[0] == clusters.shape[0] == 0)):
  return 1.0
 contingency = contingency_matrix(labels_true, labels_pred, sparse=True)
 contingency = contingency.astype(np.float64)
 mi = mutual_info_score(labels_true, labels_pred, contingency=contingency)
 (h_true, h_pred) = (entropy(labels_true), entropy(labels_pred))
 nmi = (mi / max(np.sqrt((h_true * h_pred)), 1e-10))
 return nmi

def normalized_mutual_info_score ( labels_true , labels_pred ) : ( labels_true , labels_pred ) = check_clusterings ( labels_true , labels_pred ) classes = np . unique ( labels_true ) clusters = np . unique ( labels_pred ) if ( ( classes . shape [ 0 ] == clusters . shape [ 0 ] == 1 ) or ( classes . shape [ 0 ] == clusters . shape [ 0 ] == 0 ) ) : return 1.0 contingency = contingency_matrix ( labels_true , labels_pred , sparse = True ) contingency = contingency . astype ( np . float64 ) mi = mutual_info_score ( labels_true , labels_pred , contingency = contingency ) ( h_true , h_pred ) = ( entropy ( labels_true ) , entropy ( labels_pred ) ) nmi = ( mi / max ( np . sqrt ( ( h_true * h_pred ) ) , 1e-10 ) ) return nmi
normalized mutual information between two clusterings .