def calinski_harabaz_score(X, labels):
 (X, labels) = check_X_y(X, labels)
 le = LabelEncoder()
 labels = le.fit_transform(labels)
 (n_samples, _) = X.shape
 n_labels = len(le.classes_)
 check_number_of_labels(n_labels, n_samples)
 (extra_disp, intra_disp) = (0.0, 0.0)
 mean = np.mean(X, axis=0)
 for k in range(n_labels):
  cluster_k = X[(labels == k)]
  mean_k = np.mean(cluster_k, axis=0)
  extra_disp += (len(cluster_k) * np.sum(((mean_k - mean) ** 2)))
  intra_disp += np.sum(((cluster_k - mean_k) ** 2))
 return (1.0 if (intra_disp == 0.0) else ((extra_disp * (n_samples - n_labels)) / (intra_disp * (n_labels - 1.0))))

def calinski_harabaz_score ( X , labels ) : ( X , labels ) = check_X_y ( X , labels ) le = LabelEncoder ( ) labels = le . fit_transform ( labels ) ( n_samples , _ ) = X . shape n_labels = len ( le . classes_ ) check_number_of_labels ( n_labels , n_samples ) ( extra_disp , intra_disp ) = ( 0.0 , 0.0 ) mean = np . mean ( X , axis = 0 ) for k in range ( n_labels ) : cluster_k = X [ ( labels == k ) ] mean_k = np . mean ( cluster_k , axis = 0 ) extra_disp += ( len ( cluster_k ) * np . sum ( ( ( mean_k - mean ) ** 2 ) ) ) intra_disp += np . sum ( ( ( cluster_k - mean_k ) ** 2 ) ) return ( 1.0 if ( intra_disp == 0.0 ) else ( ( extra_disp * ( n_samples - n_labels ) ) / ( intra_disp * ( n_labels - 1.0 ) ) ) )
compute the calinski and harabaz score .