KMeans
Module: strata.cluster • Kind: struct • Traits: Clusterer, Copyable, Movable
Source: strata/cluster/kmeans.mojo
struct KMeans[compute_dtype: DType = DType.float64](Clusterer, Copyable, Movable)from strata.cluster import KMeansK-Means clustering using Lloyd’s or Elkan’s algorithm.
Clusters observations into disjoint geometric partitions by minimizing within-cluster inertia (sum-of-squared Euclidean distances):
Parameters (Compile-Time)
Section titled “Parameters (Compile-Time)”| Parameter | Description |
|---|---|
compute_dtype |
Computational precision data type. Default DType.float64. |
Constructors
Section titled “Constructors”def __init__( out self, n_clusters: Int = 8, init: String = "k-means++", n_init: Int = 10, max_iter: Int = 300, tol: Float64 = 1e-4, algorithm: String = "lloyd", random_state: Int = 42,)Initialize the KMeans estimator.
| Argument | Type | Description |
|---|---|---|
n_clusters |
Int |
Number of clusters to form. Default 8. |
init |
String |
Centroid initialization strategy (‘k-means++’, ‘random’). Default ‘k-means++’. |
n_init |
Int |
Number of initializations to run. Default 10. |
max_iter |
Int |
Maximum iterations per run. Default 300. |
tol |
Float64 |
Convergence tolerance threshold. Default 1e-4. |
algorithm |
String |
Algorithm variant (‘lloyd’). Default ‘lloyd’. |
random_state |
Int |
PRNG seed for deterministic centroid placement. Default 42. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
cluster_centers_ |
Coordinates of cluster centers matrix of shape . |
labels_ |
Labels of each point vector of length . |
inertia_ |
Sum of squared distances of samples to their closest cluster center. |
n_iter_ |
Number of iterations run. |
n_features_in_ |
Number of features seen during fit. |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
KMeans.fit() |
Fits cluster centroids on Dataset feature records. |
KMeans.predict() |
Predicts closest cluster assignments for a Dataset container. |
KMeans.fit_predict() |
Fit the estimator to X and return predicted cluster labels. |
KMeans.transform() |
Transform X into the cluster-distance space. |
KMeans.fit_transform() |
Fit the estimator to X and return the transformed output. |
KMeans.score() |
Compute the estimator objective score on X. |
Method Details
Section titled “Method Details”KMeans.fit()
Section titled “KMeans.fit()”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) |
KMeans.predict()
Section titled “KMeans.predict()”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]
KMeans.fit_predict()
Section titled “KMeans.fit_predict()”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]
KMeans.transform()
Section titled “KMeans.transform()”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]
KMeans.fit_transform()
Section titled “KMeans.fit_transform()”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]
KMeans.score()
Section titled “KMeans.score()”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]
Section titled “Returns: Scalar[Self.compute_dtype]”Example
Section titled “Example”from strata.cluster import KMeansfrom strata.core import Matrix
var kmeans = KMeans[DType.float64](n_clusters=3, init="k-means++")kmeans.fit(X_data)var labels = kmeans.predict(X_data)var distances = kmeans.transform(X_data)