Datalens.ai
← All algorithms

DISTANCE

cdist

Compute distance between each pair of the two collections of inputs.

Example of usage

Compare two named customer profiles and assert the computed score.

Domains of usage

Customer segmentation · Behavioural analytics · Similarity search

Usage / limitations

+Use when: Compare customer profiles using the cdist metric; choose it when its assumptions fit your features.

−Limitations: Feature scales and representations influence the result; compare only appropriately prepared profiles.

Mathematical formula

Distance or similarity computed on aligned feature vectors; see Python implementation.

This metric returns a distance or similarity computed from aligned feature vectors. Interpret its scale according to the metric definition.

Sample dataShow
"{'variable': 'customer_signals', 'domain': 'Retail customer engagement analytics', 'feature_names': ['weekday_orders', 'weekend_orders', 'return_visits'], 'customer_A': {'weekday_orders': 12.0, 'weekend_orders': 5.0, 'return_visits': 3.0}, 'customer_B': {'weekday_orders': 9.0, 'weekend_orders': 8.0, 'return_visits': 4.0}}"

Code example (Python)

# DataLens.ai example
# Domain: Retail customer engagement analytics
# Compare two labelled customer profiles using cdist.
import numpy as np

# Read the feature values in a consistent order.
left = np.array([customer_signals["customer_A"][name] for name in feature_names], dtype=float)
right = np.array([customer_signals["customer_B"][name] for name in feature_names], dtype=float)

# Calculate the requested distance or similarity.
from scipy.spatial.distance import cdist
result = cdist(left[None, :], right[None, :], metric="euclidean")

# Regression check against the expected result for this dataset.
observed = float(np.asarray(result).ravel()[0])
assert np.isclose(observed, 4.358898943541, rtol=1e-8, atol=1e-9), (observed, 4.358898943541)
print("Expected output verified")