Cpp ML Library  1.0.0
A library of Machine Learning Algorithmns seen from the Udemy course Machine Learning A to Z.
Public Member Functions | List of all members
KMeans Class Reference

Implements the K-Means clustering algorithm with K-Means++ initialization. More...

#include <KMeans.hpp>

Public Member Functions

 KMeans (int n_clusters=8, int max_iter=300, double tol=1e-4, unsigned int random_state=0)
 Constructs a KMeans object. More...
 
 ~KMeans ()
 Destructor for KMeans.
 
void fit (const std::vector< std::vector< double >> &X)
 Fits the KMeans model to the data. More...
 
std::vector< int > predict (const std::vector< std::vector< double >> &X) const
 Predicts the closest cluster each sample in X belongs to. More...
 
const std::vector< std::vector< double > > & get_cluster_centers () const
 Returns the cluster centers. More...
 

Detailed Description

Implements the K-Means clustering algorithm with K-Means++ initialization.

Constructor & Destructor Documentation

◆ KMeans()

KMeans::KMeans ( int  n_clusters = 8,
int  max_iter = 300,
double  tol = 1e-4,
unsigned int  random_state = 0 
)

Constructs a KMeans object.

Parameters
n_clustersThe number of clusters to form.
max_iterThe maximum number of iterations.
tolThe tolerance to declare convergence.
random_stateSeed for random number generator (optional).

Member Function Documentation

◆ fit()

void KMeans::fit ( const std::vector< std::vector< double >> &  X)

Fits the KMeans model to the data.

Parameters
XA vector of feature vectors.

◆ get_cluster_centers()

const std::vector< std::vector< double > > & KMeans::get_cluster_centers ( ) const

Returns the cluster centers.

Returns
A vector of cluster centers.

◆ predict()

std::vector< int > KMeans::predict ( const std::vector< std::vector< double >> &  X) const

Predicts the closest cluster each sample in X belongs to.

Parameters
XA vector of feature vectors.
Returns
A vector of cluster labels.

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