hoeffding_tree.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_HOEFFDING_TREES_HOEFFDING_TREE_HPP
14 #define MLPACK_METHODS_HOEFFDING_TREES_HOEFFDING_TREE_HPP
15 
16 #include <mlpack/prereqs.hpp>
18 #include "gini_impurity.hpp"
21 
22 namespace mlpack {
23 namespace tree {
24 
55 template<typename FitnessFunction = GiniImpurity,
56  template<typename> class NumericSplitType =
58  template<typename> class CategoricalSplitType =
59  HoeffdingCategoricalSplit
60 >
62 {
63  public:
65  typedef NumericSplitType<FitnessFunction> NumericSplit;
67  typedef CategoricalSplitType<FitnessFunction> CategoricalSplit;
68 
91  template<typename MatType>
92  HoeffdingTree(const MatType& data,
93  const data::DatasetInfo& datasetInfo,
94  const arma::Row<size_t>& labels,
95  const size_t numClasses,
96  const bool batchTraining = true,
97  const double successProbability = 0.95,
98  const size_t maxSamples = 0,
99  const size_t checkInterval = 100,
100  const size_t minSamples = 100,
101  const CategoricalSplitType<FitnessFunction>& categoricalSplitIn
102  = CategoricalSplitType<FitnessFunction>(0, 0),
103  const NumericSplitType<FitnessFunction>& numericSplitIn =
104  NumericSplitType<FitnessFunction>(0));
105 
125  HoeffdingTree(const data::DatasetInfo& datasetInfo,
126  const size_t numClasses,
127  const double successProbability = 0.95,
128  const size_t maxSamples = 0,
129  const size_t checkInterval = 100,
130  const size_t minSamples = 100,
131  const CategoricalSplitType<FitnessFunction>& categoricalSplitIn
132  = CategoricalSplitType<FitnessFunction>(0, 0),
133  const NumericSplitType<FitnessFunction>& numericSplitIn =
134  NumericSplitType<FitnessFunction>(0),
135  std::unordered_map<size_t, std::pair<size_t, size_t>>*
136  dimensionMappings = NULL);
137 
142  HoeffdingTree();
143 
150  HoeffdingTree(const HoeffdingTree& other);
151 
155  ~HoeffdingTree();
156 
165  template<typename MatType>
166  void Train(const MatType& data,
167  const arma::Row<size_t>& labels,
168  const bool batchTraining = true);
169 
174  template<typename MatType>
175  void Train(const MatType& data,
176  const data::DatasetInfo& info,
177  const arma::Row<size_t>& labels,
178  const bool batchTraining = true);
179 
186  template<typename VecType>
187  void Train(const VecType& point, const size_t label);
188 
194  size_t SplitCheck();
195 
197  size_t SplitDimension() const { return splitDimension; }
198 
200  size_t MajorityClass() const { return majorityClass; }
202  size_t& MajorityClass() { return majorityClass; }
203 
205  double MajorityProbability() const { return majorityProbability; }
207  double& MajorityProbability() { return majorityProbability; }
208 
210  size_t NumChildren() const { return children.size(); }
211 
213  const HoeffdingTree& Child(const size_t i) const { return *children[i]; }
215  HoeffdingTree& Child(const size_t i) { return *children[i]; }
216 
218  double SuccessProbability() const { return successProbability; }
220  void SuccessProbability(const double successProbability);
221 
223  size_t MinSamples() const { return minSamples; }
225  void MinSamples(const size_t minSamples);
226 
228  size_t MaxSamples() const { return maxSamples; }
230  void MaxSamples(const size_t maxSamples);
231 
233  size_t CheckInterval() const { return checkInterval; }
235  void CheckInterval(const size_t checkInterval);
236 
244  template<typename VecType>
245  size_t CalculateDirection(const VecType& point) const;
246 
254  template<typename VecType>
255  size_t Classify(const VecType& point) const;
256 
258  size_t NumDescendants() const;
259 
271  template<typename VecType>
272  void Classify(const VecType& point, size_t& prediction, double& probability)
273  const;
274 
282  template<typename MatType>
283  void Classify(const MatType& data, arma::Row<size_t>& predictions) const;
284 
296  template<typename MatType>
297  void Classify(const MatType& data,
298  arma::Row<size_t>& predictions,
299  arma::rowvec& probabilities) const;
300 
304  void CreateChildren();
305 
307  template<typename Archive>
308  void serialize(Archive& ar, const unsigned int /* version */);
309 
310  private:
311  // We need to keep some information for before we have split.
312 
314  std::vector<NumericSplitType<FitnessFunction>> numericSplits;
316  std::vector<CategoricalSplitType<FitnessFunction>> categoricalSplits;
317 
319  std::unordered_map<size_t, std::pair<size_t, size_t>>* dimensionMappings;
321  bool ownsMappings;
322 
324  size_t numSamples;
326  size_t numClasses;
328  size_t maxSamples;
330  size_t checkInterval;
332  size_t minSamples;
334  const data::DatasetInfo* datasetInfo;
336  bool ownsInfo;
338  double successProbability;
339 
340  // And we need to keep some information for after we have split.
341 
343  size_t splitDimension;
345  size_t majorityClass;
348  double majorityProbability;
350  typename CategoricalSplitType<FitnessFunction>::SplitInfo categoricalSplit;
352  typename NumericSplitType<FitnessFunction>::SplitInfo numericSplit;
354  std::vector<HoeffdingTree*> children;
355 };
356 
357 } // namespace tree
358 } // namespace mlpack
359 
360 #include "hoeffding_tree_impl.hpp"
361 
362 #endif
HoeffdingTree()
Construct a Hoeffding tree with no data and no information.
const HoeffdingTree & Child(const size_t i) const
Get a child.
size_t NumChildren() const
Get the number of children.
Auxiliary information for a dataset, including mappings to/from strings (or other types) and the data...
~HoeffdingTree()
Clean up memory.
The HoeffdingTree object represents all of the necessary information for a Hoeffding-bound-based deci...
size_t CheckInterval() const
Get the number of samples before a split check is performed.
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
void serialize(Archive &ar, const unsigned int)
Serialize the split.
void Train(const MatType &data, const arma::Row< size_t > &labels, const bool batchTraining=true)
Train on a set of points, either in streaming mode or in batch mode, with the given labels...
size_t CalculateDirection(const VecType &point) const
Given a point and that this node is not a leaf, calculate the index of the child node this point woul...
size_t Classify(const VecType &point) const
Classify the given point, using this node and the entire (sub)tree beneath it.
size_t MaxSamples() const
Get the maximum number of samples before a split is forced.
double SuccessProbability() const
Get the confidence required for a split.
void CreateChildren()
Given that this node should split, create the children.
CategoricalSplitType< FitnessFunction > CategoricalSplit
Allow access to the categorical split type.
double MajorityProbability() const
Get the probability of the majority class (based on training samples).
size_t NumDescendants() const
Get the size of the Hoeffding Tree.
HoeffdingNumericSplit< FitnessFunction, double > HoeffdingDoubleNumericSplit
Convenience typedef.
size_t & MajorityClass()
Modify the majority class.
size_t SplitDimension() const
Get the splitting dimension (size_t(-1) if no split).
size_t MinSamples() const
Get the minimum number of samples for a split.
size_t SplitCheck()
Check if a split would satisfy the conditions of the Hoeffding bound with the node&#39;s specified succes...
HoeffdingTree & Child(const size_t i)
Modify a child.
size_t MajorityClass() const
Get the majority class.
NumericSplitType< FitnessFunction > NumericSplit
Allow access to the numeric split type.
double & MajorityProbability()
Modify the probability of the majority class.