230 Eigen::Vector3d & centroid)
const
232 Eigen::Matrix<double, 1, 9, Eigen::RowMajor> accu = Eigen::Matrix<double, 1, 9, Eigen::RowMajor>::Zero ();
233 auto point_count =
static_cast<unsigned int> (examples.size ());
235 for (std::size_t i = 0; i < point_count; ++i)
249 if (point_count != 0)
251 accu /=
static_cast<double> (point_count);
252 centroid.head<3> ().matrix () = accu.tail<3> ();
253 covariance_matrix.coeffRef (0) = accu[0] - accu[6] * accu[6];
254 covariance_matrix.coeffRef (1) = accu[1] - accu[6] * accu[7];
255 covariance_matrix.coeffRef (2) = accu[2] - accu[6] * accu[8];
256 covariance_matrix.coeffRef (4) = accu[3] - accu[7] * accu[7];
257 covariance_matrix.coeffRef (5) = accu[4] - accu[7] * accu[8];
258 covariance_matrix.coeffRef (8) = accu[5] - accu[8] * accu[8];
259 covariance_matrix.coeffRef (3) = covariance_matrix.coeff (1);
260 covariance_matrix.coeffRef (6) = covariance_matrix.coeff (2);
261 covariance_matrix.coeffRef (7) = covariance_matrix.coeff (5);
274 Eigen::Vector3d & centroid)
const
276 Eigen::Matrix<double, 1, 9, Eigen::RowMajor> accu = Eigen::Matrix<double, 1, 9, Eigen::RowMajor>::Zero ();
277 auto point_count =
static_cast<unsigned int> (examples.size ());
279 for (std::size_t i = 0; i < point_count; ++i)
288 accu[6] += te.
rot_[0];
289 accu[7] += te.
rot_[1];
290 accu[8] += te.
rot_[2];
293 if (point_count != 0)
295 accu /=
static_cast<double> (point_count);
296 centroid.head<3> ().matrix () = accu.tail<3> ();
297 covariance_matrix.coeffRef (0) = accu[0] - accu[6] * accu[6];
298 covariance_matrix.coeffRef (1) = accu[1] - accu[6] * accu[7];
299 covariance_matrix.coeffRef (2) = accu[2] - accu[6] * accu[8];
300 covariance_matrix.coeffRef (4) = accu[3] - accu[7] * accu[7];
301 covariance_matrix.coeffRef (5) = accu[4] - accu[7] * accu[8];
302 covariance_matrix.coeffRef (8) = accu[5] - accu[8] * accu[8];
303 covariance_matrix.coeffRef (3) = covariance_matrix.coeff (1);
304 covariance_matrix.coeffRef (6) = covariance_matrix.coeff (2);
305 covariance_matrix.coeffRef (7) = covariance_matrix.coeff (5);
319 float computeInformationGain(DataSet & data_set, std::vector<ExampleIndex> & examples, std::vector<LabelDataType> & label_data,
320 std::vector<float> & results, std::vector<unsigned char> & flags,
const float threshold)
const override
322 const std::size_t num_of_examples = examples.size ();
326 std::vector < LabelDataType > sums (num_of_branches + 1, 0.f);
327 std::vector < LabelDataType > sqr_sums (num_of_branches + 1, 0.f);
328 std::vector < std::size_t > branch_element_count (num_of_branches + 1, 0.f);
330 for (std::size_t branch_index = 0; branch_index < num_of_branches; ++branch_index)
332 branch_element_count[branch_index] = 1;
333 ++branch_element_count[num_of_branches];
336 for (std::size_t example_index = 0; example_index < num_of_examples; ++example_index)
338 unsigned char branch_index;
339 computeBranchIndex (results[example_index], flags[example_index], threshold, branch_index);
341 LabelDataType label = label_data[example_index];
343 ++branch_element_count[branch_index];
344 ++branch_element_count[num_of_branches];
346 sums[branch_index] += label;
347 sums[num_of_branches] += label;
350 std::vector<float> hp (num_of_branches + 1, 0.f);
351 for (std::size_t branch_index = 0; branch_index < (num_of_branches + 1); ++branch_index)
353 float pf = sums[branch_index] /
static_cast<float> (branch_element_count[branch_index]);
354 float pnf = (
static_cast<LabelDataType
>(branch_element_count[branch_index]) - sums[branch_index] + 1.f)
355 /
static_cast<LabelDataType
> (branch_element_count[branch_index]);
356 hp[branch_index] -=
static_cast<float>(pf * std::log (pf) + pnf * std::log (pnf));
360 float purity = sums[num_of_branches] /
static_cast<LabelDataType
>(branch_element_count[num_of_branches]);
367 std::vector < std::size_t > branch_element_count (num_of_branches + 1, 0);
368 std::vector < std::vector<ExampleIndex> > positive_examples;
369 positive_examples.resize (num_of_branches + 1);
372 for (std::size_t example_index = 0; example_index < num_of_examples; ++example_index)
374 unsigned char branch_index;
375 computeBranchIndex (results[example_index], flags[example_index], threshold, branch_index);
377 LabelDataType label = label_data[example_index];
381 ++branch_element_count[branch_index];
382 ++branch_element_count[num_of_branches];
384 positive_examples[branch_index].push_back (examples[example_index]);
385 positive_examples[num_of_branches].push_back (examples[example_index]);
391 std::vector < Eigen::Matrix3d > offset_covariances;
392 std::vector < Eigen::Matrix3d > angle_covariances;
394 std::vector < Eigen::Vector3d > offset_centroids;
395 std::vector < Eigen::Vector3d > angle_centroids;
397 offset_covariances.resize (num_of_branches + 1);
398 angle_covariances.resize (num_of_branches + 1);
399 offset_centroids.resize (num_of_branches + 1);
400 angle_centroids.resize (num_of_branches + 1);
402 for (std::size_t branch_index = 0; branch_index < (num_of_branches + 1); ++branch_index)
405 offset_centroids[branch_index]);
407 angle_centroids[branch_index]);
411 std::vector<float> hr (num_of_branches + 1, 0.f);
412 for (std::size_t branch_index = 0; branch_index < (num_of_branches + 1); ++branch_index)
414 hr[branch_index] =
static_cast<float>(0.5f * std::log (std::pow (2 *
M_PI, 3)
415 * offset_covariances[branch_index].determinant ())
416 + 0.5f * std::log (std::pow (2 *
M_PI, 3)
417 * angle_covariances[branch_index].determinant ()));
420 for (std::size_t branch_index = 0; branch_index < (num_of_branches + 1); ++branch_index)
422 hp[branch_index] += std::max (sums[branch_index] /
static_cast<float> (branch_element_count[branch_index]) - tp, 0.f) * hr[branch_index];
426 float information_gain = hp[num_of_branches + 1];
427 for (std::size_t branch_index = 0; branch_index < (num_of_branches); ++branch_index)
429 information_gain -=
static_cast<float> (branch_element_count[branch_index]) /
static_cast<float> (branch_element_count[num_of_branches])
433 return information_gain;
473 void computeAndSetNodeStats(DataSet & data_set, std::vector<ExampleIndex> & examples, std::vector<LabelDataType> & label_data, NodeType & node)
const override
475 const std::size_t num_of_examples = examples.size ();
477 LabelDataType sum = 0.0f;
478 LabelDataType sqr_sum = 0.0f;
479 for (std::size_t example_index = 0; example_index < num_of_examples; ++example_index)
481 const LabelDataType label = label_data[example_index];
484 sqr_sum += label * label;
487 sum /=
static_cast<float>(num_of_examples);
488 sqr_sum /=
static_cast<float>(num_of_examples);
490 const float variance = sqr_sum - sum * sum;
493 node.variance = variance;
496 std::vector < ExampleIndex > positive_examples;
498 for (std::size_t example_index = 0; example_index < num_of_examples; ++example_index)
500 LabelDataType label = label_data[example_index];
503 positive_examples.push_back (examples[example_index]);