forked from ExclusiveOrange/ExtremelyRandomTrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndectree.hpp
207 lines (169 loc) · 5.47 KB
/
ndectree.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// ndectree.hpp - 2016 - Atlee Brink
// Decision Tree module
#pragma once
#include "nutil.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <utility>
#include <vector>
namespace ndectree {
using namespace std;
const string STR_LEAF = "\\";
const string STR_BRANCH = "+";
const string STR_DEPTH = "|";
template< typename ATTR_T >
struct dectree_t {
bool isleaf; // else is branch
union {
vector< size_t > classfreqs; // if leaf
struct { // if branch
size_t attrindex;
ATTR_T splitvalue;
dectree_t *left, *right;
};
};
dectree_t( // leaf constructor
vector< size_t > &&classfreqs
) : isleaf( true ),
classfreqs( classfreqs )
{}
dectree_t( // branch constructor
size_t attrindex,
ATTR_T splitvalue,
dectree_t *left,
dectree_t *right
) : isleaf( false ),
attrindex( attrindex ),
splitvalue( splitvalue ),
left( left ),
right( right )
{}
~dectree_t() {
if( isleaf ) classfreqs.~vector<size_t>();
else {
delete left;
delete right;
}
}
void
storetostream(
ostream &out
) const {
if( isleaf ) {
out << STR_LEAF << " ";
for( size_t i = 0; i + 1 < classfreqs.size(); i++ ) {
out << classfreqs[i] << " ";
}
out << classfreqs.back() << "\n";
}
else {
out << STR_BRANCH << " " << attrindex
<< " " << splitvalue << "\n";
left->storetostream( out );
right->storetostream( out );
}
}
static
dectree_t*
loadfromstream(
istream &in,
size_t numclasses
) {
try {
string str;
// get type marker
in >> str;
if( str == STR_LEAF ) {
vector< size_t > classfreqs;
classfreqs.reserve( numclasses );
for( size_t c = 0; c < numclasses; c++ ) {
size_t freq;
in >> freq;
classfreqs.push_back( freq );
}
return new dectree_t( move( classfreqs ) );
}
else if( str == STR_BRANCH ) {
size_t attrindex;
in >> attrindex;
ATTR_T splitvalue;
in >> splitvalue;
dectree_t *left = loadfromstream( in, numclasses );
if( left == nullptr ) return nullptr;
dectree_t *right = loadfromstream( in, numclasses );
if( right == nullptr ) {
delete left;
return nullptr;
}
return new dectree_t( attrindex, splitvalue, left, right );
}
}
catch( ... ) {
cerr << "dectree_t::loadfromstream: error reading from stream" << endl;
}
return nullptr;
}
};
template< typename ATTR_T, typename LABEL_T >
struct forest_t {
vector< dectree_t< ATTR_T >* > trees;
vector< LABEL_T > indextolabel;
forest_t() {}
forest_t( forest_t &&other ) {
swap( trees, other.trees );
swap( indextolabel, other.indextolabel );
}
forest_t(
vector< dectree_t< ATTR_T >* > &&trees,
vector< LABEL_T > &&indextolabel
) : trees( trees ),
indextolabel( indextolabel )
{}
~forest_t() {
for( auto tree : trees ) delete tree;
}
forest_t&
operator=( forest_t &&other ) {
swap( trees, other.trees );
swap( indextolabel, other.indextolabel );
return *this;
}
LABEL_T
classify(
const vector< ATTR_T > featurevector
) const {
vector< size_t > labelcounts( indextolabel.size(), 0 );
for( dectree_t< ATTR_T > *pdectree : trees ) {
auto pt = pdectree;
while( !pt->isleaf ) {
ATTR_T a = featurevector[ pt->attrindex ];
pt = a < pt->splitvalue ? pt->left : pt->right;
}
size_t maxfreq = 0;
size_t maxlabelindex = 0;
size_t labelindex = 0;
for( size_t freq : pt->classfreqs ) {
if( freq > maxfreq ) {
maxfreq = freq;
maxlabelindex = labelindex;
}
labelindex++;
}
labelcounts[ maxlabelindex ]++;
}
size_t maxcount = 0;
size_t maxcountindex = 0;
size_t countindex = 0;
for( size_t count : labelcounts ) {
if( count > maxcount ) {
maxcount = count;
maxcountindex = countindex;
}
countindex++;
}
return indextolabel[ maxcountindex ];
}
};
}