33 ForestContainer(
int num_trees,
int output_dimension = 1,
bool is_leaf_constant =
true,
bool is_exponentiated =
false);
43 ForestContainer(
int num_samples,
int num_trees,
int output_dimension = 1,
bool is_leaf_constant =
true,
bool is_exponentiated =
false);
51 void MergeForests(
int inbound_forest_index,
int outbound_forest_index) {
52 forests_[inbound_forest_index]->MergeForest(*forests_[outbound_forest_index]);
61 forests_[forest_index]->AddValueToLeaves(constant_value);
70 forests_[forest_index]->MultiplyLeavesByValue(constant_multiple);
135 std::vector<double> PredictRawSingleTree(
ForestDataset& dataset,
int forest_num,
int tree_num);
136 void PredictInPlace(
ForestDataset& dataset, std::vector<double>& output);
137 void PredictRawInPlace(
ForestDataset& dataset, std::vector<double>& output);
138 void PredictRawInPlace(
ForestDataset& dataset,
int forest_num, std::vector<double>& output);
139 void PredictRawSingleTreeInPlace(
ForestDataset& dataset,
int forest_num,
int tree_num, std::vector<double>& output);
140 void PredictLeafIndicesInplace(Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>& covariates,
141 Eigen::Map<Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>& output,
142 std::vector<int>& forest_indices,
int num_trees, data_size_t n);
144 inline TreeEnsemble* GetEnsemble(
int i) {
return forests_[i].get();}
145 inline int32_t NumSamples() {
return num_samples_;}
146 inline int32_t NumTrees() {
return num_trees_;}
147 inline int32_t NumTrees(
int ensemble_num) {
return forests_[ensemble_num]->NumTrees();}
148 inline int32_t NumLeaves(
int ensemble_num) {
return forests_[ensemble_num]->NumLeaves();}
149 inline int32_t EnsembleTreeMaxDepth(
int ensemble_num,
int tree_num) {
return forests_[ensemble_num]->TreeMaxDepth(tree_num);}
150 inline double EnsembleAverageMaxDepth(
int ensemble_num) {
return forests_[ensemble_num]->AverageMaxDepth();}
151 inline double AverageMaxDepth() {
152 double numerator = 0.;
153 double denominator = 0.;
154 for (
int i = 0; i < num_samples_; i++) {
155 for (
int j = 0; j < num_trees_; j++) {
156 numerator +=
static_cast<double>(forests_[i]->TreeMaxDepth(j));
160 return numerator / denominator;
162 inline int32_t OutputDimension() {
return output_dimension_;}
163 inline int32_t OutputDimension(
int ensemble_num) {
return forests_[ensemble_num]->OutputDimension();}
164 inline bool IsLeafConstant() {
return is_leaf_constant_;}
165 inline bool IsLeafConstant(
int ensemble_num) {
return forests_[ensemble_num]->IsLeafConstant();}
166 inline bool IsExponentiated() {
return is_exponentiated_;}
167 inline bool IsExponentiated(
int ensemble_num) {
return forests_[ensemble_num]->IsExponentiated();}
168 inline bool AllRoots(
int ensemble_num) {
return forests_[ensemble_num]->AllRoots();}
169 inline void SetLeafValue(
int ensemble_num,
double leaf_value) {forests_[ensemble_num]->SetLeafValue(leaf_value);}
170 inline void SetLeafVector(
int ensemble_num, std::vector<double>& leaf_vector) {forests_[ensemble_num]->SetLeafVector(leaf_vector);}
171 inline void IncrementSampleCount() {num_samples_++;}
173 void SaveToJsonFile(std::string filename) {
174 nlohmann::json model_json = this->
to_json();
175 std::ofstream output_file(filename);
176 output_file << model_json << std::endl;
179 void LoadFromJsonFile(std::string filename) {
180 std::ifstream f(filename);
181 nlohmann::json file_tree_json = nlohmann::json::parse(f);
186 std::string DumpJsonString() {
187 nlohmann::json model_json = this->
to_json();
188 return model_json.dump();
191 void LoadFromJsonString(std::string& json_string) {
192 nlohmann::json file_tree_json = nlohmann::json::parse(json_string);
201 output_dimension_ = 0;
202 is_leaf_constant_ = 0;
203 initialized_ =
false;
209 void from_json(
const nlohmann::json& forest_container_json);
214 std::vector<std::unique_ptr<TreeEnsemble>> forests_;
217 int output_dimension_;
218 bool is_exponentiated_{
false};
219 bool is_leaf_constant_;
220 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.