38 ForestContainer(
int num_trees,
int output_dimension = 1,
bool is_leaf_constant =
true,
bool is_exponentiated =
false);
48 ForestContainer(
int num_samples,
int num_trees,
int output_dimension = 1,
bool is_leaf_constant =
true,
bool is_exponentiated =
false);
113 std::vector<double> PredictRawSingleTree(
ForestDataset& dataset,
int forest_num,
int tree_num);
114 void PredictInPlace(
ForestDataset& dataset, std::vector<double>& output);
115 void PredictRawInPlace(
ForestDataset& dataset, std::vector<double>& output);
116 void PredictRawInPlace(
ForestDataset& dataset,
int forest_num, std::vector<double>& output);
117 void PredictRawSingleTreeInPlace(
ForestDataset& dataset,
int forest_num,
int tree_num, std::vector<double>& output);
118 void PredictLeafIndicesInplace(Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>& covariates,
119 Eigen::Map<Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>& output,
120 std::vector<int>& forest_indices,
int num_trees, data_size_t n);
122 inline TreeEnsemble* GetEnsemble(
int i) {
return forests_[i].get();}
123 inline int32_t NumSamples() {
return num_samples_;}
124 inline int32_t NumTrees() {
return num_trees_;}
125 inline int32_t NumTrees(
int ensemble_num) {
return forests_[ensemble_num]->NumTrees();}
126 inline int32_t NumLeaves(
int ensemble_num) {
return forests_[ensemble_num]->NumLeaves();}
127 inline int32_t EnsembleTreeMaxDepth(
int ensemble_num,
int tree_num) {
return forests_[ensemble_num]->TreeMaxDepth(tree_num);}
128 inline double EnsembleAverageMaxDepth(
int ensemble_num) {
return forests_[ensemble_num]->AverageMaxDepth();}
129 inline double AverageMaxDepth() {
130 double numerator = 0.;
131 double denominator = 0.;
132 for (
int i = 0; i < num_samples_; i++) {
133 for (
int j = 0; j < num_trees_; j++) {
134 numerator +=
static_cast<double>(forests_[i]->TreeMaxDepth(j));
138 return numerator / denominator;
140 inline int32_t OutputDimension() {
return output_dimension_;}
141 inline int32_t OutputDimension(
int ensemble_num) {
return forests_[ensemble_num]->OutputDimension();}
142 inline bool IsLeafConstant() {
return is_leaf_constant_;}
143 inline bool IsLeafConstant(
int ensemble_num) {
return forests_[ensemble_num]->IsLeafConstant();}
144 inline bool IsExponentiated() {
return is_exponentiated_;}
145 inline bool IsExponentiated(
int ensemble_num) {
return forests_[ensemble_num]->IsExponentiated();}
146 inline bool AllRoots(
int ensemble_num) {
return forests_[ensemble_num]->AllRoots();}
147 inline void SetLeafValue(
int ensemble_num,
double leaf_value) {forests_[ensemble_num]->SetLeafValue(leaf_value);}
148 inline void SetLeafVector(
int ensemble_num, std::vector<double>& leaf_vector) {forests_[ensemble_num]->SetLeafVector(leaf_vector);}
149 inline void IncrementSampleCount() {num_samples_++;}
151 void SaveToJsonFile(std::string filename) {
152 nlohmann::json model_json = this->
to_json();
153 std::ofstream output_file(filename);
154 output_file << model_json << std::endl;
157 void LoadFromJsonFile(std::string filename) {
158 std::ifstream f(filename);
159 nlohmann::json file_tree_json = nlohmann::json::parse(f);
164 std::string DumpJsonString() {
165 nlohmann::json model_json = this->
to_json();
166 return model_json.dump();
169 void LoadFromJsonString(std::string& json_string) {
170 nlohmann::json file_tree_json = nlohmann::json::parse(json_string);
179 output_dimension_ = 0;
180 is_leaf_constant_ = 0;
181 initialized_ =
false;
187 void from_json(
const nlohmann::json& forest_container_json);
192 std::vector<std::unique_ptr<TreeEnsemble>> forests_;
195 int output_dimension_;
196 bool is_exponentiated_{
false};
197 bool is_leaf_constant_;
198 bool initialized_{
false};
ForestContainer(int num_samples, int num_trees, int output_dimension=1, bool is_leaf_constant=true, bool is_exponentiated=false)
Construct a new ForestContainer object.