CLUSTERING
K-Means
Partition observations into k groups around centroids.
Example of usage
Group customers by purchasing patterns.
Usage / limitations
+Use when: Explore compact clusters of numerical data.
−Limitations: Requires k and can be sensitive to scale and initialisation.
Mathematical formula
min sum(||x_i - centroid_assigned(i)||^2)
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 KMeans
# 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 = KMeans(n_clusters=2, random_state=0, n_init=10).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 KMeans
# 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 = KMeans(n_clusters=2, random_state=0, n_init=10).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")