A class implementing K-Means clustering algorithm.
A class implementing K-Means clustering algorithmThis class provides functionality for unsupervised clustering using the K-Means algorithm. It partitions data into k clusters by minimizing within-cluster sum of squares.
- Template Parameters
-
T | The data type for matrix elements (typically float or double) |
KMeans<double> kmeans(3);
Matrix<double> data = your_data_matrix;
kmeans.fit(data);
std::vector<int> labels = kmeans.predict(data);
#pragma once
#include <limits>
#include <random>
#include <vector>
#include "../utils/matrix.hpp"
template<typename T>
class KMeans {
public:
KMeans(size_t k, size_t max_iters = 300, T tol = 1e-4, int random_state = -1);
private:
size_t k_;
size_t max_iters_;
T tol_;
int random_state_;
T inertia_;
size_t n_iter_;
bool is_fitted_;
std::vector<int> assign_clusters(
const Matrix<T> &data)
const;
void update_centroids(
const Matrix<T> &data,
const std::vector<int> &labels);
T squared_distance(const std::vector<T> &point1, const std::vector<T> &point2) const;
};
}
T inertia() const
Get the within-cluster sum of squares (inertia)
std::vector< int > fit_predict(const Matrix< T > &data)
Fit the model and predict cluster labels.
std::vector< int > predict(const Matrix< T > &data) const
Predict cluster labels for the given data.
Matrix< T > cluster_centers() const
Get the cluster centroids.
size_t n_iter() const
Get the number of iterations performed.
void fit(const Matrix< T > &data)
Fit the K-Means model to the data.
Namespace containing traditional machine learning algorithms.
KMeans< float > KMeansF
Type aliases for common use cases.