code: def approximate_predict_flat(
      clusterer,
      points_to_predict,
      n_clusters=None,
      cluster_selection_epsilon=None,
      prediction_data=None,
      return_prediction_data=False,
  ):
      """
      Predict the cluster label of new points at a particular flat clustering,
          specified by n_clusters. This is a modified version of
          hdbscan.approximate_predict to allow selection of n_clusters.
  
      Parameters
      ----------
      clusterer : HDBSCAN
          A clustering object that has been fit to the data and
          either had ``prediction_data=True`` set, or called the
          ``generate_prediction_data`` method after the fact.
  
      points_to_predict : array, or array-like (n_samples, n_features)
          The new data points to predict cluster labels for. They should
          have the same dimensionality as the original dataset over which
          clusterer was fit.
  
      n_clusters: int, default=None
          The number of clusters to have in the flat clustering
              (over the training data, not points_to_predict)
          Ignored when prediction_data is supplied.
  
      cluster_selection_epsilon: float, default=None
          core-distance below which to stop splitting clusters.
          This can indirectly impose n_clusters.
          This argument is ignored if n_clusters is supplied.
  
      prediction_data: PredictionData, default=None
          If supplied, use this to predict clusters for points_to_predict.
          This allows predicting on multiple datasets without corrupting
              prediction data associated with clusterer.
  
          If neither n_clusters, nor prediction_data are supplied,
              then the prediction_data associated with clusterer is used.
  
      return_prediction_data: bool, default=False
          If True, return prediction_data along with labels and proba.
  
      Returns
      -------
      labels : array (n_samples,)
          The predicted labels of the ``points_to_predict``
  
      probabilities : array (n_samples,)
          The soft cluster scores for each of the ``points_to_predict``
  
      prediction_data: PredictionData, optional
          prediction_data used to predict.
          Returned if return_prediciton_data is set to True.
  
  
      Usage
      -----
      # From a fitted HDBSCAN model, predict for n_clusters=5
      labels, proba = approximate_predict_flat(
                          clusterer, X_predict, n_clusters=5)
  
      # Store prediciton data for later use.
      labels, proba, pred_data = approximate_predict_flat(
                                      clusterer, X_predict, n_clusters=5,
                                      return_prediction_data=True)
      # and use this prediction data to predict on new points
      labels1, proba1 = approximate_predict_flat(
                                      clusterer, X_pred1,
                                      prediction_data=pred_data)
  
      See Also
      ---------
      :py:func:`hdbscan.prediction.approximate_predict`
      """
      # Get number of fitted clusters for later use.
      n_clusters_fit = np.sum(np.unique(clusterer.labels_) >= 0)
      if n_clusters is not None:
          n_clusters = int(n_clusters)  # Ensure n_clusters is int
  
      # We'll need the condensed tree later...
      condensed_tree = clusterer.condensed_tree_
  
      # If none of the three arguments: prediction_data, n_clusters,
      #   and cluster_selection_epsilon are supplied,
      # then use clusterer's prediciton data directly
      if (
          (prediction_data is None)
          and ((n_clusters is None) or (n_clusters == n_clusters_fit))
          and (cluster_selection_epsilon is None)
      ):
          prediction_data = clusterer.prediction_data_
  
      # If either of n_clusters or cluster_selection_epsilon were supplied,
      #   then build prediction data from these by modifying clusterer's
      if not isinstance(prediction_data, PredictionData):
          if clusterer.prediction_data_ is None:
              raise ValueError(
                  "Clusterer does not have prediction data!"
                  " Try fitting with prediction_data=True set,"
                  " or run generate_prediction_data on the clusterer"
              )
          # Get prediction data from clusterer
          prediction_data = clusterer.prediction_data_
          # Modify prediction_data to reflect new n_clusters
          # First, make a copy of prediction data to avoid modifying source
          prediction_data = copy.deepcopy(prediction_data)
          # Cluster selection method is hold by condensed_tree.
          # Change from 'eom' to 'leaf' if n_clusters is too large.
          if (condensed_tree.cluster_selection_method == "eom") and (
              (n_clusters is not None) and (n_clusters > n_clusters_fit)
          ):
              warn(
                  f"Cannot predict more than {n_clusters_fit} with cluster "
                  "selection method 'eom'. Changing to method 'leaf'..."
              )
              condensed_tree.cluster_selection_method = "leaf"
          # This change does not affect the tree associated with 'clusterer'
          # Re-initialize prediction_data for the specified n_clusters or epsilon
          re_init(
              prediction_data,
              condensed_tree,
              n_clusters=n_clusters,
              cluster_selection_epsilon=cluster_selection_epsilon,
          )
  
      # ============================================================
      # Now we're ready to use prediction_data
      # The rest of the code is copied from HDBSCAN's approximate_predict,
      #   but modified to use prediction_data instead of clusterer's attribute
      points_to_predict = np.asarray(points_to_predict)
  
      if (
          points_to_predict.shape[1]
          != prediction_data.raw_data.shape[1]
      ):
          raise ValueError(
              "New points dimension does not match fit data!"
          )
  
      if prediction_data.cluster_tree.shape[0] == 0:
          warn(
              "Prediction data does not have any defined clusters, new data"
              " will be automatically predicted as noise."
          )
          labels = -1 * np.ones(
              points_to_predict.shape[0], dtype=np.int32
          )
          probabilities = np.zeros(
              points_to_predict.shape[0], dtype=np.float32
          )
          if return_prediction_data:
              return labels, probabilities, prediction_data
          else:
              return labels, probabilities
  
      labels = np.empty(points_to_predict.shape[0], dtype=np.int32)
      probabilities = np.empty(
          points_to_predict.shape[0], dtype=np.float64
      )
  
      min_samples = clusterer.min_samples or clusterer.min_cluster_size
      neighbor_distances, neighbor_indices = prediction_data.tree.query(
          points_to_predict, k=2 * min_samples
      )
  
      for i in range(points_to_predict.shape[0]):
          label, prob = _find_cluster_and_probability(
              condensed_tree,
              prediction_data.cluster_tree,
              neighbor_indices[i],
              neighbor_distances[i],
              prediction_data.core_distances,
              prediction_data.cluster_map,
              prediction_data.max_lambdas,
              min_samples,
          )
          labels[i] = label
          probabilities[i] = prob
  
      if return_prediction_data:
          return labels, probabilities, prediction_data
      else:
          return labels, probabilities
  
imports: import copy
  from warnings import warn
  import numpy
  from ._hdbscan_tree import compute_stability, get_cluster_tree_leaves
  from .hdbscan_ import HDBSCAN, _tree_to_labels
  from .plots import _bfs_from_cluster_tree
  from .prediction import PredictionData, _find_cluster_and_probability, _find_neighbor_and_lambda
  from ._prediction_utils import get_tree_row_with_child, dist_membership_vector, outlier_membership_vector, prob_in_some_cluster, all_points_dist_membership_vector, all_points_outlier_membership_vector, all_points_prob_in_some_cluster
label_desc: Use image node_id 2 for calling a global function with example usage: approximate_predict_flat(clusterer, points_to_predict, n_clusters, cluster_selection_epsilon, prediction_data, return_prediction_data) and returns: labels,  probabilities,  prediction_data, labels,  probabilities, labels,  probabilities,  prediction_data, labels,  probabilities