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)
33 : n_classes(n_classes),
34 class_transl_ptr(nullptr),
35 true_max_class(0),
36 stored_distributions(){};
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
98 uint n_classes;
99 std::shared_ptr<std::vector<uint>> class_transl_ptr;
100 uint true_max_class;
101 std::vector<Vec<float>> stored_distributions;
102 };
103 }; // namespace forpy
104
105 CEREAL_REGISTER_TYPE(forpy::ClassificationLeaf);
106 #endif // FORPY_LEAFS_CLASSIFICATIONLEAF_H_
forpy::ClassificationLeaf::make_leaf
void make_leaf(const TodoMark &todo_info, const IDataProvider &data_provider, Desk *desk) const
Interface implementation.
forpy::ClassificationLeaf::create_duplicate
std::shared_ptr< ILeaf > create_duplicate() const
Interface implementation.
Definition: classificationleaf.h:40
forpy::IThreshOpt
Find an optimal threshold.
Definition: ithreshopt.h:23
forpy::ClassificationLeaf::operator==
bool operator==(const ILeaf &rhs) const
forpy::ILeaf::is_compatible_with
virtual bool is_compatible_with(const IDataProvider &)
Checks compatibility with a certain IDataProvider.
Definition: ileaf.h:34
forpy::ClassificationLeaf::transfer_or_run_check
void transfer_or_run_check(ILeaf *other, IThreshOpt *thresh_opt, IDataProvider *dprov)
Interface implementation.
Definition: classificationleaf.h:47
forpy::ClassificationLeaf::get_result_type
Data< Mat > get_result_type(const bool &predict_proba, const bool &for_forest=false) const
Interface implementation.
forpy::ClassificationLeaf::finalize_capacity
void finalize_capacity(const size_t &n)
Interface implementation.
Definition: classificationleaf.h:75
forpy::IDataProvider
A data provider for the training of one tree.
Definition: idataprovider.h:22
forpy::id_t
size_t id_t
Element id type.
Definition: types.h:106
forpy::ClassificationLeaf::get_result
void get_result(const id_t &node_id, Data< MatRef > &target_v, const bool &predict_proba, const bool &for_forest) const
Interface implementation.
forpy::Data
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
ileaf.h
forpy::TodoMark
Stores the parameters for one marked tree node.
Definition: types.h:152
forpy::ClassificationLeaf::get_stored_dists
const std::vector< Vec< float > > & get_stored_dists() const
forpy::ClassificationLeaf::class_transl_ptr
std::shared_ptr< std::vector< uint > > class_transl_ptr
Definition: classificationleaf.h:99
forpy::ClassificationLeaf::ClassificationLeaf
ClassificationLeaf(const uint &n_classes=0)
Definition: classificationleaf.h:32
forpy::ClassificationLeaf::ensure_capacity
void ensure_capacity(const size_t &n)
Interface implementation.
Definition: classificationleaf.h:72
forpy::ILeaf
Stores and returns leaf values, and combines them to forest results.
Definition: ileaf.h:23
forpy::ClassificationLeaf::n_classes
uint n_classes
Definition: classificationleaf.h:96
forpy::ClassificationLeaf::operator<<
friend std::ostream & operator<<(std::ostream &stream, const ClassificationLeaf &self)
Definition: classificationleaf.h:79
forpy::ClassificationLeaf
Stores the probability distributions for n_classes at a leaf.
Definition: classificationleaf.h:26
forpy::Vec
Eigen::Matrix< DT, Eigen::Dynamic, 1, Eigen::ColMajor > Vec
Definition: types.h:73
forpy
Definition: fastdprov.h:13
forpy::ClassificationLeaf::get_result_columns
size_t get_result_columns(const size_t &n_trees=1, const bool &predict_proba=false, const bool &for_forest=false) const
Interface implementation.
forpy::Desk
Main thread desk object.
Definition: desk.h:201
forpy::ClassificationLeaf::is_compatible_with
bool is_compatible_with(const IDataProvider &)
Interface implementation.
Definition: classificationleaf.h:43
forpy::ClassificationLeaf::get_map
const std::vector< Mat< float > > * get_map() const
Get all leafs.
Definition: classificationleaf.h:88
forpy::ClassificationLeaf::stored_distributions
std::vector< Vec< float > > stored_distributions
Definition: classificationleaf.h:101
CEREAL_REGISTER_TYPE
CEREAL_REGISTER_TYPE(forpy::ClassificationLeaf)
forpy::uint
unsigned int uint
Convenience typedef for unsigned int.
Definition: types.h:113
forpy::ClassificationLeaf::true_max_class
uint true_max_class
Definition: classificationleaf.h:100
forpy::ClassificationLeaf::serialize
void serialize(Archive &ar, const uint)
Definition: classificationleaf.h:93