code: def _calculate_global_linearity(
      predict_fn: Callable,
      input_shape: Tuple,
      X_samples: np.ndarray,
      model_type: str,
      alphas: np.ndarray,
  ) -> np.ndarray:
      """Calculates the norm of the difference between the output of a linear superposition of vectors and the
      linear superposition of the outputs for each individual vector.
  
      Parameters
      ----------
      predict_fn
          Model prediction function.
      input_shape
          Shape of the input.
      X_samples
          Array of feature vectors in the linear superposition.
      model_type
          Supported values: ``'classifier'`` | ``'regressor'``.
      alphas
          Array of coefficients in the linear superposition.
  
      Returns
      -------
      Linearity score.
  
      """
      ss = X_samples.shape[
          :2
      ]  # X_samples shape=(nb_instances, nb_samples, nb_features)
      X_samples = X_samples.reshape(
          (X_samples.shape[0] * X_samples.shape[1],) + input_shape
      )
  
      t_0 = time()
      if model_type == "classifier":
          outs = np.log(predict_fn(X_samples) + 1e-10)
          outs_shape = outs.shape[1:]
          outs = outs.reshape(
              ss + outs_shape
          )  # shape=(nb_instances, nb_samples, nb_classes)
      elif model_type == "regressor":
          outs = predict_fn(X_samples)
          outs_shape = outs.shape[1:]
          if len(outs.shape) == 1:
              outs = outs.reshape(
                  ss + (1,)
              )  # shape=(nb_instances, nb_samples, 1)
          else:  # if regression on multiple targets
              outs = outs.reshape(
                  ss + outs_shape
              )  # shape=(nb_instances, nb_samples, nb_targets)
      else:
          raise ValueError(
              "Passed 'model_type' not supported. Supported model types: 'classifier', 'regressor'"
          )
      t_f = time() - t_0
      logger.debug("predict time {}".format(t_f))
  
      if len(outs_shape) == 0:
          sum_out = np.matmul(alphas, outs)
      else:
          sum_out = _linear_superposition(alphas, outs, outs_shape)
  
      X_samples = X_samples.reshape(ss + input_shape)
      summ = _linear_superposition(alphas, X_samples, input_shape)
  
      if model_type == "classifier":
          out_sum = np.log(predict_fn(summ) + 1e-10)
          out_sum_shape = out_sum.shape[1:]
      elif model_type == "regressor":
          out_sum = predict_fn(summ)
          out_sum_shape = out_sum.shape[1:]
          if len(out_sum.shape) == 1:
              out_sum = out_sum.reshape((ss[0],) + (1,))
          else:
              out_sum = out_sum.reshape((ss[0],) + out_sum_shape)
      else:
          raise ValueError(
              "Passed 'model_type' not supported. Supported model types: 'classifier', 'regressor'"
          )
      logger.debug(out_sum.shape)
      logger.debug(sum_out.shape)
  
      diff = out_sum - sum_out
      linearity_score = norm(diff.reshape(diff.shape[0], -1), axis=1)
  
      return linearity_score
  
file_path: alibi/alibi/confidence/model_linearity.py
imports: import logging
  import string
  from time import time
  from typing import Callable, List, Optional, Tuple, Union
  import numpy
  from numpy.linalg import norm
  from sklearn.neighbors import NearestNeighbors
label_desc: Use image node_id 2 for calling a global function with example usage: _calculate_global_linearity(predict_fn, input_shape, X_samples, model_type, alphas) and returns: linearity_score