mlpack  3.1.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
127  HoeffdingTree(const data::DatasetInfo& datasetInfo,
128  const size_t numClasses,
129  const double successProbability = 0.95,
130  const size_t maxSamples = 0,
131  const size_t checkInterval = 100,
132  const size_t minSamples = 100,
133  const CategoricalSplitType<FitnessFunction>& categoricalSplitIn
134  = CategoricalSplitType<FitnessFunction>(0, 0),
135  const NumericSplitType<FitnessFunction>& numericSplitIn =
136  NumericSplitType<FitnessFunction>(0),
137  std::unordered_map<size_t, std::pair<size_t, size_t>>*
138  dimensionMappings = NULL,
139  const bool copyDatasetInfo = true);
140 
145  HoeffdingTree();
146 
153  HoeffdingTree(const HoeffdingTree& other);
154 
158  ~HoeffdingTree();
159 
168  template<typename MatType>
169  void Train(const MatType& data,
170  const arma::Row<size_t>& labels,
171  const bool batchTraining = true);
172 
177  template<typename MatType>
178  void Train(const MatType& data,
179  const data::DatasetInfo& info,
180  const arma::Row<size_t>& labels,
181  const bool batchTraining = true);
182 
189  template<typename VecType>
190  void Train(const VecType& point, const size_t label);
191 
197  size_t SplitCheck();
198 
200  size_t SplitDimension() const { return splitDimension; }
201 
203  size_t MajorityClass() const { return majorityClass; }
205  size_t& MajorityClass() { return majorityClass; }
206 
208  double MajorityProbability() const { return majorityProbability; }
210  double& MajorityProbability() { return majorityProbability; }
211 
213  size_t NumChildren() const { return children.size(); }
214 
216  const HoeffdingTree& Child(const size_t i) const { return *children[i]; }
218  HoeffdingTree& Child(const size_t i) { return *children[i]; }
219 
221  double SuccessProbability() const { return successProbability; }
223  void SuccessProbability(const double successProbability);
224 
226  size_t MinSamples() const { return minSamples; }
228  void MinSamples(const size_t minSamples);
229 
231  size_t MaxSamples() const { return maxSamples; }
233  void MaxSamples(const size_t maxSamples);
234 
236  size_t CheckInterval() const { return checkInterval; }
238  void CheckInterval(const size_t checkInterval);
239 
247  template<typename VecType>
248  size_t CalculateDirection(const VecType& point) const;
249 
257  template<typename VecType>
258  size_t Classify(const VecType& point) const;
259 
261  size_t NumDescendants() const;
262 
274  template<typename VecType>
275  void Classify(const VecType& point, size_t& prediction, double& probability)
276  const;
277 
285  template<typename MatType>
286  void Classify(const MatType& data, arma::Row<size_t>& predictions) const;
287 
299  template<typename MatType>
300  void Classify(const MatType& data,
301  arma::Row<size_t>& predictions,
302  arma::rowvec& probabilities) const;
303 
307  void CreateChildren();
308 
310  template<typename Archive>
311  void serialize(Archive& ar, const unsigned int /* version */);
312 
313  private:
314  // We need to keep some information for before we have split.
315 
317  std::vector<NumericSplitType<FitnessFunction>> numericSplits;
319  std::vector<CategoricalSplitType<FitnessFunction>> categoricalSplits;
320 
322  std::unordered_map<size_t, std::pair<size_t, size_t>>* dimensionMappings;
324  bool ownsMappings;
325 
327  size_t numSamples;
329  size_t numClasses;
331  size_t maxSamples;
333  size_t checkInterval;
335  size_t minSamples;
337  const data::DatasetInfo* datasetInfo;
339  bool ownsInfo;
341  double successProbability;
342 
343  // And we need to keep some information for after we have split.
344 
346  size_t splitDimension;
348  size_t majorityClass;
351  double majorityProbability;
353  typename CategoricalSplitType<FitnessFunction>::SplitInfo categoricalSplit;
355  typename NumericSplitType<FitnessFunction>::SplitInfo numericSplit;
357  std::vector<HoeffdingTree*> children;
358 };
359 
360 } // namespace tree
361 } // namespace mlpack
362 
363 #include "hoeffding_tree_impl.hpp"
364 
365 #endif
HoeffdingTree()
Construct a Hoeffding tree with no data and no information.
Auxiliary information for a dataset, including mappings to/from strings (or other types) and the data...
double SuccessProbability() const
Get the confidence required for a split.
size_t SplitDimension() const
Get the splitting dimension (size_t(-1) if no split).
size_t CheckInterval() const
Get the number of samples before a split check is performed.
~HoeffdingTree()
Clean up memory.
The HoeffdingTree object represents all of the necessary information for a Hoeffding-bound-based deci...
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...
const HoeffdingTree & Child(const size_t i) const
Get a child.
void CreateChildren()
Given that this node should split, create the children.
CategoricalSplitType< FitnessFunction > CategoricalSplit
Allow access to the categorical split type.
size_t MajorityClass() const
Get the majority class.
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 NumChildren() const
Get the number of children.
HoeffdingNumericSplit< FitnessFunction, double > HoeffdingDoubleNumericSplit
Convenience typedef.
size_t & MajorityClass()
Modify the majority class.
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 MaxSamples() const
Get the maximum number of samples before a split is forced.
size_t NumDescendants() const
Get the size of the Hoeffding Tree.
size_t MinSamples() const
Get the minimum number of samples for a split.
double MajorityProbability() const
Get the probability of the majority class (based on training samples).
NumericSplitType< FitnessFunction > NumericSplit
Allow access to the numeric split type.
double & MajorityProbability()
Modify the probability of the majority class.
size_t Classify(const VecType &point) const
Classify the given point, using this node and the entire (sub)tree beneath it.