42 template <
class FeatureType,
50 , num_of_features_(1000)
51 , num_of_thresholds_(10)
52 , feature_handler_(nullptr)
53 , stats_estimator_(nullptr)
57 , decision_tree_trainer_data_provider_()
58 , random_features_at_split_node_(false)
61 template <
class FeatureType,
70 template <
class FeatureType,
80 std::vector<FeatureType> features;
82 if (!random_features_at_split_node_)
83 feature_handler_->createRandomFeatures(num_of_features_, features);
89 if (decision_tree_trainer_data_provider_) {
90 std::cerr <<
"use decision_tree_trainer_data_provider_" << std::endl;
92 decision_tree_trainer_data_provider_->getDatasetAndLabels(
93 data_set_, label_data_, examples_);
94 trainDecisionTreeNode(
95 features, examples_, label_data_, max_tree_depth_, tree.
getRoot());
101 trainDecisionTreeNode(
102 features, examples_, label_data_, max_tree_depth_, tree.
getRoot());
106 template <
class FeatureType,
114 std::vector<ExampleIndex>& examples,
115 std::vector<LabelType>& label_data,
116 const std::size_t max_depth,
119 const std::size_t num_of_examples = examples.size();
120 if (num_of_examples == 0) {
122 "Reached invalid point in decision tree training: Number of examples is 0!\n");
126 if (max_depth == 0) {
127 stats_estimator_->computeAndSetNodeStats(data_set_, examples, label_data, node);
131 if (examples.size() < min_examples_for_split_) {
132 stats_estimator_->computeAndSetNodeStats(data_set_, examples, label_data, node);
136 if (random_features_at_split_node_) {
138 feature_handler_->createRandomFeatures(num_of_features_, features);
141 std::vector<float> feature_results;
142 std::vector<unsigned char> flags;
144 feature_results.reserve(num_of_examples);
145 flags.reserve(num_of_examples);
148 int best_feature_index = -1;
149 float best_feature_threshold = 0.0f;
150 float best_feature_information_gain = 0.0f;
152 const std::size_t num_of_features = features.size();
153 for (std::size_t feature_index = 0; feature_index < num_of_features;
156 feature_handler_->evaluateFeature(
157 features[feature_index], data_set_, examples, feature_results, flags);
160 if (!thresholds_.empty()) {
163 for (std::size_t threshold_index = 0; threshold_index < thresholds_.size();
166 const float information_gain =
167 stats_estimator_->computeInformationGain(data_set_,
172 thresholds_[threshold_index]);
174 if (information_gain > best_feature_information_gain) {
175 best_feature_information_gain = information_gain;
176 best_feature_index =
static_cast<int>(feature_index);
177 best_feature_threshold = thresholds_[threshold_index];
182 std::vector<float> thresholds;
183 thresholds.reserve(num_of_thresholds_);
184 createThresholdsUniform(num_of_thresholds_, feature_results, thresholds);
188 for (std::size_t threshold_index = 0; threshold_index < num_of_thresholds_;
190 const float threshold = thresholds[threshold_index];
193 const float information_gain = stats_estimator_->computeInformationGain(
194 data_set_, examples, label_data, feature_results, flags, threshold);
196 if (information_gain > best_feature_information_gain) {
197 best_feature_information_gain = information_gain;
198 best_feature_index =
static_cast<int>(feature_index);
199 best_feature_threshold = threshold;
205 if (best_feature_index == -1) {
206 stats_estimator_->computeAndSetNodeStats(data_set_, examples, label_data, node);
211 std::vector<unsigned char> branch_indices;
212 branch_indices.reserve(num_of_examples);
214 feature_handler_->evaluateFeature(
215 features[best_feature_index], data_set_, examples, feature_results, flags);
217 stats_estimator_->computeBranchIndices(
218 feature_results, flags, best_feature_threshold, branch_indices);
221 stats_estimator_->computeAndSetNodeStats(data_set_, examples, label_data, node);
225 const std::size_t num_of_branches = stats_estimator_->getNumOfBranches();
227 std::vector<std::size_t> branch_counts(num_of_branches, 0);
228 for (std::size_t example_index = 0; example_index < num_of_examples;
230 ++branch_counts[branch_indices[example_index]];
233 node.feature = features[best_feature_index];
234 node.threshold = best_feature_threshold;
235 node.sub_nodes.resize(num_of_branches);
237 for (std::size_t branch_index = 0; branch_index < num_of_branches; ++branch_index) {
238 if (branch_counts[branch_index] == 0) {
239 NodeType branch_node;
240 stats_estimator_->computeAndSetNodeStats(
241 data_set_, examples, label_data, branch_node);
244 node.sub_nodes[branch_index] = branch_node;
249 std::vector<LabelType> branch_labels;
250 std::vector<ExampleIndex> branch_examples;
251 branch_labels.reserve(branch_counts[branch_index]);
252 branch_examples.reserve(branch_counts[branch_index]);
254 for (std::size_t example_index = 0; example_index < num_of_examples;
256 if (branch_indices[example_index] == branch_index) {
257 branch_examples.push_back(examples[example_index]);
258 branch_labels.push_back(label_data[example_index]);
262 trainDecisionTreeNode(features,
266 node.sub_nodes[branch_index]);
271 template <
class FeatureType,
279 std::vector<float>& values,
280 std::vector<float>& thresholds)
283 float min_value = ::std::numeric_limits<float>::max();
284 float max_value = -::std::numeric_limits<float>::max();
286 const std::size_t num_of_values = values.size();
287 for (std::size_t value_index = 0; value_index < num_of_values; ++value_index) {
288 const float value = values[value_index];
290 if (value < min_value)
292 if (value > max_value)
296 const float range = max_value - min_value;
297 const float step = range /
static_cast<float>(num_of_thresholds + 2);
300 thresholds.resize(num_of_thresholds);
302 for (std::size_t threshold_index = 0; threshold_index < num_of_thresholds;
304 thresholds[threshold_index] =
305 min_value + step * (
static_cast<float>(threshold_index + 1));