Kmeans
Module: kmeans.py
This module provides a K-means clustering implementation using scikit-learn's MiniBatchKMeans. The Kmeans class inherits from a generic Clustering base class and offers: - Option for the user to input a custom number of clusters - Option for Automatic selection of the optimal number of clusters via silhouette or elbow methods
Classes:
Name | Description |
---|---|
Kmeans |
K-Means clustering with automatic parameter selection and optional user override. |
Dependencies
- numpy
- sklearn.cluster.MiniBatchKMeans
- sklearn.metrics.silhouette_score
- base.py (Clustering)
Key Features
- Scans [2..max_k] to find the best cluster count using silhouette or elbow
- Final cluster count stored in
optimal_k
, with a fitted model and labels - Inherits from the base
Clustering
for consistent plotting and metric computation
Version Info
- 28/Dec/2024: Initial version
Kmeans
Bases: Clustering
K-Means clustering with optional user-defined 'n_clusters' or automatic selection.
Attributes:
Name | Type | Description |
---|---|---|
n_clusters |
Optional[int]
|
If provided, the class will skip automatic selection and use this number of clusters. |
max_k |
int
|
Maximum number of clusters to consider for automatic selection if n_clusters is None. |
labels |
Optional[ndarray]
|
Cluster labels for each data point after fitting. |
n_clusters_ |
Optional[int]
|
The actual number of clusters used by the final fitted model. |
inertia_ |
Optional[float]
|
The final inertia (sum of squared distances to the closest centroid). |
cluster_centers_ |
Optional[ndarray]
|
Coordinates of cluster centers in the final fitted model. |
Source code in scirex/core/ml/unsupervised/clustering/kmeans.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
__init__(n_clusters=None, max_k=10)
Initialize the Kmeans clustering class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_clusters
|
Optional[int]
|
User-defined number of clusters when provided, the algorithm will ignore automatic selection and directly use 'n_clusters'. Defaults to None. |
None
|
max_k
|
Optional[int]
|
Maximum number of clusters to try for automatic selection if n_clusters is None. Defaults to 10. |
10
|
Source code in scirex/core/ml/unsupervised/clustering/kmeans.py
fit(X)
Fit the K-Means model to the data. If 'n_clusters' is set by the user, it uses that directly. Otherwise, it performs automatic selection of the optimal number of clusters using both the silhouette and elbow methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
Scaled feature matrix of shape (n_samples, n_features). |
required |
Source code in scirex/core/ml/unsupervised/clustering/kmeans.py
get_model_params()
Retrieve key parameters and results from the fitted K-Means model.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: - max_k (int): Maximum clusters originally specified for auto-selection - n_clusters (int): The actual number of clusters used in the final model - inertia_ (float): Final within-cluster sum of squares (inertia) - cluster_centers_ (Optional[List[List[float]]]): Cluster centers as a list of lists |