DISTANCE
jensenshannon
Compute the Jensen-Shannon distance (metric) between.
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 jensenshannon metric; choose it when its assumptions fit your features.
−Limitations: Feature scales and representations influence the result; compare only appropriately prepared profiles.
Mathematical formula
JS distance = √(½ KL(P‖M) + ½ KL(Q‖M)), M=(P+Q)/2
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': 'Customer traffic distribution comparison', 'feature_names': ['mobile', 'desktop', 'tablet'], 'customer_A': {'mobile': 0.6, 'desktop': 0.3, 'tablet': 0.1}, 'customer_B': {'mobile': 0.3, 'desktop': 0.4, 'tablet': 0.3}}"
Code example (Python)
import numpy as np
customer_signals = {'variable': 'customer_signals', 'domain': 'Customer traffic distribution comparison', 'feature_names': ['mobile', 'desktop', 'tablet'], 'customer_A': {'mobile': 0.6, 'desktop': 0.3, 'tablet': 0.1}, 'customer_B': {'mobile': 0.3, 'desktop': 0.4, 'tablet': 0.3}}
feature_names = customer_signals["feature_names"]
# DataLens.ai example
# Domain: Customer traffic distribution comparison
# Compare two labelled customer profiles using jensenshannon.
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 jensenshannon
result = jensenshannon(left, right)
# Regression check against the expected result for this dataset.
observed = float(np.asarray(result).ravel()[0])
assert np.isclose(observed, 0.23501263638, rtol=1e-8, atol=1e-9), (observed, 0.23501263638)
print("Expected output verified")
# DataLens.ai example
# Domain: Customer traffic distribution comparison
# Compare two labelled customer profiles using jensenshannon.
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 jensenshannon
result = jensenshannon(left, right)
# Regression check against the expected result for this dataset.
observed = float(np.asarray(result).ravel()[0])
assert np.isclose(observed, 0.23501263638, rtol=1e-8, atol=1e-9), (observed, 0.23501263638)
print("Expected output verified")