code: def _calculate_pairwise_linearity(
      predict_fn: Callable,
      x: np.ndarray,
      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 a test vector `x` and
      vectors in `X_samples` and the linear superposition of the outputs, averaged over all the vectors in `X_samples`.
  
      Parameters
      ----------
      predict_fn
          Model prediction function.
      x
          Test instance for which to calculate the linearity measure.
      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:]
          x_out = np.log(
              predict_fn(x) + 1e-10
          )  # shape=(nb_instances, nb_classes)
          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:]
          x_out = predict_fn(x)
          if len(outs.shape) == 1:
              outs = outs.reshape(
                  ss + (1,)
              )  # shape=(nb_instances, nb_samples, 1)
              x_out = x_out.reshape(
                  x_out.shape + (1,)
              )  # shape=(nb_instances, 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", t_f)
  
      x_out_stack = np.repeat(
          x_out.reshape(
              (
                  x_out.shape[0],
                  1,
              )
              + (x_out.shape[1:])
          ),
          outs.shape[1],
          axis=1,
      )
  
      # linear superposition of the outputs
      sum_out = np.matmul(
          np.array([x_out_stack, outs]).T, alphas
      ).T  # shape=(nb_instances,nb_samples,nb_targets)
  
      X_samples = X_samples.reshape(ss + input_shape)
      x_stack = np.repeat(
          x.reshape(
              (
                  x.shape[0],
                  1,
              )
              + (x.shape[1:])
          ),
          X_samples.shape[1],
          axis=1,
      )
  
      # linear superposition of the inputs
      summ = np.matmul(
          np.array([x_stack, X_samples]).T, alphas
      ).T  # shape=(nb_instances,nb_samples,input_shape)
      if model_type == "classifier":
          # output of the linear superposition of inputs
          out_sum = np.log(
              predict_fn(
                  summ.reshape(
                      (summ.shape[0] * summ.shape[1],) + summ.shape[2:]
                  )
              )
              + 1e-10
          )
          out_sum_shape = out_sum.shape[1:]
          out_sum = out_sum.reshape(ss + out_sum_shape)
      elif model_type == "regressor":
          out_sum = predict_fn(
              summ.reshape(
                  (summ.shape[0] * summ.shape[1],) + summ.shape[2:]
              )
          )
          out_sum_shape = out_sum.shape[1:]
          if len(out_sum.shape) == 1:
              out_sum = out_sum.reshape(ss + (1,))
          else:
              out_sum = out_sum.reshape(ss + 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], diff.shape[1], -1), axis=2
      ).mean(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 3 for calling a global function with example usage: _calculate_pairwise_linearity(predict_fn, x, input_shape, X_samples, model_type, alphas) and returns: linearity_score