code: def hardness_region_competence(neighbors_idx, labels, safe_k):
      """Calculate the Instance hardness of the sample based on its neighborhood.
      The sample is deemed hard to classify when there is overlap between
      different classes in the region of competence. This method does not
      takes into account the target label of the test sample
  
      This hardness measure is used to select whether use DS or use the KNN for
      the classification of a given query sample
  
      Parameters
      ----------
      neighbors_idx : array of shape = [n_samples_test, k]
          Indices of the nearest neighbors for each considered sample
  
      labels : array of shape = [n_samples_train]
          labels associated with each training sample
  
      safe_k : int
          Number of neighbors used to estimate the hardness of the corresponding
          region
  
      Returns
      -------
      hardness : array of shape = [n_samples_test]
          The Hardness level associated with each example.
  
      References
      ----------
      Smith, M.R., Martinez, T. and Giraud-Carrier, C., 2014. An instance level
      analysis of data complexity.
      Machine learning, 95(2), pp.225-256
      """
      if neighbors_idx.ndim < 2:
          neighbors_idx = np.atleast_2d(neighbors_idx)
  
      neighbors_y = labels[neighbors_idx[:, :safe_k]]
      _, num_majority_class = mode(neighbors_y, axis=1)
      hardness = ((safe_k - num_majority_class) / safe_k).reshape(
          -1,
      )
  
      return hardness
  
imports: import numpy
  from scipy.stats import mode
  from sklearn.neighbors import NearestNeighbors
label_desc: Use image node_id 1 for calling a global function with example usage: hardness_region_competence(neighbors_idx, labels, safe_k) and returns: hardness