Clustering¶
base ¶
Abstract base class for clustering backends.
Clusterer ¶
Bases: ABC
Base interface for feature-space clustering algorithms.
Implementations wrap scikit-learn or custom clustering methods and expose
a uniform fit / predict API.
cluster_centers_
abstractmethod
property
¶
Return cluster centroids if available.
Returns:
| Type | Description |
|---|---|
ndarray or None
|
Array of shape |
model
abstractmethod
property
¶
Return the underlying fitted model object for serialization.
Returns:
| Type | Description |
|---|---|
object
|
Backend-specific model instance. |
fit
abstractmethod
¶
Fit the clusterer to feature data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix of shape |
required |
Returns:
| Type | Description |
|---|---|
Clusterer
|
Fitted clusterer instance ( |
predict
abstractmethod
¶
Assign cluster labels to feature data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Integer cluster labels of shape |
clustering ¶
Clustering backend factory and registry.
CLUSTERER_REGISTRY
module-attribute
¶
CLUSTERER_REGISTRY: Dict[str, Type[Clusterer]] = {'kmeans': SklearnKMeansClusterer, 'minibatch_kmeans': SklearnMiniBatchClusterer, 'regular_space': SklearnRegularSpaceClusterer}
SklearnKMeansClusterer ¶
Bases: Clusterer
Wrap sklearn.cluster.KMeans for AdaptivePy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_clusters
|
int
|
Number of clusters. |
required |
random_state
|
int or None
|
Random seed passed to KMeans. |
None
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to |
{}
|
fit ¶
Fit KMeans on the provided feature matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix of shape |
required |
Returns:
| Type | Description |
|---|---|
SklearnKMeansClusterer
|
Fitted clusterer. |
predict ¶
Predict cluster labels for X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Cluster labels. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
SklearnMiniBatchClusterer ¶
Bases: Clusterer
Wrap sklearn.cluster.MiniBatchKMeans for large datasets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_clusters
|
int
|
Number of clusters. |
required |
random_state
|
int or None
|
Random seed passed to MiniBatchKMeans. |
None
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to |
{}
|
cluster_centers_
property
¶
Return MiniBatchKMeans cluster centers.
fit ¶
Fit MiniBatchKMeans on the provided feature matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix of shape |
required |
Returns:
| Type | Description |
|---|---|
SklearnMiniBatchClusterer
|
Fitted clusterer. |
predict ¶
Predict cluster labels for X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Cluster labels. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
SklearnRegularSpaceClusterer ¶
SklearnRegularSpaceClusterer(min_dist: float, max_clusters: Optional[int] = None, random_state: Optional[int] = None)
Bases: Clusterer
Greedy regular-space clustering in feature space.
This implements a distance-threshold variant commonly used in MD analysis:
cluster seeds are chosen iteratively so that no two centers are closer than
min_dist. All frames are then assigned to their nearest center.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_dist
|
float
|
Minimum Euclidean distance between cluster centers. |
required |
max_clusters
|
int or None
|
Optional upper bound on the number of clusters. If |
None
|
random_state
|
int or None
|
Seed for shuffling frame order when selecting new centers. |
None
|
cluster_centers_
property
¶
Return regular-space cluster centers.
fit ¶
Select regular-space centers and assign all frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix of shape |
required |
Returns:
| Type | Description |
|---|---|
SklearnRegularSpaceClusterer
|
Fitted clusterer. |
predict ¶
Assign labels by nearest regular-space center.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Cluster labels. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
create_clusterer ¶
create_clusterer(method: str, n_clusters: int, random_state: int | None = None, params: Dict[str, Any] | None = None) -> Clusterer
Instantiate a registered clustering backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
Clustering method name ( |
required |
n_clusters
|
int
|
Target number of clusters (used by k-means variants; mapped to
|
required |
random_state
|
int or None
|
Random seed for reproducibility. |
None
|
params
|
dict or None
|
Additional backend-specific parameters. |
None
|
Returns:
| Type | Description |
|---|---|
Clusterer
|
Unfitted clusterer instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
fit_clusterer ¶
sklearn_kmeans ¶
KMeans clustering via scikit-learn.
SklearnKMeansClusterer ¶
Bases: Clusterer
Wrap sklearn.cluster.KMeans for AdaptivePy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_clusters
|
int
|
Number of clusters. |
required |
random_state
|
int or None
|
Random seed passed to KMeans. |
None
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to |
{}
|
fit ¶
Fit KMeans on the provided feature matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix of shape |
required |
Returns:
| Type | Description |
|---|---|
SklearnKMeansClusterer
|
Fitted clusterer. |
predict ¶
Predict cluster labels for X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Cluster labels. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
sklearn_minibatch ¶
MiniBatchKMeans clustering via scikit-learn.
SklearnMiniBatchClusterer ¶
Bases: Clusterer
Wrap sklearn.cluster.MiniBatchKMeans for large datasets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_clusters
|
int
|
Number of clusters. |
required |
random_state
|
int or None
|
Random seed passed to MiniBatchKMeans. |
None
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to |
{}
|
cluster_centers_
property
¶
Return MiniBatchKMeans cluster centers.
fit ¶
Fit MiniBatchKMeans on the provided feature matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix of shape |
required |
Returns:
| Type | Description |
|---|---|
SklearnMiniBatchClusterer
|
Fitted clusterer. |
predict ¶
Predict cluster labels for X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Cluster labels. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
regular_space ¶
Regular-space clustering for molecular dynamics feature data.
Frames are assigned to clusters such that each cluster center is at least
min_dist away from all previously selected centers (in feature space).
Remaining frames are assigned to the nearest center.
SklearnRegularSpaceClusterer ¶
SklearnRegularSpaceClusterer(min_dist: float, max_clusters: Optional[int] = None, random_state: Optional[int] = None)
Bases: Clusterer
Greedy regular-space clustering in feature space.
This implements a distance-threshold variant commonly used in MD analysis:
cluster seeds are chosen iteratively so that no two centers are closer than
min_dist. All frames are then assigned to their nearest center.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_dist
|
float
|
Minimum Euclidean distance between cluster centers. |
required |
max_clusters
|
int or None
|
Optional upper bound on the number of clusters. If |
None
|
random_state
|
int or None
|
Seed for shuffling frame order when selecting new centers. |
None
|
cluster_centers_
property
¶
Return regular-space cluster centers.
fit ¶
Select regular-space centers and assign all frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix of shape |
required |
Returns:
| Type | Description |
|---|---|
SklearnRegularSpaceClusterer
|
Fitted clusterer. |
predict ¶
Assign labels by nearest regular-space center.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Cluster labels. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |