ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Bagged.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "utils/Types.hpp"
4
5#include <Eigen/Dense>
6#include <cstdint>
7#include <memory>
8#include <utility>
9#include <vector>
10
11namespace ppforest2 {
32 template<typename M> struct Bagged {
33 using Ptr = std::unique_ptr<Bagged<M>>;
34
36 std::unique_ptr<M> model;
38 std::vector<int> sample_indices;
39
40 Bagged(std::unique_ptr<M> model, std::vector<int> sample_indices)
41 : model(std::move(model))
42 , sample_indices(std::move(sample_indices)) {}
43
54 template<typename Derived = M> static Ptr make(std::unique_ptr<Derived> model, std::vector<int> sample_indices) {
55 return std::make_unique<Bagged<M>>(std::move(model), std::move(sample_indices));
56 }
57
59 types::Outcome predict(types::FeatureVector const& x) const { return this->model->predict(x); }
60
62 types::OutcomeVector predict(types::FeatureMatrix const& x) const { return this->model->predict(x); }
63
75 std::vector<int> oob_indices(int n_total) const {
76 if (n_total <= 0) {
77 return {};
78 }
79
80 std::vector<std::uint8_t> in_bag(static_cast<std::size_t>(n_total), 0);
81
82 for (int const i : sample_indices) {
83 if (i >= 0 && i < n_total) {
84 in_bag[static_cast<std::size_t>(i)] = 1;
85 }
86 }
87
88 std::vector<int> oob;
89 oob.reserve(static_cast<std::size_t>(n_total));
90 for (int i = 0; i < n_total; ++i) {
91 if (in_bag[static_cast<std::size_t>(i)] == 0) {
92 oob.push_back(i);
93 }
94 }
95 return oob;
96 }
97
104 types::OutcomeVector predict_oob(types::FeatureMatrix const& x, std::vector<int> const& row_idx) const {
105 if (row_idx.empty()) {
106 return types::OutcomeVector(0);
107 }
108 return model->predict(static_cast<types::FeatureMatrix>(x(row_idx, Eigen::all)));
109 }
110
112 bool degenerate() const { return model->degenerate; }
113
126 bool operator==(Bagged const& other) const { return *model == *other.model; }
127 bool operator!=(Bagged const& other) const { return !(*this == other); }
128 };
129
147 template<typename M, typename F>
148 void for_each_bag_with_oob(std::vector<std::unique_ptr<Bagged<M>>> const& bags, int n_total, F fn) {
149 int const n = static_cast<int>(bags.size());
150 for (int k = 0; k < n; ++k) {
151 Bagged<M> const& bag = *bags[static_cast<std::size_t>(k)];
152 std::vector<int> oob_idx = bag.oob_indices(n_total);
153 if (oob_idx.empty()) {
154 continue;
155 }
156 fn(bag, oob_idx, k);
157 }
158 }
159}
Eigen::Matrix< Feature, Eigen::Dynamic, Eigen::Dynamic > FeatureMatrix
Dynamic-size matrix of feature values.
Definition Types.hpp:33
Eigen::Matrix< Outcome, Eigen::Dynamic, 1 > OutcomeVector
Dynamic-size column vector of predictions.
Definition Types.hpp:42
Eigen::Matrix< Feature, Eigen::Dynamic, 1 > FeatureVector
Dynamic-size column vector of feature values.
Definition Types.hpp:36
Feature Outcome
Scalar type for predictions (float for both classification and regression).
Definition Types.hpp:30
Binarization strategies for multiclass-to-binary reduction.
Definition Benchmark.hpp:25
void for_each_bag_with_oob(std::vector< std::unique_ptr< Bagged< M > > > const &bags, int n_total, F fn)
Apply fn to every bag in bags that has usable OOB rows.
Definition Bagged.hpp:148
Bootstrap-aggregated model wrapper.
Definition Bagged.hpp:32
static Ptr make(std::unique_ptr< Derived > model, std::vector< int > sample_indices)
Construct a Bagged<M> and return it as a Ptr.
Definition Bagged.hpp:54
std::unique_ptr< Bagged< M > > Ptr
Definition Bagged.hpp:33
bool operator!=(Bagged const &other) const
Definition Bagged.hpp:127
types::OutcomeVector predict(types::FeatureMatrix const &x) const
Delegate batch prediction to the wrapped model.
Definition Bagged.hpp:62
types::OutcomeVector predict_oob(types::FeatureMatrix const &x, std::vector< int > const &row_idx) const
Predict a subset of rows (typically OOB indices).
Definition Bagged.hpp:104
std::vector< int > sample_indices
Definition Bagged.hpp:38
std::unique_ptr< Tree > model
Definition Bagged.hpp:36
bool degenerate() const
Whether the wrapped model reported a degenerate training run.
Definition Bagged.hpp:112
std::vector< int > oob_indices(int n_total) const
Row indices of training observations not in the bootstrap sample.
Definition Bagged.hpp:75
bool operator==(Bagged const &other) const
Structural equality on the wrapped model only.
Definition Bagged.hpp:126
types::Outcome predict(types::FeatureVector const &x) const
Delegate single-row prediction to the wrapped model.
Definition Bagged.hpp:59
Bagged(std::unique_ptr< M > model, std::vector< int > sample_indices)
Definition Bagged.hpp:40