Available Metrics#

class cxplain.metrics.Metric#

Base class for metrics used by different explainers.

abstract static calculate(x: NDArray[Any, Any], y: NDArray[Any, Any])#

Abstract method to caclculate the distance between x and y.

Parameters:
  • x – Input to distance calculation.

  • y – Input to distance calculation.

abstract static calculate_gradient(x: NDArray[Any, Any], y: NDArray[Any, Any])#

Abstract method to caclculate the pointwise gradient of the metric with respect to x.

Parameters:
  • x – Input to metric calculation.

  • y – Input to metric calculation.

class cxplain.metrics.ManhattenMetric#

A class for calculating pointwise Manhattan distance and its gradient between two NumPy arrays.

static calculate(x: NDArray[Any, Any], y: NDArray[Any, Any]) NDArray[Any, Any]#

Calculate pointwise Manhattan distance between x and y.

Parameters:
  • x – Input to distance calculation.

  • y – Input to distance calculation.

Returns:

Pointwise Manhattan distance.

Return type:

NDArray

Example:

>>> x = np.array([1, 2, 3])
>>> y = np.array([4, 5, 6])
>>> result = ManhattenMetric.calculate(x, y)
>>> print(result)  # Output: array([3, 3, 3])
static calculate_gradient(x: NDArray[Any, Any], y: NDArray[Any, Any]) NDArray[Any, Any]#

Calculate the pointwise gradient between x and y of the Manhatten distance with respect to x.

Parameters:
  • x – Input for gradient calculation.

  • y – Input for gradient calculation.

Returns:

Pointwise gradient.

Return type:

NDArray

Example:

>>> x = np.array([1.0, 2.0, 3.0])
>>> y = np.array([4.0, 5.0, 6.0])
>>> gradient = ManhattenMetric.calculate_gradient(x, y)
>>> print(gradient)  # Output: array([-1., -1., -1.])
class cxplain.metrics.EuclideanMetric#

A class for calculating pointwise Euclidean distance and its gradient between two NumPy arrays.

static calculate(x: NDArray[Any, Any], y: NDArray[Any, Any]) NDArray[Any, Any]#

Calculate pointwise Euclidean distance between x and y.

Parameters:
  • x – Input to distance calculation.

  • y – Input to distance calculation.

Returns:

Pointwise Euclidean distance.

Return type:

NDArray

Example: >>> x = np.array([1.0, 2.0, 3.0]) >>> y = np.array([4.0, 5.0, 6.0]) >>> # Calculate pointwise Euclidean distance >>> distance = EuclideanMetric.calculate(x, y) >>> print(distance) # Output: array([9., 9., 9.])

static calculate_gradient(x: NDArray[Any, Any], y: NDArray[Any, Any]) NDArray[Any, Any]#

Calculate the gradient of pointwise Euclidean distance between x and y with respect to x.

Parameters:
  • x – Input for gradient calculation.

  • y – Input for gradient calculation.

Returns:

Pointwise Euclidean gradient.

Return type:

NDArray

Example:

>>> x = np.array([1.0, 2.0, 3.0])
>>> y = np.array([4.0, 5.0, 6.0])
>>> # Calculate pointwise Euclidean gradient
>>> gradient = EuclideanMetric.calculate_gradient(x, y)
>>> print(gradient)  # Output: array([-6., -6., -6.])
cxplain.metrics.get_distance_metric(metric_name: str) Type[Metric]#

Factory method for distance metrices.

Parameters:

metric_name – Name of the distance metric that should be applied.

Raises:

MetricNotImplementedError – Raised if desired metric is not yet implemented.

Returns:

The desired distance metric.

Return type:

Type[Metric]