Brush C++ API
A flexible interpretable machine learning framework
Toggle main menu visibility
Loading...
Searching...
No Matches
tree_node.h
Go to the documentation of this file.
1
#ifndef TREE_NODE_H
2
#define TREE_NODE_H
3
#include <tuple>
4
#include <unordered_map>
5
6
#include "
../init.h
"
7
#include "
../data/data.h
"
8
#include "
node.h
"
9
#include "
functions.h
"
10
#include "
nodetype.h
"
11
#include "../../thirdparty/tree.hh"
12
13
using
std::string;
14
using
Brush::Data::Dataset
;
15
using
Brush::Node
;
16
21
template
<>
22
class
tree_node_
<
Node
> {
// size: 5*4=20 bytes (on 32 bit arch), can be reduced by 8.
23
public
:
24
tree_node_
()
25
:
parent
(0),
first_child
(0),
last_child
(0),
prev_sibling
(0),
next_sibling
(0)
26
{}
27
28
tree_node_
(
const
Node
& val)
29
:
parent
(0),
first_child
(0),
last_child
(0),
prev_sibling
(0),
next_sibling
(0),
data
(val)
30
{}
31
32
tree_node_
(
Node
&& val)
33
:
parent
(0),
first_child
(0),
last_child
(0),
prev_sibling
(0),
next_sibling
(0),
data
(val)
34
{}
35
36
tree_node_<Node>
*
parent
;
37
tree_node_<Node>
*
first_child
, *
last_child
;
38
tree_node_<Node>
*
prev_sibling
, *
next_sibling
;
39
Node
data
;
40
41
template
<
typename
T>
42
auto
fit
(
const
Dataset
& d);
43
44
template
<
typename
T>
45
auto
predict
(
const
Dataset
& d,
const
float
** weights=
nullptr
);
46
47
template
<
typename
T,
typename
W>
48
auto
predict
(
const
Dataset
& d,
const
W** weights);
49
50
string
get_model
(
bool
pretty=
false
)
const
;
51
string
get_tree_model
(
bool
pretty=
false
,
string
offset=
""
)
const
;
52
53
int
get_complexity
()
const
;
54
int
get_linear_complexity
()
const
;
55
int
get_size
(
bool
include_weight=
true
)
const
;
56
57
};
58
using
TreeNode
=
class
tree_node_
<
Node
>;
59
61
// fit, eval, predict
62
63
#include "
dispatch_table.h
"
64
65
template
<
typename
T>
66
auto
TreeNode::fit(
const
Dataset
& d)
67
{
68
auto
F =
dtable_fit
.template Get<T>(data.node_type, data.sig_hash);
69
return
F(d, (*
this
));
70
};
71
72
template
<
typename
T>
73
auto
TreeNode::predict(
const
Dataset
& d,
const
float
** weights)
74
{
75
auto
F =
dtable_predict
.template Get<T>(data.node_type, data.sig_hash);
76
return
F(d, (*
this
), weights);
77
};
78
79
template
<
typename
T,
typename
W>
80
auto
TreeNode::predict(
const
Dataset
& d,
const
W** weights)
81
{
82
auto
F =
dtable_predict
.template Get<T>(data.node_type, data.sig_dual_hash);
83
return
F(d, (*
this
), weights);
84
};
85
86
// serialization functions
87
void
to_json
(json &j,
const
tree<Node> &t);
88
void
from_json
(
const
json &j, tree<Node> &t);
89
90
// namespace node{
91
92
// template<NodeType NT=0>
93
// string get_model(const Node& data, const vector<string>& children)
94
// {
95
// string args = "";
96
// for (int i = 0; i < children.size(); ++i){
97
// args += children.at(i);
98
// if (i < children.size()-1)
99
// args += ",";
100
// }
101
102
// return fmt::format("{}({})", data.get_name(), args);
103
104
// }
105
106
// template<>
107
// string get_model<NodeType::SplitBest>(const Node& data, const vector<string>& children)
108
// {
109
// return fmt::format("IF-THEN-ELSE({}>{:.3f},{},{})",
110
// data.get_feature(),
111
// data.W,
112
// children.at(0),
113
// children.at(1)
114
// );
115
116
// }
117
118
// template<>
119
// string get_model<NodeType::SplitOn>(const Node& data, const vector<string>& children)
120
// {
121
// return fmt::format("IF-THEN-ELSE({}>{:.3f},{},{})",
122
// children.at(0),
123
// data.W,
124
// children.at(1),
125
// children.at(2)
126
// );
127
128
// }
129
// }
130
#endif
Brush::Data::Dataset
holds variable type data.
Definition
data.h:51
tree_node_< Node >::fit
auto fit(const Dataset &d)
tree_node_< Node >::get_linear_complexity
int get_linear_complexity() const
tree_node_< Node >::get_model
string get_model(bool pretty=false) const
tree_node_< Node >::predict
auto predict(const Dataset &d, const float **weights=nullptr)
tree_node_< Node >::tree_node_
tree_node_(const Node &val)
Definition
tree_node.h:28
tree_node_< Node >::get_tree_model
string get_tree_model(bool pretty=false, string offset="") const
tree_node_< Node >::predict
auto predict(const Dataset &d, const W **weights)
tree_node_< Node >::data
Node data
Definition
tree_node.h:39
tree_node_< Node >::prev_sibling
tree_node_< Node > * prev_sibling
Definition
tree_node.h:38
tree_node_< Node >::parent
tree_node_< Node > * parent
Definition
tree_node.h:36
tree_node_< Node >::tree_node_
tree_node_(Node &&val)
Definition
tree_node.h:32
tree_node_< Node >::tree_node_
tree_node_()
Definition
tree_node.h:24
tree_node_< Node >::get_size
int get_size(bool include_weight=true) const
tree_node_< Node >::get_complexity
int get_complexity() const
tree_node_< Node >::last_child
tree_node_< Node > * last_child
Definition
tree_node.h:37
tree_node_< Node >::first_child
tree_node_< Node > * first_child
Definition
tree_node.h:37
tree_node_< Node >::next_sibling
tree_node_< Node > * next_sibling
Definition
tree_node.h:38
tree_node_
Definition
dispatch_table.h:22
data.h
dispatch_table.h
TreeNode
class tree_node_< Node > TreeNode
Definition
dispatch_table.h:23
functions.h
init.h
Brush::dtable_predict
DispatchTable< false > dtable_predict
Definition
dispatch_table.cpp:6
Brush::from_json
void from_json(const json &j, Fitness &f)
Definition
fitness.cpp:31
Brush::to_json
void to_json(json &j, const Fitness &f)
Definition
fitness.cpp:6
Brush::dtable_fit
DispatchTable< true > dtable_fit
Definition
dispatch_table.cpp:5
node.h
nodetype.h
Brush::Node
class holding the data for a node in a tree.
Definition
node.h:89
src
program
tree_node.h
Generated by
1.17.0