code: def linearity_measure(
      predict_fn: Callable,
      x: np.ndarray,
      feature_range: Optional[Union[List, np.ndarray]] = None,
      method: str = "grid",
      X_train: Optional[np.ndarray] = None,
      epsilon: float = 0.04,
      nb_samples: int = 10,
      res: int = 100,
      alphas: Optional[np.ndarray] = None,
      agg: str = "global",
      model_type: str = "classifier",
  ) -> np.ndarray:
      """Calculate the linearity measure of the model around an instance of interest x.
  
      Parameters
      ----------
      predict_fn
          Predict function.
      x
          Instance of interest.
      feature_range
          Array with min and max values for each feature.
      method
          Method for sampling. Supported values: ``'knn'`` | ``'grid'``.
      X_train
          Training set.
      epsilon
          Size of the sampling region as a percentage of the feature range.
      nb_samples
          Number of samples to generate.
      res
          Resolution of the grid. Number of intervals in which the features range is discretized.
      alphas
          Coefficients in the superposition.
      agg
          Aggregation method. Supported values: ``'global'`` | ``'pairwise'``.
      model_type
          Type of task. Supported values: ``'regressor'`` | ``'classifier'``.
  
      Returns
      -------
      Linearity measure.
  
      """
      if method == "knn":
          assert (
              X_train is not None
          ), " Method 'knn' requires X_train != None"
          lin = _linearity_measure(
              predict_fn,
              x,
              X_train=X_train,
              feature_range=None,
              method=method,
              nb_samples=nb_samples,
              res=res,
              epsilon=epsilon,
              alphas=alphas,
              model_type=model_type,
              agg=agg,
          )
      elif method == "grid":
          assert feature_range is not None or X_train is not None, (
              "Method 'grid' requires "
              "feature_range != None or X_train != None"
          )
          if X_train is not None and feature_range is None:
              feature_range = infer_feature_range(
                  X_train
              )  # infer from dataset
          elif feature_range is not None:
              feature_range = np.asarray(feature_range)
  
          lin = _linearity_measure(
              predict_fn,
              x,
              X_train=None,
              feature_range=feature_range,
              method=method,
              nb_samples=nb_samples,
              res=res,
              epsilon=epsilon,
              alphas=alphas,
              model_type=model_type,
              agg=agg,
          )
      else:
          raise ValueError(
              'Method not understood. Supported methods: "knn", "grid"'
          )
  
      return lin
  
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 8 for calling a global function with example usage: linearity_measure(predict_fn, x, feature_range, method, X_train, epsilon, nb_samples, res, alphas, agg, model_type) and returns: lin