def homogeneity_completeness_v_measure(labels_true, labels_pred):
 (labels_true, labels_pred) = check_clusterings(labels_true, labels_pred)
 if (len(labels_true) == 0):
  return (1.0, 1.0, 1.0)
 entropy_C = entropy(labels_true)
 entropy_K = entropy(labels_pred)
 contingency = contingency_matrix(labels_true, labels_pred, sparse=True)
 MI = mutual_info_score(None, None, contingency=contingency)
 homogeneity = ((MI / entropy_C) if entropy_C else 1.0)
 completeness = ((MI / entropy_K) if entropy_K else 1.0)
 if ((homogeneity + completeness) == 0.0):
  v_measure_score = 0.0
 else:
  v_measure_score = (((2.0 * homogeneity) * completeness) / (homogeneity + completeness))
 return (homogeneity, completeness, v_measure_score)

def homogeneity_completeness_v_measure ( labels_true , labels_pred ) : ( labels_true , labels_pred ) = check_clusterings ( labels_true , labels_pred ) if ( len ( labels_true ) == 0 ) : return ( 1.0 , 1.0 , 1.0 ) entropy_C = entropy ( labels_true ) entropy_K = entropy ( labels_pred ) contingency = contingency_matrix ( labels_true , labels_pred , sparse = True ) MI = mutual_info_score ( None , None , contingency = contingency ) homogeneity = ( ( MI / entropy_C ) if entropy_C else 1.0 ) completeness = ( ( MI / entropy_K ) if entropy_K else 1.0 ) if ( ( homogeneity + completeness ) == 0.0 ) : v_measure_score = 0.0 else : v_measure_score = ( ( ( 2.0 * homogeneity ) * completeness ) / ( homogeneity + completeness ) ) return ( homogeneity , completeness , v_measure_score )
compute the homogeneity and completeness and v-measure scores at once .