Cluster features
K-means selection diagnostics and cluster membership features.
featurely.cluster
K-means cluster membership features.
Clustering partitions rows into groups; one-hot membership lets a linear model fit a separate intercept per group, and centroid distance adds a within-group gradient. Clustering uses only feature columns, so the derived features are target-free.
plot_kmeans_selection(df, features, k_range=None, random_state=315, sample_size=5000, title=None)
Plot inertia (elbow) and silhouette score across candidate k values.
Inertia always decreases with k, so we look for the elbow where the marginal gain flattens. Silhouette measures how well separated the clusters are; it is computed on a random subsample because the full pairwise calculation is quadratic in row count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input frame. |
required |
features
|
list[str]
|
Columns to cluster on; standard-scaled before fitting. |
required |
k_range
|
Iterable[int] | None
|
Candidate cluster counts; defaults to |
None
|
random_state
|
int
|
Seed for k-means initialization and subsampling. |
315
|
sample_size
|
int
|
Rows sampled for the silhouette calculation. |
5000
|
title
|
str | None
|
Optional figure title. |
None
|
Returns:
| Type | Description |
|---|---|
dict[int, float]
|
Mapping of k to silhouette score. |
Source code in src/featurely/cluster.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
compute_kmeans_features(df, features, k, prefix, one_hot=True, add_distance=True, random_state=315)
Return cluster membership and centroid distance candidates.
Features are standard-scaled before clustering so no single column dominates the distance metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input frame; not modified. |
required |
features
|
list[str]
|
Columns to cluster on. |
required |
k
|
int
|
Number of clusters. |
required |
prefix
|
str
|
Prefix for output column names. |
required |
one_hot
|
bool
|
When True, include one-hot membership columns named
|
True
|
add_distance
|
bool
|
When True, include |
True
|
random_state
|
int
|
Seed for k-means initialization. |
315
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A frame of cluster membership and distance candidate columns. |
Source code in src/featurely/cluster.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |