ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Forest.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "models/Bagged.hpp"
4#include "models/Model.hpp"
5#include "models/Tree.hpp"
6#include "stats/Stats.hpp"
7
8#include <memory>
9#include <vector>
10
11namespace ppforest2 {
31 class Forest : public Model {
32 public:
33 using Ptr = std::unique_ptr<Forest>;
34 using Model::predict;
35
40 using RNG = stats::RNG;
41
44 std::vector<BaggedTree::Ptr> trees;
45
55 static Ptr train(TrainingSpec const& spec, FeatureMatrix const& x, OutcomeVector const& y);
56
58 Outcome predict(FeatureVector const& x) const override = 0;
59
61 void accept(Model::Visitor& visitor) const override = 0;
62
75
76 bool operator==(Forest const& other) const;
77 bool operator!=(Forest const& other) const;
78
79 protected:
80 Forest() = default;
81 explicit Forest(TrainingSpec::Ptr spec) { training_spec = std::move(spec); };
82
95 virtual BaggedTree::Ptr train_tree(FeatureMatrix const& x, OutcomeVector const& y, RNG& rng) const = 0;
96
117 };
118}
stats::RNG RNG
Definition Forest.hpp:40
void add_tree(BaggedTree::Ptr tree)
Add a trained bagged tree to the forest.
types::FeatureMatrix FeatureMatrix
Definition Forest.hpp:36
std::vector< BaggedTree::Ptr > trees
Bootstrap-aggregated trees. Each BaggedTree pairs the polymorphic inner Tree with its sample indices.
Definition Forest.hpp:44
void accept(Model::Visitor &visitor) const override=0
Accept a model visitor (mode-specific dispatch).
types::OutcomeVector OutcomeVector
Definition Forest.hpp:38
types::Outcome Outcome
Definition Forest.hpp:39
void build_trees(types::FeatureMatrix const &x, types::OutcomeVector const &y)
Build training_spec->size bagged trees in parallel and attach them to this forest.
virtual BaggedTree::Ptr train_tree(FeatureMatrix const &x, OutcomeVector const &y, RNG &rng) const =0
Train one bagged tree on a bootstrap resample of x / y.
static Ptr train(TrainingSpec const &spec, FeatureMatrix const &x, OutcomeVector const &y)
Train a random forest.
bool operator==(Forest const &other) const
types::FeatureVector FeatureVector
Definition Forest.hpp:37
Forest(TrainingSpec::Ptr spec)
Definition Forest.hpp:81
std::unique_ptr< Forest > Ptr
Definition Forest.hpp:33
Outcome predict(FeatureVector const &x) const override=0
Per-row prediction (mode-specific: majority vote or mean).
bool operator!=(Forest const &other) const
Visitor interface for model dispatch.
Definition Model.hpp:51
Abstract base class for predictive models (trees and forests).
Definition Model.hpp:29
virtual types::Outcome predict(types::FeatureVector const &x) const =0
Predict a single observation.
TrainingSpec::Ptr training_spec
Training specification used to build this model.
Definition Model.hpp:70
Training configuration for projection pursuit trees and forests.
Definition TrainingSpec.hpp:43
std::shared_ptr< TrainingSpec > Ptr
Definition TrainingSpec.hpp:45
pcg32 RNG
Definition Stats.hpp:24
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
std::unique_ptr< Bagged< Tree > > Ptr
Definition Bagged.hpp:33