-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecision-tree.cpp
270 lines (220 loc) · 8.48 KB
/
decision-tree.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <queue>
#include <cmath>
#include <vector>
#include <iomanip>
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
using std::pair;
using std::queue;
using std::vector;
using std::distance;
using predicate_t = pair<unsigned, double>;
static constexpr double EPSILON = 1e-15;
static constexpr predicate_t null_predicate = {0, -std::numeric_limits<double>::max()};
struct object {
object(vector<double> features, unsigned int class_id) : features(move(features)), class_id(class_id) {}
template<class RandomIt>
static void sort_feature(RandomIt first, RandomIt last, size_t i) {
sort(first, last, [i](const auto& a, const auto& b) { return a.features[i] < b.features[i]; });
}
vector<double> features;
unsigned int class_id;
};
using dataset_t = vector<object>;
using dataset_ptr = dataset_t::iterator;
void update_gini_variable(size_t &val, long long &sum, int delta) {
sum -= val * val;
val += delta;
sum += val * val;
}
predicate_t gini_predicate(dataset_ptr begin, dataset_ptr end, size_t feature_size, size_t class_size) {
predicate_t best_predicate = null_predicate;
double best_gain = null_predicate.second;
for (size_t i = 0; i < feature_size; ++i) {
object::sort_feature(begin, end, i);
if (begin->features[i] == (end - 1)->features[i]) continue;
vector<size_t> l_count(class_size);
vector<size_t> r_count(class_size);
size_t l_size = 0;
size_t r_size = distance(begin, end);
long long l_sum = 0;
long long r_sum = 0;
for (auto j = begin; j != end; ++j) {
update_gini_variable(r_count[j->class_id], r_sum, 1);
}
for (auto j = begin + 1; j != end; ++j) {
auto previous_class = (j - 1)->class_id;
update_gini_variable(r_count[previous_class], r_sum, -1);
update_gini_variable(l_count[previous_class], l_sum, 1);
++l_size;
--r_size;
auto current_value = j->features[i];
auto previous_value = (j - 1)->features[i];
if (previous_value != current_value) {
double gain = 1.0 * l_sum / l_size + 1.0 * r_sum / r_size;
if (gain > best_gain) {
best_gain = gain;
best_predicate = {i, (previous_value + current_value) / 2.0};
}
}
}
}
return best_predicate;
}
double entropy(const vector<size_t>& counts, size_t s) {
double sum = 0;
for (auto i : counts) {
if (i != 0) {
sum -= (i * 1.0 / s) * log(i * 1.0 / s);
}
}
return sum;
}
predicate_t entropy_predicate(dataset_ptr begin, dataset_ptr end, size_t feature_size, size_t class_size) {
predicate_t best_predicate = null_predicate;
double best_gain = null_predicate.second;
for (unsigned i = 0; i < feature_size; ++i) {
object::sort_feature(begin, end, i);
if (begin->features[i] == (end - 1)->features[i]) continue;
vector<size_t> l_count(class_size);
vector<size_t> r_count(class_size);
size_t l_size = 0;
size_t r_size = distance(begin, end);
for (auto i = begin; i != end; ++i) {
++r_count[i->class_id];
}
for (auto j = begin + 1; j != end; ++j) {
auto previous = (j - 1)->class_id;
--r_count[previous];
++l_count[previous];
++l_size;
--r_size;
auto current_value = j->features[i];
auto previous_value = (j - 1)->features[i];
if (previous_value == current_value) continue;
double gain = -entropy(l_count, l_size) * l_size - entropy(r_count, r_size) * r_size;
if (gain > best_gain) {
best_gain = gain;
best_predicate = {i, (previous_value + current_value) / 2.0};
}
}
}
return best_predicate;
}
struct decision_tree {
const decision_tree* left;
const decision_tree* right;
const size_t size;
const unsigned int class_id;
const predicate_t predicate;
decision_tree(decision_tree* left, decision_tree* right, predicate_t predicate) :
left(left),
right(right),
size((left ? left->size : 0) + (right ? right->size : 0) + 1),
class_id(std::numeric_limits<unsigned int>::max()),
predicate(move(predicate)) {}
explicit decision_tree(unsigned int class_id) :
left(nullptr),
right(nullptr),
size(1),
class_id(class_id),
predicate() {}
~decision_tree() {
delete left;
delete right;
}
static decision_tree* make_tree(const dataset_t& train_set, size_t class_size, int max_level) {
return decision_tree_builder(class_size, train_set, max_level).build_tree();
}
void print() const {
cout << std::fixed << std::setprecision(10) << size << '\n';
queue<const decision_tree*> nodes;
nodes.emplace(this);
int node_index = 1;
while (!nodes.empty()) {
const auto& node = nodes.front();
if (node->size == 1) {
cout << "C " << node->class_id + 1 << '\n';
} else {
cout << "Q " << node->predicate.first + 1 << ' ' << node->predicate.second << ' '
<< ++node_index << ' ';
cout << ++node_index << '\n';
nodes.emplace(node->right);
nodes.emplace(node->left);
}
nodes.pop();
}
}
class decision_tree_builder {
private:
const size_t class_size;
dataset_t objects;
size_t max_level;
vector<unsigned int> class_distribution(dataset_ptr begin, dataset_ptr end) const {
vector<unsigned int> distribution(class_size);
for (auto object = begin; object != end; ++object) {
++distribution[object->class_id];
}
return distribution;
}
decision_tree* get_leaf(dataset_ptr begin, dataset_ptr end) {
auto distribution = class_distribution(begin, end);
auto max_iterator = std::max_element(distribution.begin(), distribution.end());
unsigned int most_common_class = static_cast<unsigned int>(distance(distribution.begin(), max_iterator));
return new decision_tree(most_common_class);
}
public:
decision_tree_builder(const size_t class_size, dataset_t objects, size_t max_level) :
class_size(class_size),
objects(move(objects)),
max_level(max_level) {}
decision_tree* build_tree() {
return build_tree(objects.begin(), objects.end());
}
decision_tree* build_tree(dataset_ptr begin, dataset_ptr end, size_t level = 0) {
const size_t features_size = begin->features.size();
auto class_not_eq = [](dataset_t::const_reference a, dataset_t::const_reference b) {
return a.class_id != b.class_id;
};
if (adjacent_find(begin, end, class_not_eq) == end) {
return new decision_tree(begin->class_id);
}
if (level == max_level) {
return get_leaf(begin, end);
}
auto get_predicate = objects.size() < 1000 ? entropy_predicate : gini_predicate;
predicate_t predicate = get_predicate(begin, end, features_size, class_size);
if (predicate == null_predicate) {
return get_leaf(begin, end);
}
auto middle = std::partition(begin, end, [&predicate](dataset_t::const_reference obj) {
return obj.features[predicate.first] > predicate.second;
});
return new decision_tree(
build_tree(begin, middle, level + 1),
build_tree(middle, end, level + 1),
predicate
);
}
};
};
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(nullptr);
size_t features_size, class_size, max_level, objects_size;
cin >> features_size >> class_size >> max_level >> objects_size;
dataset_t train_set(objects_size, {vector<double>(features_size), 0});
for (auto& train_object : train_set) {
for (auto& f : train_object.features) {
cin >> f;
}
cin >> train_object.class_id;
--train_object.class_id;
}
decision_tree* dt = decision_tree::make_tree(train_set, class_size, max_level);
dt->print();
delete dt;
return 0;
}