forpy  2
classificationleaf.h
Go to the documentation of this file.
1 /* Author: Christoph Lassner. */
2 #pragma once
3 #ifndef FORPY_LEAFS_CLASSIFICATIONLEAF_H_
4 #define FORPY_LEAFS_CLASSIFICATIONLEAF_H_
5 
6 #include "../global.h"
7 
8 #include "../util/serialization/basics.h"
9 
10 #include <numeric>
11 #include <vector>
12 
13 #include "../data_providers/idataprovider.h"
14 #include "../types.h"
15 #include "../util/serialization/eigen.h"
16 #include "../util/serialization/serialization.h"
17 #include "../util/storage.h"
18 #include "./ileaf.h"
19 
20 namespace forpy {
26 class ClassificationLeaf : public ILeaf {
27  public:
32  inline explicit ClassificationLeaf(const uint &n_classes = 0)
34  class_transl_ptr(nullptr),
35  true_max_class(0),
37 
39  inline std::shared_ptr<ILeaf> create_duplicate() const {
41  return std::make_shared<ClassificationLeaf>(n_classes);
42  };
43  inline bool is_compatible_with(const IDataProvider & /*data_provider*/) {
44  return true;
45  }
46  bool is_compatible_with(const IThreshOpt &threshopt);
47  inline void transfer_or_run_check(ILeaf *other, IThreshOpt *thresh_opt,
48  IDataProvider *dprov) {
49  auto *cl_ot = dynamic_cast<ClassificationLeaf *>(other);
50  if (cl_ot == nullptr) {
51  cl_ot->n_classes = n_classes;
52  cl_ot->class_transl_ptr = class_transl_ptr;
53  cl_ot->true_max_class = true_max_class;
54  } else {
55  other->is_compatible_with(*dprov);
56  other->is_compatible_with(*thresh_opt);
57  }
58  }
59  void make_leaf(const TodoMark &todo_info, const IDataProvider &data_provider,
60  Desk *desk) const;
61  size_t get_result_columns(const size_t &n_trees = 1,
62  const bool &predict_proba = false,
63  const bool &for_forest = false) const;
64  Data<Mat> get_result_type(const bool &predict_proba,
65  const bool &for_forest = false) const;
66  void get_result(const id_t &node_id, Data<MatRef> &target_v,
67  const bool &predict_proba, const bool &for_forest) const;
68  void get_result(const std::vector<Data<Mat>> &leaf_results,
69  Data<MatRef> &target_v,
70  const Vec<float> &weights = Vec<float>(),
71  const bool &predict_proba = false) const;
72  inline void ensure_capacity(const size_t &n) {
73  stored_distributions.resize(n);
74  };
75  inline void finalize_capacity(const size_t &n) { ensure_capacity(n); };
77 
78  bool operator==(const ILeaf &rhs) const;
79  inline friend std::ostream &operator<<(std::ostream &stream,
80  const ClassificationLeaf &self) {
81  stream << "forpy::ClassificationLeaf[" << self.stored_distributions.size()
82  << " stored]";
83  return stream;
84  };
85 
86  const std::vector<Vec<float>> &get_stored_dists() const;
87 
88  inline const std::vector<Mat<float>> *get_map() const { return nullptr; };
89 
90  private:
91  friend class cereal::access;
92  template <class Archive>
93  void serialize(Archive &ar, const uint) {
94  ar(cereal::make_nvp("base", cereal::base_class<ILeaf>(this)),
95  CEREAL_NVP(n_classes), CEREAL_NVP(stored_distributions));
96  };
97 
99  std::shared_ptr<std::vector<uint>> class_transl_ptr;
101  std::vector<Vec<float>> stored_distributions;
102 };
103 }; // namespace forpy
104 
106 #endif // FORPY_LEAFS_CLASSIFICATIONLEAF_H_
void make_leaf(const TodoMark &todo_info, const IDataProvider &data_provider, Desk *desk) const
Interface implementation.
std::shared_ptr< ILeaf > create_duplicate() const
Interface implementation.
Find an optimal threshold.
Definition: ithreshopt.h:23
bool operator==(const ILeaf &rhs) const
virtual bool is_compatible_with(const IDataProvider &)
Checks compatibility with a certain IDataProvider.
Definition: ileaf.h:34
void transfer_or_run_check(ILeaf *other, IThreshOpt *thresh_opt, IDataProvider *dprov)
Interface implementation.
Data< Mat > get_result_type(const bool &predict_proba, const bool &for_forest=false) const
Interface implementation.
void finalize_capacity(const size_t &n)
Interface implementation.
A data provider for the training of one tree.
Definition: idataprovider.h:22
size_t id_t
Element id type.
Definition: types.h:106
void get_result(const id_t &node_id, Data< MatRef > &target_v, const bool &predict_proba, const bool &for_forest) const
Interface implementation.
typename mu::variant< Empty, STOT< float >, STOT< double >, STOT< uint >, STOT< uint8_t > > Data
Storing a variant of the provided data container type.
Definition: storage.h:126
Stores the parameters for one marked tree node.
Definition: types.h:152
const std::vector< Vec< float > > & get_stored_dists() const
std::shared_ptr< std::vector< uint > > class_transl_ptr
ClassificationLeaf(const uint &n_classes=0)
void ensure_capacity(const size_t &n)
Interface implementation.
Stores and returns leaf values, and combines them to forest results.
Definition: ileaf.h:23
friend std::ostream & operator<<(std::ostream &stream, const ClassificationLeaf &self)
Stores the probability distributions for n_classes at a leaf.
Eigen::Matrix< DT, Eigen::Dynamic, 1, Eigen::ColMajor > Vec
Definition: types.h:73
size_t get_result_columns(const size_t &n_trees=1, const bool &predict_proba=false, const bool &for_forest=false) const
Interface implementation.
Main thread desk object.
Definition: desk.h:201
bool is_compatible_with(const IDataProvider &)
Interface implementation.
const std::vector< Mat< float > > * get_map() const
Get all leafs.
std::vector< Vec< float > > stored_distributions
CEREAL_REGISTER_TYPE(forpy::ClassificationLeaf)
unsigned int uint
Convenience typedef for unsigned int.
Definition: types.h:113
void serialize(Archive &ar, const uint)