Datalens.ai
← All algorithms

DISTANCE

directed_hausdorff

Compute the directed Hausdorff distance between two 2-D arrays.

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 directed_hausdorff 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 directed_hausdorff.
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 directed_hausdorff
# A sequence of daily observations is treated as three 2-D locations.
a_points = np.stack([left, np.arange(len(left))], axis=1)
b_points = np.stack([right, np.arange(len(right))], axis=1)
result = directed_hausdorff(a_points, b_points, seed=0)[0]

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