KMeans¶
- class pycave.clustering.KMeans(num_clusters=1, *, init_strategy='kmeans++', convergence_tolerance=0.0001, batch_size=None, trainer_params=None)[source]¶
Bases:
ConfigurableBaseEstimator
[KMeansModel
],TransformerMixin
[Union
[ndarray
,Tensor
],Tensor
],PredictorMixin
[Union
[ndarray
,Tensor
],Tensor
]Model for clustering data into a predefined number of clusters. More information on K-means clustering is available on Wikipedia.
See also
PyTorch module for the K-Means model.
Configuration class for a K-Means model.
- Parameters:
num_clusters (int) -- The number of clusters.
init_strategy (KMeansInitStrategy) -- The strategy for initializing centroids.
convergence_tolerance (float) -- Training is conducted until the Frobenius norm of the change between cluster centroids falls below this threshold. The tolerance is multiplied by the average variance of the features.
batch_size (int | None) -- The batch size to use when fitting the model. If not provided, the full data will be used as a single batch. Set this if the full data does not fit into memory.
trainer_params (dict[str, Any] | None) --
Initialization parameters to use when initializing a PyTorch Lightning trainer. By default, it disables various stdout logs unless PyCave is configured to do verbose logging. Checkpointing and logging are disabled regardless of the log level. This estimator further sets the following overridable defaults:
max_epochs=300
Note
The number of epochs passed to the initializer only define the number of optimization epochs. Prior to that, initialization is run which may perform additional iterations through the data.
Methods
Fits the KMeans model on the provided data by running Lloyd's algorithm. |
|
Predicts the closest cluster for each item provided. |
|
Computes the average inertia of all the provided datapoints. |
|
Computes the inertia for each of the the provided datapoints. |
|
Transforms the provided data into the cluster-distance space. |
Inherited Methods
Clones the estimator without copying any fitted attributes. |
|
Fits the estimator using the provided data and subsequently predicts the labels for the data using the fitted estimator. |
|
Fits the estimator using the provided data and subsequently transforms the data using the fitted estimator. |
|
Returns the estimator's parameters as passed to the initializer. |
|
Loads the estimator and (if available) the fitted model. |
|
Loads the fitted attributes that are stored at the fitted path. |
|
Initializes this estimator by loading its parameters. |
|
Saves the estimator to the provided directory. |
|
Saves the fitted attributes of this estimator. |
|
Saves the parameters of this estimator. |
|
Sets the provided values on the estimator. |
|
Returns the trainer as configured by the estimator. |
Attributes
|
Returns the list of fitted attributes that ought to be saved and loaded. |
|
The fitted PyTorch module with all estimated parameters. |
|
A boolean indicating whether the model converged during training. |
|
The number of iterations the model was fitted for, excluding initialization. |
|
The mean squared distance of all datapoints to their closest cluster centers. |