Datalens.ai
← All algorithms

DISTANCE

minkowski

Compute the Minkowski distance between two 1-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 minkowski metric; choose it when its assumptions fit your features.

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

Mathematical formula

dₚ(x,y) = (Σᵢ |xᵢ − yᵢ|ᵖ)^(1/p)

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 minkowski.
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 minkowski
result = minkowski(left, right, p=3)

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