Skip to content

MiniBatchKMeans

Module: strata.cluster • Kind: struct • Traits: Clusterer, Copyable, Movable
Source: strata/cluster/minibatch_kmeans.mojo

struct MiniBatchKMeans[compute_dtype: DType = DType.float64](Clusterer, Copyable, Movable)
from strata.cluster import MiniBatchKMeans

Mini-Batch K-Means clustering algorithm.

Mini-Batch K-Means uses mini-batches of samples to reduce computation time while optimizing the same objective function as full-batch K-Means:

arg⁡min⁡C∑i=1Nmin⁡μj∈C∥xi−μj∥22\arg\min_{C} \sum_{i=1}^{N} \min_{\mu_j \in C} \|x_i - \mu_j\|_2^2
Parameter Description
compute_dtype Computational precision data type. Default DType.float64.

def __init__(
out self,
n_clusters: Int = 8,
init: String = "k-means++",
max_iter: Int = 100,
batch_size: Int = 1024,
tol: Float64 = 0.0,
max_no_improvement: Int = 10,
n_init: Int = 3,
reassignment_ratio: Float64 = 0.01,
random_state: Int = 42,
)
Argument Type Description
n_clusters Int The number of clusters to form as well as the number of centroids to generate. Default 8.
init String Method for initialization (‘k-means++’, ‘random’). Default ‘k-means++’.
max_iter Int Maximum number of mini-batch iterations. Default 100.
batch_size Int Size of mini-batches drawn per iteration. Default 1024.
tol Float64 Tolerance threshold for early stopping based on center shift. Default 1e-4.
max_no_improvement Int Early stopping iteration count without inertia improvement. Default 10.
n_init Int Number of random initialization attempts. Default 3.
reassignment_ratio Float64 Fraction of max count threshold for center reassignment. Default 0.01.
random_state Int PRNG seed for reproducible centroid initializations. Default 42.

Attribute Description
cluster_centers_ Coordinates of cluster centers matrix of shape (K,D)(K, D).
labels_ Labels of each point vector of length NN.
inertia_ Sum of squared distances of samples to their closest cluster center.
n_iter_ Number of iterations run during fitting.
n_steps_ Total mini-batch update steps performed.
n_features_in_ Number of features seen during fit.
is_fitted Boolean flag indicating if estimator has been fitted.

Method Description
MiniBatchKMeans.partial_fit() Incrementally update the estimator on a mini-batch of samples.
MiniBatchKMeans.fit() Fits cluster centroids on Dataset feature records.
MiniBatchKMeans.predict() Predicts closest cluster assignments for a Dataset container.
MiniBatchKMeans.fit_predict() Fit the estimator to X and return predicted cluster labels.
MiniBatchKMeans.transform() Transform X into the cluster-distance space.
MiniBatchKMeans.fit_transform() Fit the estimator to X and return the transformed output.
MiniBatchKMeans.score() Compute the estimator objective score on X.

def partial_fit[in_dtype: DType](mut self, X: Matrix[in_dtype])

Incrementally update the estimator on a mini-batch of samples.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.

def fit[in_dtype: DType](mut self, X: Matrix[in_dtype])
def fit[feat_dtype: DType, target_dtype: DType](
mut self,
dataset: Dataset[feat_dtype, target_dtype],
)

Overload Note: This method supports multiple overloaded call signatures.

Fits cluster centroids on Dataset feature records.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding data records. (Overloaded alternative)

def predict[in_dtype: DType](self, X: Matrix[in_dtype]) -> List[Int]
def predict[feat_dtype: DType, target_dtype: DType](
self,
dataset: Dataset[feat_dtype, target_dtype],
) -> List[Int]

Overload Note: This method supports multiple overloaded call signatures.

Predicts closest cluster assignments for a Dataset container.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding data records. (Overloaded alternative)

Returns: List[Int]


def fit_predict[in_dtype: DType](mut self, X: Matrix[in_dtype]) -> List[Int]

Fit the estimator to X and return predicted cluster labels.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.

Returns: List[Int]


def transform[in_dtype: DType](
self,
X: Matrix[in_dtype],
) -> Matrix[in_dtype]

Transform X into the cluster-distance space.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.

Returns: Matrix[in_dtype]


def fit_transform[in_dtype: DType](
mut self,
X: Matrix[in_dtype],
) -> Matrix[in_dtype]

Fit the estimator to X and return the transformed output.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.

Returns: Matrix[in_dtype]


def score[in_dtype: DType](
self,
X: Matrix[in_dtype],
) -> Scalar[Self.compute_dtype]

Compute the estimator objective score on X.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.

Returns: Scalar[Self.compute_dtype]