code: def validity_index(
      X,
      labels,
      metric="euclidean",
      d=None,
      per_cluster_scores=False,
      mst_raw_dist=False,
      verbose=False,
      **kwd_args,
  ):
      """
      Compute the density based cluster validity index for the
      clustering specified by `labels` and for each cluster in `labels`.
  
      Parameters
      ----------
      X : array (n_samples, n_features) or (n_samples, n_samples)
          The input data of the clustering. This can be the data, or, if
          metric is set to `precomputed` the pairwise distance matrix used
          for the clustering.
  
      labels : array (n_samples)
          The label array output by the clustering, providing an integral
          cluster label to each data point, with -1 for noise points.
  
      metric : optional, string (default 'euclidean')
          The metric used to compute distances for the clustering (and
          to be re-used in computing distances for mr distance). If
          set to `precomputed` then X is assumed to be the precomputed
          distance matrix between samples.
  
      d : optional, integer (or None) (default None)
          The number of features (dimension) of the dataset. This need only
          be set in the case of metric being set to `precomputed`, where
          the ambient dimension of the data is unknown to the function.
  
      per_cluster_scores : optional, boolean (default False)
          Whether to return the validity index for individual clusters.
          Defaults to False with the function returning a single float
          value for the whole clustering.
  
      mst_raw_dist : optional, boolean (default False)
          If True, the MST's are constructed solely via 'raw' distances (depending on the given metric, e.g. euclidean distances)
          instead of using mutual reachability distances. Thus setting this parameter to True avoids using 'all-points-core-distances' at all.
          This is advantageous specifically in the case of elongated clusters that lie in close proximity to each other <citation needed>.
  
      **kwd_args :
          Extra arguments to pass to the distance computation for other
          metrics, such as minkowski, Mahanalobis etc.
  
      Returns
      -------
      validity_index : float
          The density based cluster validity index for the clustering. This
          is a numeric value between -1 and 1, with higher values indicating
          a 'better' clustering.
  
      per_cluster_validity_index : array (n_clusters,)
          The cluster validity index of each individual cluster as an array.
          The overall validity index is the weighted average of these values.
          Only returned if per_cluster_scores is set to True.
  
      References
      ----------
      Moulavi, D., Jaskowiak, P.A., Campello, R.J., Zimek, A. and Sander, J.,
      2014. Density-Based Clustering Validation. In SDM (pp. 839-847).
      """
      core_distances = {}
      density_sparseness = {}
      mst_nodes = {}
      mst_edges = {}
  
      max_cluster_id = labels.max() + 1
      density_sep = np.inf * np.ones(
          (max_cluster_id, max_cluster_id), dtype=np.float64
      )
      cluster_validity_indices = np.empty(
          max_cluster_id, dtype=np.float64
      )
  
      for cluster_id in range(max_cluster_id):
          if np.sum(labels == cluster_id) == 0:
              continue
  
          (
              distances_for_mst,
              core_distances[cluster_id],
          ) = distances_between_points(
              X,
              labels,
              cluster_id,
              metric,
              d,
              no_coredist=mst_raw_dist,
              print_max_raw_to_coredist_ratio=verbose,
              **kwd_args,
          )
  
          (
              mst_nodes[cluster_id],
              mst_edges[cluster_id],
          ) = internal_minimum_spanning_tree(distances_for_mst)
          density_sparseness[cluster_id] = (
              mst_edges[cluster_id].T[2].max()
          )
  
      for i in range(max_cluster_id):
          if np.sum(labels == i) == 0:
              continue
  
          internal_nodes_i = mst_nodes[i]
          for j in range(i + 1, max_cluster_id):
              if np.sum(labels == j) == 0:
                  continue
  
              internal_nodes_j = mst_nodes[j]
              density_sep[i, j] = density_separation(
                  X,
                  labels,
                  i,
                  j,
                  internal_nodes_i,
                  internal_nodes_j,
                  core_distances[i],
                  core_distances[j],
                  metric=metric,
                  no_coredist=mst_raw_dist,
                  **kwd_args,
              )
              density_sep[j, i] = density_sep[i, j]
  
      n_samples = float(X.shape[0])
      result = 0
  
      for i in range(max_cluster_id):
          if np.sum(labels == i) == 0:
              continue
  
          min_density_sep = density_sep[i].min()
          cluster_validity_indices[i] = (
              min_density_sep - density_sparseness[i]
          ) / max(min_density_sep, density_sparseness[i])
  
          if verbose:
              print(
                  "Minimum density separation: " + str(min_density_sep)
              )
              print("Density sparseness: " + str(density_sparseness[i]))
  
          cluster_size = np.sum(labels == i)
          result += (
              cluster_size / n_samples
          ) * cluster_validity_indices[i]
  
      if per_cluster_scores:
          return result, cluster_validity_indices
      else:
          return result
  
file_path: hdbscan/hdbscan/validity.py
imports: import numpy
  from sklearn.metrics import pairwise_distances
  from scipy.spatial.distance import cdist
  from ._hdbscan_linkage import mst_linkage_core
  from .hdbscan_ import isclose
label_desc: Use image node_id 6 for calling a global function with example usage: validity_index(X, labels, metric, d, per_cluster_scores, mst_raw_dist, verbose) and returns: result,  cluster_validity_indices, result