mlpack  2.0.1
dual_tree_kmeans_statistic.hpp
Go to the documentation of this file.
1 
14 #ifndef __MLPACK_METHODS_KMEANS_DTNN_STATISTIC_HPP
15 #define __MLPACK_METHODS_KMEANS_DTNN_STATISTIC_HPP
16 
18 
19 namespace mlpack {
20 namespace kmeans {
21 
23  neighbor::NeighborSearchStat<neighbor::NearestNeighborSort>
24 {
25  public:
27  neighbor::NeighborSearchStat<neighbor::NearestNeighborSort>(),
28  upperBound(DBL_MAX),
29  lowerBound(DBL_MAX),
30  owner(size_t(-1)),
31  pruned(size_t(-1)),
32  staticPruned(false),
35  centroid(),
36  trueParent(NULL)
37  {
38  // Nothing to do.
39  }
40 
41  template<typename TreeType>
42  DualTreeKMeansStatistic(TreeType& node) :
43  neighbor::NeighborSearchStat<neighbor::NearestNeighborSort>(),
44  upperBound(DBL_MAX),
45  lowerBound(DBL_MAX),
46  owner(size_t(-1)),
47  pruned(size_t(-1)),
48  staticPruned(false),
51  trueParent(node.Parent())
52  {
53  // Empirically calculate the centroid.
54  centroid.zeros(node.Dataset().n_rows);
55  for (size_t i = 0; i < node.NumPoints(); ++i)
56  {
57  // Correct handling of cover tree: don't double-count the point which
58  // appears in the children.
60  node.NumChildren() > 0)
61  continue;
62  centroid += node.Dataset().col(node.Point(i));
63  }
64 
65  for (size_t i = 0; i < node.NumChildren(); ++i)
66  centroid += node.Child(i).NumDescendants() *
67  node.Child(i).Stat().Centroid();
68 
69  centroid /= node.NumDescendants();
70 
71  // Set the true children correctly.
72  trueChildren.resize(node.NumChildren());
73  for (size_t i = 0; i < node.NumChildren(); ++i)
74  trueChildren[i] = &node.Child(i);
75  }
76 
77  double UpperBound() const { return upperBound; }
78  double& UpperBound() { return upperBound; }
79 
80  double LowerBound() const { return lowerBound; }
81  double& LowerBound() { return lowerBound; }
82 
83  const arma::vec& Centroid() const { return centroid; }
84  arma::vec& Centroid() { return centroid; }
85 
86  size_t Owner() const { return owner; }
87  size_t& Owner() { return owner; }
88 
89  size_t Pruned() const { return pruned; }
90  size_t& Pruned() { return pruned; }
91 
92  bool StaticPruned() const { return staticPruned; }
93  bool& StaticPruned() { return staticPruned; }
94 
97 
100 
101  void* TrueParent() const { return trueParent; }
102  void*& TrueParent() { return trueParent; }
103 
104  void* TrueChild(const size_t i) const { return trueChildren[i]; }
105  void*& TrueChild(const size_t i) { return trueChildren[i]; }
106 
107  size_t NumTrueChildren() const { return trueChildren.size(); }
108 
109  private:
110  double upperBound;
111  double lowerBound;
112  size_t owner;
113  size_t pruned;
117  arma::vec centroid;
118  void* trueParent;
119  std::vector<void*> trueChildren;
120 };
121 
122 } // namespace kmeans
123 } // namespace mlpack
124 
125 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Extra data for each node in the tree.
NeighborSearchStat()
Initialize the statistic with the worst possible distance according to our sorting policy...
The TreeTraits class provides compile-time information on the characteristics of a given tree type...
Definition: tree_traits.hpp:79