Machine learning algorithms in C++
Public Types | Public Member Functions | Private Member Functions | Private Attributes
KMeans Class Reference

Implementation of the k-means algorithm. More...

#include <KMeans.hpp>

Collaboration diagram for KMeans:

Public Types

enum  InitializationMethod { RANDOM, SAMPLE }
 

Public Member Functions

 KMeans ()
 
MatrixD predict (MatrixD data)
 Assigns elements of a data set to clusters. More...
 
void fit (MatrixD data, unsigned int k, unsigned int iters=100, unsigned int inits=100, double distance=2, InitializationMethod initMethod=SAMPLE, bool verbose=false)
 Find the k centroids that best fit the data. More...
 
const MatrixD & getY () const
 
const MatrixD & getCentroids () const
 
unsigned int getK () const
 
unsigned int getTotalIterations () const
 
double getDistance () const
 
double getSse () const
 

Private Member Functions

double SSE ()
 

Private Attributes

MatrixD X
 
MatrixD y
 
MatrixD centroids
 
unsigned int k
 
unsigned int totalIterations
 
double distance
 
double sse
 
InitializationMethod initMethod
 

Detailed Description

Implementation of the k-means algorithm.

Author
Douglas De Rizzo Meneghetti (dougl.nosp@m.asri.nosp@m.zzom@.nosp@m.gmai.nosp@m.l.com)
Date
2017-10-25 Implementaion of the k-means algorithm

Definition at line 17 of file KMeans.hpp.

Member Enumeration Documentation

◆ InitializationMethod

Enumerator
RANDOM 
SAMPLE 

Definition at line 19 of file KMeans.hpp.

Constructor & Destructor Documentation

◆ KMeans()

KMeans::KMeans ( )
inline

Definition at line 34 of file KMeans.hpp.

Member Function Documentation

◆ fit()

void KMeans::fit ( MatrixD  data,
unsigned int  k,
unsigned int  iters = 100,
unsigned int  inits = 100,
double  distance = 2,
InitializationMethod  initMethod = SAMPLE,
bool  verbose = false 
)
inline

Find the k centroids that best fit the data.

Parameters
dataa Matrix containing the data
knumber of clusters to be generated
itersnumber of maximum assignment/adjustment iterations
initsnumber of algorithm reinitialization
distanceL norm of the distance measure to be used (1 for Manhattan, 2 for Euclidean etc.)
initMethodcentroid initialization method
verbosewhether to output progress or not

Definition at line 72 of file KMeans.hpp.

Here is the call graph for this function:

◆ getCentroids()

const MatrixD& KMeans::getCentroids ( ) const
inline

Definition at line 129 of file KMeans.hpp.

◆ getDistance()

double KMeans::getDistance ( ) const
inline

Definition at line 141 of file KMeans.hpp.

◆ getK()

unsigned int KMeans::getK ( ) const
inline

Definition at line 133 of file KMeans.hpp.

◆ getSse()

double KMeans::getSse ( ) const
inline

Definition at line 145 of file KMeans.hpp.

◆ getTotalIterations()

unsigned int KMeans::getTotalIterations ( ) const
inline

Definition at line 137 of file KMeans.hpp.

◆ getY()

const MatrixD& KMeans::getY ( ) const
inline

Definition at line 125 of file KMeans.hpp.

◆ predict()

MatrixD KMeans::predict ( MatrixD  data)
inline

Assigns elements of a data set to clusters.

Parameters
dataa Matrix containing elements in rows and features in columns
Returns
column vector with the index of clusters each element is assigned to

Definition at line 41 of file KMeans.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SSE()

double KMeans::SSE ( )
inlineprivate
Returns
Sum of squared errors between elements and their centroids

Definition at line 29 of file KMeans.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ centroids

MatrixD KMeans::centroids
private

Definition at line 21 of file KMeans.hpp.

◆ distance

double KMeans::distance
private

Definition at line 23 of file KMeans.hpp.

◆ initMethod

InitializationMethod KMeans::initMethod
private

Definition at line 24 of file KMeans.hpp.

◆ k

unsigned int KMeans::k
private

Definition at line 22 of file KMeans.hpp.

◆ sse

double KMeans::sse
private

Definition at line 23 of file KMeans.hpp.

◆ totalIterations

unsigned int KMeans::totalIterations
private

Definition at line 22 of file KMeans.hpp.

◆ X

MatrixD KMeans::X
private

Definition at line 21 of file KMeans.hpp.

◆ y

MatrixD KMeans::y
private

Definition at line 21 of file KMeans.hpp.


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