# alibi_detect.utils.distance module¶

alibi_detect.utils.distance.abdm(X, cat_vars, cat_vars_bin={})[source]

Calculate the pair-wise distances between categories of a categorical variable using the Association-Based Distance Metric based on Le et al (2005). http://www.jaist.ac.jp/~bao/papers/N26.pdf

Parameters
• X (numpy.ndarray) – Batch of arrays.

• cat_vars (dict) – Dict with as keys the categorical columns and as optional values the number of categories per categorical variable.

• cat_vars_bin (dict) – Dict with as keys the binned numerical columns and as optional values the number of bins per variable.

Return type

dict

Returns

Dict with as keys the categorical columns and as values the pairwise distance matrix for the variable.

alibi_detect.utils.distance.cityblock_batch(X, y)[source]

Calculate the L1 distances between a batch of arrays X and an array of the same shape y.

Parameters
• X (numpy.ndarray) – Batch of arrays to calculate the distances from

• y (numpy.ndarray) – Array to calculate the distance to

Return type

numpy.ndarray

Returns

Array of distances from each array in X to y

alibi_detect.utils.distance.multidim_scaling(d_pair, n_components=2, use_metric=True, standardize_cat_vars=True, feature_range=None, smooth=1.0, center=True, update_feature_range=True)[source]

Apply multidimensional scaling to pairwise distance matrices.

Parameters
• d_pair (dict) – Dict with as keys the column index of the categorical variables and as values a pairwise distance matrix for the categories of the variable.

• n_components (int) – Number of dimensions in which to immerse the dissimilarities.

• use_metric (bool) – If True, perform metric MDS; otherwise, perform nonmetric MDS.

• standardize_cat_vars (bool) – Standardize numerical values of categorical variables if True.

• feature_range (Optional[tuple]) – Tuple with min and max ranges to allow for perturbed instances. Min and max ranges can be floats or numpy arrays with dimension (1 x nb of features) for feature-wise ranges.

• smooth (float) – Smoothing exponent between 0 and 1 for the distances. Lower values of l will smooth the difference in distance metric between different features.

• center (bool) – Whether to center the scaled distance measures. If False, the min distance for each feature except for the feature with the highest raw max distance will be the lower bound of the feature range, but the upper bound will be below the max feature range.

• update_feature_range (bool) – Update feature range with scaled values.

Return type
Returns

Dict with multidimensional scaled version of pairwise distance matrices.

alibi_detect.utils.distance.mvdm(X, y, cat_vars, alpha=1)[source]

Calculate the pair-wise distances between categories of a categorical variable using the Modified Value Difference Measure based on Cost et al (1993). https://link.springer.com/article/10.1023/A:1022664626993

Parameters
• X (numpy.ndarray) – Batch of arrays.

• y (numpy.ndarray) – Batch of labels or predictions.

• cat_vars (dict) – Dict with as keys the categorical columns and as optional values the number of categories per categorical variable.

• alpha (int) – Power of absolute difference between conditional probabilities.

Return type

Dict[Any, numpy.ndarray]

Returns

Dict with as keys the categorical columns and as values the pairwise distance matrix for the variable.

alibi_detect.utils.distance.relative_euclidean_distance(x, y, eps=1e-12, axis=-1)[source]

Relative Euclidean distance in TensorFlow.

Parameters
• x (tensorflow.Tensor) – Tensor used in distance computation.

• y (tensorflow.Tensor) – Tensor used in distance computation.

• eps (float) – Epsilon added to denominator for numerical stability.

• axis (int) – Axis used to compute distance.

Return type

tensorflow.Tensor

Returns

Tensor with relative Euclidean distance across specified axis.