CLUSTERING
AgglomerativeClustering
Agglomerative Clustering.
Example of usage
Python API reference included; executable data demonstration pending review.
Usage / limitations
+Use when: Discover latent groups.
−Limitations: Check input requirements, mathematical assumptions and version-specific behaviour before using.
Mathematical formula
See method documentation: implementation and assumptions vary.
Sample dataShow
{
"variable": "customers",
"domain": "Customer engagement segmentation",
"feature_names": [
"monthly_spend_gbp",
"visits_per_month"
],
"records": [
{
"customer_id": "C01",
"monthly_spend_gbp": 20,
"visits_per_month": 2
},
{
"customer_id": "C02",
"monthly_spend_gbp": 22,
"visits_per_month": 3
},
{
"customer_id": "C03",
"monthly_spend_gbp": 18,
"visits_per_month": 2
},
{
"customer_id": "C04",
"monthly_spend_gbp": 80,
"visits_per_month": 12
},
{
"customer_id": "C05",
"monthly_spend_gbp": 84,
"visits_per_month": 13
},
{
"customer_id": "C06",
"monthly_spend_gbp": 78,
"visits_per_month": 11
}
]
}
Code example (Python)
# DataLens.ai example — sample data: customer engagement cohorts
customers = {'variable':'customers', 'domain':'Customer engagement segmentation', 'feature_names':['monthly_spend_gbp','visits_per_month'], 'records':[
{'customer_id':'C01','monthly_spend_gbp':20,'visits_per_month':2},
{'customer_id':'C02','monthly_spend_gbp':22,'visits_per_month':3},
{'customer_id':'C03','monthly_spend_gbp':18,'visits_per_month':2},
{'customer_id':'C04','monthly_spend_gbp':80,'visits_per_month':12},
{'customer_id':'C05','monthly_spend_gbp':84,'visits_per_month':13},
{'customer_id':'C06','monthly_spend_gbp':78,'visits_per_month':11}]}
# DataLens.ai example
# Domain: Customer engagement segmentation.
# The sample data above are included automatically when copied.
import numpy as np
from sklearn.cluster import AgglomerativeClustering
# Build the numeric feature matrix from named customer attributes.
X = np.array([[record[feature] for feature in customers["feature_names"]] for record in customers["records"]], dtype=float)
# Fit or evaluate the requested clustering technique.
labels = AgglomerativeClustering(n_clusters=2).fit_predict(X)
assert len(set(labels)) == 2 and labels[0] == labels[1] == labels[2] and labels[3] == labels[4] == labels[5] and labels[0] != labels[3]
# All checks passed; no exception was raised.
print("Expected output verified")
# DataLens.ai example
# Domain: Customer engagement segmentation.
# The sample data above are included automatically when copied.
import numpy as np
from sklearn.cluster import AgglomerativeClustering
# Build the numeric feature matrix from named customer attributes.
X = np.array([[record[feature] for feature in customers["feature_names"]] for record in customers["records"]], dtype=float)
# Fit or evaluate the requested clustering technique.
labels = AgglomerativeClustering(n_clusters=2).fit_predict(X)
assert len(set(labels)) == 2 and labels[0] == labels[1] == labels[2] and labels[3] == labels[4] == labels[5] and labels[0] != labels[3]
# All checks passed; no exception was raised.
print("Expected output verified")