Statistical model reference
K-Means clustering
Review when to use this method, its data requirements, implementation patterns, and interpretation guidance.
Description
An unsupervised machine learning technique that identifies natural groupings or clusters within data based on similarity measures. It organizes observations into homogeneous groups where members are similar to each other but different from those in other clusters, making it valuable for market segmentation, pattern recognition, image processing, and identifying distinct subgroups in heterogeneous populations.
Use Cases
- clustering
- segmentation
Requirements
- Sample Size: medium, large
- Missing Data: none, random
- Data Distribution: normal, non_normal
- Relationship Type: non_linear
Variable Types
Dependent Variables
Independent Variables
- continuous
Implementation
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3)
model.fit(X)
labels = model.labels_
Documentation
model <- kmeans(df, centers=3)
labels <- model$cluster
Documentation
# K-Means Clustering implementation for spss
# Code available in professional versions
Documentation
# K-Means Clustering implementation for sas
# Code available in professional versions
Documentation
# K-Means Clustering implementation for stata
# Code available in professional versions
Documentation
Synthetic Data Example
A dataset suitable for K-Means Clustering analysis
R Code for Data Generation and Analysis
# Generate synthetic data for this model type
set.seed(123)
n <- 100 # sample size
# Generate data
# ...specific code for this model...
# Descriptive statistics
# ...specific code for this model...
# Visualization
# ...specific code for this model...
# Model fitting
# ...specific code for this model...
# Model evaluation
# ...specific code for this model...
Copy this code into your R environment to generate synthetic data and perform analysis with this model.
Expected Analysis Results
Console Output
> # Fit clustering model
> model <- kmeans(df, centers = 3, nstart = 25)
> # Examine cluster sizes
> table(model$cluster)
1 2 3
42 68 40
> # Cluster centers
> model$centers
x y z
1 2.36743 6.54327 8.923145
2 9.46725 4.23844 13.42371
3 -1.56824 9.31278 7.651242
> # Within-cluster sum of squares
> model$withinss
[1] 126.4562 153.4781 84.3471
> # Visualization of clusters
> library(ggplot2)
> ggplot(df, aes(x = x, y = y, color = factor(model$cluster))) +
+ geom_point() +
+ labs(title = "K-means Clustering Results",
+ color = "Cluster")
> # Silhouette score to evaluate clustering quality
> library(cluster)
> sil <- silhouette(model$cluster, dist(df))
> summary(sil)
Silhouette of 150 units in 3 clusters:
Cluster sizes and average silhouette widths:
42 68 40
0.7257432 0.5683210 0.6824531
Individual silhouette widths:
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.3271 0.5406 0.6534 0.6398 0.7542 0.8763
These results are from running the R code on synthetic data. Your actual results may vary depending on your data.
Interpretation Guide
Need help interpreting the results of your K-Means clustering analysis? Our comprehensive interpretation guide explains:
- How to read and understand model outputs
- Interpreting coefficients and effect sizes correctly
- Understanding diagnostic plots and visualizations
- Common pitfalls and how to avoid them
- Making valid conclusions from your analysis