Bisecting K-means¶
Algorithm¶
Bisecting K-means starts with all observations in one cluster, then uses two-way K-means splits to reduce total within-cluster squared error. The splits form a binary cluster tree.
Inputs and limitations¶
- Pass a non-empty rectangular list of numeric observations and
1 <= k <= len(samples). - The implementation uses Euclidean distance and random K-means initialization.
- Splitting stops when no candidate reduces the error, so fewer than
kclusters may result. predictaccepts a list of samples.- Tree plotting needs the optional
toyml[plot]dependencies.
Minimal example¶
from toyml.clustering import BisectingKMeans
samples = [[0.0], [0.1], [4.0], [4.1]]
model = BisectingKMeans(k=2)
print(model.fit_predict(samples))
print(model.cluster_tree_.get_clusters())
API¶
toyml.clustering.bisecting_kmeans.BisectingKMeans
dataclass
¶
BisectingKMeans(k: int)
Bisecting K-means algorithm.
A divisive hierarchical clustering algorithm using top-down K-means splits.
Examples:
>>> from toyml.clustering import BisectingKMeans
>>> dataset = [[1.0, 1.0], [1.0, 2.0], [2.0, 1.0], [10.0, 1.0], [10.0, 2.0], [11.0, 1.0]]
>>> model = BisectingKMeans(k=3)
>>> labels = model.fit_predict(dataset)
>>> clusters = model.cluster_tree_.get_clusters()
References
- Harrington
- Tan
See Also
- K-means algorithm: toyml.clustering.kmeans
cluster_tree_
class-attribute
instance-attribute
¶
cluster_tree_: ClusterTree = field(default_factory=ClusterTree, init=False)
The cluster tree
labels_
class-attribute
instance-attribute
¶
labels_: list[int] = field(default_factory=list, init=False)
The cluster labels of the dataset.
fit
¶
fit(dataset: list[list[float]]) -> BisectingKMeans
Fit the dataset with Bisecting K-means algorithm.
| PARAMETER | DESCRIPTION |
|---|---|
dataset
|
The set of data points for clustering.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BisectingKMeans
|
self. |
Source code in src/toyml/clustering/bisecting_kmeans.py
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 | |
fit_predict
¶
fit_predict(dataset: list[list[float]]) -> list[int]
Fit and predict the cluster label of the dataset.
| PARAMETER | DESCRIPTION |
|---|---|
dataset
|
The set of data points for clustering.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[int]
|
Cluster labels of the dataset samples. |
Source code in src/toyml/clustering/bisecting_kmeans.py
208 209 210 211 212 213 214 215 216 217 | |
predict
¶
predict(points: list[list[float]]) -> list[int]
Predict the cluster label of the given points.
| PARAMETER | DESCRIPTION |
|---|---|
points
|
A list of data points to predict.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[int]
|
A list of predicted cluster labels for the input points. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the model has not been fitted yet. |
Source code in src/toyml/clustering/bisecting_kmeans.py
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 | |