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]);