code: def silhouette_samples(X, labels, *, metric="euclidean", **kwds):
      """Compute the Silhouette Coefficient for each sample.
  
      The Silhouette Coefficient is a measure of how well samples are clustered
      with samples that are similar to themselves. Clustering models with a high
      Silhouette Coefficient are said to be dense, where samples in the same
      cluster are similar to each other, and well separated, where samples in
      different clusters are not very similar to each other.
  
      The Silhouette Coefficient is calculated using the mean intra-cluster
      distance (``a``) and the mean nearest-cluster distance (``b``) for each
      sample.  The Silhouette Coefficient for a sample is ``(b - a) / max(a,
      b)``.
      Note that Silhouette Coefficient is only defined if number of labels
      is 2 ``<= n_labels <= n_samples - 1``.
  
      This function returns the Silhouette Coefficient for each sample.
  
      The best value is 1 and the worst value is -1. Values near 0 indicate
      overlapping clusters.
  
      Read more in the :ref:`User Guide <silhouette_coefficient>`.
  
      Parameters
      ----------
      X : {array-like, sparse matrix} of shape (n_samples_a, n_samples_a) if metric == \
              "precomputed" or (n_samples_a, n_features) otherwise
          An array of pairwise distances between samples, or a feature array. If
          a sparse matrix is provided, CSR format should be favoured avoiding
          an additional copy.
  
      labels : array-like of shape (n_samples,)
          Label values for each sample.
  
      metric : str or callable, default='euclidean'
          The metric to use when calculating distance between instances in a
          feature array. If metric is a string, it must be one of the options
          allowed by :func:`~sklearn.metrics.pairwise_distances`.
          If ``X`` is the distance array itself, use "precomputed" as the metric.
          Precomputed distance matrices must have 0 along the diagonal.
  
      **kwds : optional keyword parameters
          Any further parameters are passed directly to the distance function.
          If using a ``scipy.spatial.distance`` metric, the parameters are still
          metric dependent. See the scipy docs for usage examples.
  
      Returns
      -------
      silhouette : array-like of shape (n_samples,)
          Silhouette Coefficients for each sample.
  
      References
      ----------
  
      .. [1] `Peter J. Rousseeuw (1987). "Silhouettes: a Graphical Aid to the
         Interpretation and Validation of Cluster Analysis". Computational
         and Applied Mathematics 20: 53-65.
         <https://www.sciencedirect.com/science/article/pii/0377042787901257>`_
  
      .. [2] `Wikipedia entry on the Silhouette Coefficient
         <https://en.wikipedia.org/wiki/Silhouette_(clustering)>`_
      """
      X, labels = check_X_y(X, labels, accept_sparse=["csr"])
  
      # Check for non-zero diagonal entries in precomputed distance matrix
      if metric == "precomputed":
          error_msg = ValueError(
              "The precomputed distance matrix contains non-zero "
              "elements on the diagonal. Use np.fill_diagonal(X, 0)."
          )
          if X.dtype.kind == "f":
              atol = np.finfo(X.dtype).eps * 100
              if np.any(np.abs(X.diagonal()) > atol):
                  raise error_msg
          elif np.any(X.diagonal() != 0):  # integral dtype
              raise error_msg
  
      le = LabelEncoder()
      labels = le.fit_transform(labels)
      n_samples = len(labels)
      label_freqs = np.bincount(labels)
      check_number_of_labels(len(le.classes_), n_samples)
  
      kwds["metric"] = metric
      reduce_func = functools.partial(
          _silhouette_reduce, labels=labels, label_freqs=label_freqs
      )
      results = zip(
          *pairwise_distances_chunked(
              X, reduce_func=reduce_func, **kwds
          )
      )
      intra_clust_dists, inter_clust_dists = results
      intra_clust_dists = np.concatenate(intra_clust_dists)
      inter_clust_dists = np.concatenate(inter_clust_dists)
  
      denom = (label_freqs - 1).take(labels, mode="clip")
      with np.errstate(divide="ignore", invalid="ignore"):
          intra_clust_dists /= denom
  
      sil_samples = inter_clust_dists - intra_clust_dists
      with np.errstate(divide="ignore", invalid="ignore"):
          sil_samples /= np.maximum(
              intra_clust_dists, inter_clust_dists
          )
      # nan values are for clusters of size 1, and should be 0
      return np.nan_to_num(sil_samples)
  
file_path: catboost/contrib/python/scikit-learn/py3/sklearn/metrics/cluster/_unsupervised.py
imports: import functools
  from numbers import Integral
  import numpy
  from scipy.sparse import issparse
  from ...preprocessing import LabelEncoder
  from ...utils import _safe_indexing, check_random_state, check_X_y
  from ...utils._param_validation import Interval, StrOptions, validate_params
  from ..pairwise import _VALID_METRICS, pairwise_distances, pairwise_distances_chunked
label_desc: Use image node_id 4 for calling a global function with example usage: silhouette_samples(X, labels) and returns: np