|
| | GeometricCluster ()=default |
| |
| bool | fit (const Eigen::Ref< const Eigen::MatrixXd > &points, size_t nClusters, std::default_random_engine &rng, size_t max_iter=1000) |
| | Fits the K-means clustering model on the given dataset.
|
| |
| const size_t & | operator[] (size_t i) const |
| | Accesses the cluster label of the i-th point.
|
| |
| Eigen::VectorXd | centroid (size_t i) const |
| | Returns the centroid of the i-th cluster.
|
| |
| size_t | nClusters () const |
| | Returns the number of clusters.
|
| |
| size_t | nPoints () const |
| | Returns the number of data points.
|
| |
| size_t | dim () const |
| | Returns the dimension of the data.
|
| |
| const Eigen::VectorXs & | getLabels () const |
| | Returns the cluster assignments vector.
|
| |
Implements a standard k-means clustering algorithm.
Mathematical Formulation K-means partitions \(N\) observations \(\{\mathbf{x}_1, \dots, \mathbf{x}_N\}\) where \(\mathbf{x}_j \in \mathbb{R}^d\) into \(K\) clusters \(\mathbf{S} = \{S_1, \dots, S_K\}\) to minimize the within-cluster sum of squares (WCSS):
\[
\arg\min_{\mathbf{S}} \sum_{i=1}^K \sum_{\mathbf{x} \in S_i} \|\mathbf{x} - \boldsymbol{\mu}_i\|_2^2
\]
where \(\boldsymbol{\mu}_i\) is the centroid of the points in \(S_i\).
Implementation Algorithm
- Initialize centroids \(\boldsymbol{\mu}_i\) by randomly selecting \(K\) distinct data points.
- Assign each point \(\mathbf{x}_j\) to the cluster with the nearest centroid:
\[
S_i^{(t)} = \left\{ \mathbf{x}_j : \|\mathbf{x}_j - \boldsymbol{\mu}_i^{(t)}\|_2 \le \|\mathbf{x}_j - \boldsymbol{\mu}_m^{(t)}\|_2 \ \forall m=1,\dots,K \right\}
\]
- Update the centroids to be the arithmetic mean of all points in that cluster:
\[
\boldsymbol{\mu}_i^{(t+1)} = \frac{1}{|S_i^{(t)}|} \sum_{\mathbf{x}_j \in S_i^{(t)}} \mathbf{x}_j
\]
- Iterate steps 2-3 until convergence (centroids no longer change or
max_iter is reached).