Clustering Kmeans#
kmeans
#
| CLASS | DESCRIPTION |
|---|---|
KMeans |
K-Means clustering with k-means++ initialization. |
| FUNCTION | DESCRIPTION |
|---|---|
kmeans |
Perform k-means clustering (functional interface). |
Classes#
KMeans(n_states: int, init: Literal['k-means++', 'random'] = 'k-means++', n_init: int = 1, n_iter: int = 100, tol: float = 0.0001, device: torch.device | str | None = None, dtype: torch.dtype = torch.float32)
#
Bases: Estimator
K-Means clustering with k-means++ initialization.
Implements the standard k-means clustering algorithm with support for multiple initializations and both random and k-means++ initialization strategies.
| PARAMETER | DESCRIPTION |
|---|---|
n_states
|
Number of states (clusters) to form. Must be at least 1.
TYPE:
|
init
|
Initialization method for cluster centers.
TYPE:
|
n_init
|
Number of random initializations to try.
TYPE:
|
n_iter
|
Maximum number of iterations for a single run.
TYPE:
|
tol
|
Relative tolerance for convergence.
TYPE:
|
device
|
Device to run computations on. If None, automatically selects CUDA if available, otherwise CPU.
TYPE:
|
dtype
|
Data type to use for computations.
TYPE:
|
Properties
means : torch.Tensor Cluster centers of shape (n_states, in_features).
labels : torch.Tensor Labels of each training sample indicating cluster assignment of shape (n_samples,).
inertia : float Sum of squared distances from samples to their closest cluster center.
Examples:
Basic K-Means clustering:
>>> kmeans = KMeans(n_states=3)
>>> X = torch.randn(100, 2)
>>> labels = kmeans.fit_predict(X)
>>> labels.shape
torch.Size([100])
Multiple initializations:
>>> kmeans = KMeans(n_states=3, n_init=10)
>>> kmeans.fit(X)
>>> print(f"Best inertia: {kmeans.inertia:.2f}")
| METHOD | DESCRIPTION |
|---|---|
fit |
Fit k-means clustering to input data. |
predict |
Predict cluster labels for input data. |
fit_predict |
Fit k-means and predict cluster labels. |
score |
Compute the negative inertia (higher is better). |
get_results |
Get all fitted parameters as a dictionary. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
means |
Cluster centers of shape (n_states, in_features).
TYPE:
|
labels |
Training sample labels of shape (n_samples,).
TYPE:
|
inertia |
Sum of squared distances to nearest cluster center.
TYPE:
|
Source code in spectre/clustering/kmeans.py
Attributes#
means: torch.Tensor
property
#
Cluster centers of shape (n_states, in_features).
labels: torch.Tensor
property
#
Training sample labels of shape (n_samples,).
inertia: torch.Tensor
property
#
Sum of squared distances to nearest cluster center.
Functions#
fit(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> KMeans
#
Fit k-means clustering to input data.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples,). Weights are used to compute weighted cluster centers and weighted inertia. Weights are automatically normalized to sum to n_samples to preserve effective sample size. If None, all samples are weighted equally.
TYPE:
|
target
|
Target values. Not supported.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
KMeans
|
Returns self for method chaining. |
Source code in spectre/clustering/kmeans.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
predict(X: torch.Tensor) -> torch.Tensor
#
Predict cluster labels for input data.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Predicted cluster labels of shape (n_samples,). |
Source code in spectre/clustering/kmeans.py
fit_predict(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Fit k-means and predict cluster labels.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples,). Weights are used to compute weighted cluster centers and weighted inertia. If None, all samples are weighted equally.
TYPE:
|
target
|
Target values. Not supported.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Predicted cluster labels of shape (n_samples,). |
Source code in spectre/clustering/kmeans.py
score(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> float
#
Compute the negative inertia (higher is better).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples,). If provided, computes weighted inertia.
TYPE:
|
target
|
Target values. Not supported.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Negative inertia score (higher values indicate better clustering). |
Source code in spectre/clustering/kmeans.py
get_results() -> dict[str, torch.Tensor]
#
Get all fitted parameters as a dictionary.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Dictionary containing computed results. |
Source code in spectre/clustering/kmeans.py
Functions#
kmeans(X: torch.Tensor, *, n_states: int, init: Literal['k-means++', 'random'] = 'k-means++', n_init: int = 1, n_iter: int = 100, tol: float = 0.0001, weights: torch.Tensor | None = None, device: torch.device | str | None = None, dtype: torch.dtype = torch.float32) -> dict[str, torch.Tensor | float | list]
#
Perform k-means clustering (functional interface).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
n_states
|
Number of states (clusters) to form.
TYPE:
|
init
|
Initialization method for cluster centers.
TYPE:
|
n_init
|
Number of random initializations to try.
TYPE:
|
n_iter
|
Maximum number of iterations for a single run.
TYPE:
|
tol
|
Relative tolerance for convergence.
TYPE:
|
weights
|
Sample weights of shape (n_samples,). Weights are used to compute weighted cluster centers and weighted inertia. If None, all samples are weighted equally.
TYPE:
|
device
|
Device on which to perform computations.
TYPE:
|
dtype
|
Data type for computations.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor | float | list]
|
Dictionary with clustering results. |