Darwin  1.10(beta)
drwnKMeans.h
1 /******************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2017, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnKMeans.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <vector>
16 #include <limits>
17 
18 #include "Eigen/Core"
19 
20 #include "drwnBase.h"
21 #include "drwnFeatureTransform.h"
22 
23 using namespace std;
24 using namespace Eigen;
25 
26 // drwnKMeans class -----------------------------------------------------------
53 
55  public:
56  static int DEFAULT_K;
57  static int MAX_ITERATIONS;
58 
59  private:
60  unsigned _numClusters;
61  MatrixXd _centroids;
62  VectorXd _cSqNorm;
63 
64  public:
66  drwnKMeans(unsigned k = DEFAULT_K);
68  drwnKMeans(const drwnKMeans& ft);
69  ~drwnKMeans();
70 
71  // access functions
72  const char *type() const { return "drwnKMeans"; }
73  drwnKMeans *clone() const { return new drwnKMeans(*this); }
74 
76  const MatrixXd& getCentroids() const { return _centroids; }
77 
78  // i/o
79  void clear();
80  bool save(drwnXMLNode& node) const;
81  bool load(drwnXMLNode& node);
82 
83  // training
85  double train(const vector<vector<double> >& features);
86  double train(const vector<vector<double> >& features, const vector<double>& weights);
87 
88  // evaluation
90  void transform(const vector<double>& x, vector<double>& y) const;
91 };
Implements k-means clustering. Outputs the squared-distance to each of the cluster centroids...
Definition: drwnKMeans.h:54
virtual void transform(vector< double > &x) const
transforms a feature vector in-place
Definition: drwnFeatureTransform.cpp:57
static int MAX_ITERATIONS
maximum number of training iterations
Definition: drwnKMeans.h:57
static int DEFAULT_K
default number of clusters
Definition: drwnKMeans.h:56
virtual double train(const vector< vector< double > > &features)=0
Estimate the parameters of the features transformation. This function must be implemented in the deri...
Implements interface for unsupervised feature transforms (i.e, without class labels).
Definition: drwnFeatureTransform.h:118
const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnKMeans.h:72
drwnKMeans * clone() const
returns a copy of the class usually implemented as virtual Foo* clone() { return new Foo(*this); } ...
Definition: drwnKMeans.h:73
const MatrixXd & getCentroids() const
returns the k centroids
Definition: drwnKMeans.h:76