Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
Loading...
Searching...
No Matches
ml::KMeans< T > Class Template Reference

#include <kmeans.hpp>

Public Member Functions

 KMeans (size_t k, size_t max_iters=300, T tol=1e-4, int random_state=-1)
 Constructor with number of clusters.
 
void fit (const Matrix< T > &data)
 Fit the K-Means model to the data.
 
std::vector< intpredict (const Matrix< T > &data) const
 Predict cluster labels for the given data.
 
std::vector< intfit_predict (const Matrix< T > &data)
 Fit the model and predict cluster labels.
 
Matrix< T > cluster_centers () const
 Get the cluster centroids.
 
inertia () const
 Get the within-cluster sum of squares (inertia)
 
size_t n_iter () const
 Get the number of iterations performed.
 

Detailed Description

template<typename T>
class ml::KMeans< T >
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/ml/ml.hpp.

Definition at line 41 of file kmeans.hpp.

Constructor & Destructor Documentation

◆ KMeans()

template<typename T >
ml::KMeans< T >::KMeans ( size_t  k,
size_t  max_iters = 300,
tol = 1e-4,
int  random_state = -1 
)

Constructor with number of clusters.

Parameters
kNumber of clusters
max_itersMaximum number of iterations
tolTolerance for convergence
random_stateRandom seed for reproducibility

Definition at line 11 of file kmeans.cpp.

Member Function Documentation

◆ cluster_centers()

template<typename T >
Matrix< T > ml::KMeans< T >::cluster_centers ( ) const

Get the cluster centroids.

Returns
Matrix containing cluster centroids
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/ml/kmeans.hpp.

Definition at line 88 of file kmeans.cpp.

◆ fit()

template<typename T >
void ml::KMeans< T >::fit ( const Matrix< T > &  data)

Fit the K-Means model to the data.

Parameters
dataInput data matrix where rows are samples and columns are features
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/ml/kmeans.hpp.

Definition at line 20 of file kmeans.cpp.

◆ fit_predict()

template<typename T >
std::vector< int > ml::KMeans< T >::fit_predict ( const Matrix< T > &  data)

Fit the model and predict cluster labels.

Parameters
dataInput data matrix
Returns
Vector of cluster labels (0 to k-1)
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/ml/kmeans.hpp.

Definition at line 82 of file kmeans.cpp.

◆ inertia()

template<typename T >
T ml::KMeans< T >::inertia ( ) const

Get the within-cluster sum of squares (inertia)

Returns
The inertia value
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/ml/kmeans.hpp.

Definition at line 96 of file kmeans.cpp.

◆ n_iter()

template<typename T >
size_t ml::KMeans< T >::n_iter ( ) const

Get the number of iterations performed.

Returns
Number of iterations
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/ml/kmeans.hpp.

Definition at line 104 of file kmeans.cpp.

◆ predict()

template<typename T >
std::vector< int > ml::KMeans< T >::predict ( const Matrix< T > &  data) const

Predict cluster labels for the given data.

Parameters
dataInput data matrix
Returns
Vector of cluster labels (0 to k-1)
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/ml/kmeans.hpp.

Definition at line 74 of file kmeans.cpp.


The documentation for this class was generated from the following files: