232 Eigen::Vector3d & centroid)
const
234 Eigen::Matrix<double, 1, 9, Eigen::RowMajor> accu = Eigen::Matrix<double, 1, 9, Eigen::RowMajor>::Zero ();
235 unsigned int point_count =
static_cast<unsigned int> (examples.size ());
237 for (std::size_t i = 0; i < point_count; ++i)
251 if (point_count != 0)
253 accu /=
static_cast<double> (point_count);
254 centroid.head<3> ().matrix () = accu.tail<3> ();
255 covariance_matrix.coeffRef (0) = accu[0] - accu[6] * accu[6];
256 covariance_matrix.coeffRef (1) = accu[1] - accu[6] * accu[7];
257 covariance_matrix.coeffRef (2) = accu[2] - accu[6] * accu[8];
258 covariance_matrix.coeffRef (4) = accu[3] - accu[7] * accu[7];
259 covariance_matrix.coeffRef (5) = accu[4] - accu[7] * accu[8];
260 covariance_matrix.coeffRef (8) = accu[5] - accu[8] * accu[8];
261 covariance_matrix.coeffRef (3) = covariance_matrix.coeff (1);
262 covariance_matrix.coeffRef (6) = covariance_matrix.coeff (2);
263 covariance_matrix.coeffRef (7) = covariance_matrix.coeff (5);
276 Eigen::Vector3d & centroid)
const
278 Eigen::Matrix<double, 1, 9, Eigen::RowMajor> accu = Eigen::Matrix<double, 1, 9, Eigen::RowMajor>::Zero ();
279 unsigned int point_count =
static_cast<unsigned int> (examples.size ());
281 for (std::size_t i = 0; i < point_count; ++i)
290 accu[6] += te.
rot_[0];
291 accu[7] += te.
rot_[1];
292 accu[8] += te.
rot_[2];
295 if (point_count != 0)
297 accu /=
static_cast<double> (point_count);
298 centroid.head<3> ().matrix () = accu.tail<3> ();
299 covariance_matrix.coeffRef (0) = accu[0] - accu[6] * accu[6];
300 covariance_matrix.coeffRef (1) = accu[1] - accu[6] * accu[7];
301 covariance_matrix.coeffRef (2) = accu[2] - accu[6] * accu[8];
302 covariance_matrix.coeffRef (4) = accu[3] - accu[7] * accu[7];
303 covariance_matrix.coeffRef (5) = accu[4] - accu[7] * accu[8];
304 covariance_matrix.coeffRef (8) = accu[5] - accu[8] * accu[8];
305 covariance_matrix.coeffRef (3) = covariance_matrix.coeff (1);
306 covariance_matrix.coeffRef (6) = covariance_matrix.coeff (2);
307 covariance_matrix.coeffRef (7) = covariance_matrix.coeff (5);
321 float computeInformationGain(DataSet & data_set, std::vector<ExampleIndex> & examples, std::vector<LabelDataType> & label_data,
322 std::vector<float> & results, std::vector<unsigned char> & flags,
const float threshold)
const override
324 const std::size_t num_of_examples = examples.size ();
328 std::vector < LabelDataType > sums (num_of_branches + 1, 0.f);
329 std::vector < LabelDataType > sqr_sums (num_of_branches + 1, 0.f);
330 std::vector < std::size_t > branch_element_count (num_of_branches + 1, 0.f);
332 for (std::size_t branch_index = 0; branch_index < num_of_branches; ++branch_index)
334 branch_element_count[branch_index] = 1;
335 ++branch_element_count[num_of_branches];
338 for (std::size_t example_index = 0; example_index < num_of_examples; ++example_index)
340 unsigned char branch_index;
341 computeBranchIndex (results[example_index], flags[example_index], threshold, branch_index);
343 LabelDataType label = label_data[example_index];
345 ++branch_element_count[branch_index];
346 ++branch_element_count[num_of_branches];
348 sums[branch_index] += label;
349 sums[num_of_branches] += label;
352 std::vector<float> hp (num_of_branches + 1, 0.f);
353 for (std::size_t branch_index = 0; branch_index < (num_of_branches + 1); ++branch_index)
355 float pf = sums[branch_index] /
static_cast<float> (branch_element_count[branch_index]);
356 float pnf = (
static_cast<LabelDataType
>(branch_element_count[branch_index]) - sums[branch_index] + 1.f)
357 /
static_cast<LabelDataType
> (branch_element_count[branch_index]);
358 hp[branch_index] -=
static_cast<float>(pf * std::log (pf) + pnf * std::log (pnf));
362 float purity = sums[num_of_branches] /
static_cast<LabelDataType
>(branch_element_count[num_of_branches]);
369 std::vector < std::size_t > branch_element_count (num_of_branches + 1, 0);
370 std::vector < std::vector<ExampleIndex> > positive_examples;
371 positive_examples.resize (num_of_branches + 1);
374 for (std::size_t example_index = 0; example_index < num_of_examples; ++example_index)
376 unsigned char branch_index;
377 computeBranchIndex (results[example_index], flags[example_index], threshold, branch_index);
379 LabelDataType label = label_data[example_index];
383 ++branch_element_count[branch_index];
384 ++branch_element_count[num_of_branches];
386 positive_examples[branch_index].push_back (examples[example_index]);
387 positive_examples[num_of_branches].push_back (examples[example_index]);
393 std::vector < Eigen::Matrix3d > offset_covariances;
394 std::vector < Eigen::Matrix3d > angle_covariances;
396 std::vector < Eigen::Vector3d > offset_centroids;
397 std::vector < Eigen::Vector3d > angle_centroids;
399 offset_covariances.resize (num_of_branches + 1);
400 angle_covariances.resize (num_of_branches + 1);
401 offset_centroids.resize (num_of_branches + 1);
402 angle_centroids.resize (num_of_branches + 1);
404 for (std::size_t branch_index = 0; branch_index < (num_of_branches + 1); ++branch_index)
407 offset_centroids[branch_index]);
409 angle_centroids[branch_index]);
413 std::vector<float> hr (num_of_branches + 1, 0.f);
414 for (std::size_t branch_index = 0; branch_index < (num_of_branches + 1); ++branch_index)
416 hr[branch_index] =
static_cast<float>(0.5f * std::log (std::pow (2 *
M_PI, 3)
417 * offset_covariances[branch_index].determinant ())
418 + 0.5f * std::log (std::pow (2 *
M_PI, 3)
419 * angle_covariances[branch_index].determinant ()));
422 for (std::size_t branch_index = 0; branch_index < (num_of_branches + 1); ++branch_index)
424 hp[branch_index] += std::max (sums[branch_index] /
static_cast<float> (branch_element_count[branch_index]) - tp, 0.f) * hr[branch_index];
428 float information_gain = hp[num_of_branches + 1];
429 for (std::size_t branch_index = 0; branch_index < (num_of_branches); ++branch_index)
431 information_gain -=
static_cast<float> (branch_element_count[branch_index]) /
static_cast<float> (branch_element_count[num_of_branches])
435 return information_gain;
475 void computeAndSetNodeStats(DataSet & data_set, std::vector<ExampleIndex> & examples, std::vector<LabelDataType> & label_data, NodeType & node)
const override
477 const std::size_t num_of_examples = examples.size ();
479 LabelDataType sum = 0.0f;
480 LabelDataType sqr_sum = 0.0f;
481 for (std::size_t example_index = 0; example_index < num_of_examples; ++example_index)
483 const LabelDataType label = label_data[example_index];
486 sqr_sum += label * label;
489 sum /=
static_cast<float>(num_of_examples);
490 sqr_sum /=
static_cast<float>(num_of_examples);
492 const float variance = sqr_sum - sum * sum;
495 node.variance = variance;
498 std::vector < ExampleIndex > positive_examples;
500 for (std::size_t example_index = 0; example_index < num_of_examples; ++example_index)
502 LabelDataType label = label_data[example_index];
505 positive_examples.push_back (examples[example_index]);