def adjusted_mutual_info_score(labels_true, labels_pred):
 (labels_true, labels_pred) = check_clusterings(labels_true, labels_pred)
 n_samples = labels_true.shape[0]
 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)
 emi = expected_mutual_information(contingency, n_samples)
 (h_true, h_pred) = (entropy(labels_true), entropy(labels_pred))
 ami = ((mi - emi) / (max(h_true, h_pred) - emi))
 return ami

def adjusted_mutual_info_score ( labels_true , labels_pred ) : ( labels_true , labels_pred ) = check_clusterings ( labels_true , labels_pred ) n_samples = labels_true . shape [ 0 ] 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 ) emi = expected_mutual_information ( contingency , n_samples ) ( h_true , h_pred ) = ( entropy ( labels_true ) , entropy ( labels_pred ) ) ami = ( ( mi - emi ) / ( max ( h_true , h_pred ) - emi ) ) return ami
adjusted mutual information between two clusterings .