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);
56 void MergeForests(
int inbound_forest_index,
int outbound_forest_index) {
57 forests_[inbound_forest_index]->MergeForest(*forests_[outbound_forest_index]);
66 forests_[forest_index]->AddValueToLeaves(constant_value);
75 forests_[forest_index]->MultiplyLeavesByValue(constant_multiple);
140 std::vector<double> PredictRawSingleTree(
ForestDataset& dataset,
int forest_num,
int tree_num);
141 void PredictInPlace(
ForestDataset& dataset, std::vector<double>& output);
142 void PredictRawInPlace(
ForestDataset& dataset, std::vector<double>& output);
143 void PredictRawInPlace(
ForestDataset& dataset,
int forest_num, std::vector<double>& output);
144 void PredictRawSingleTreeInPlace(
ForestDataset& dataset,
int forest_num,
int tree_num, std::vector<double>& output);
145 void PredictLeafIndicesInplace(Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>& covariates,
146 Eigen::Map<Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>& output,
147 std::vector<int>& forest_indices,
int num_trees, data_size_t n);
149 inline TreeEnsemble* GetEnsemble(
int i) {
return forests_[i].get();}
150 inline int32_t NumSamples() {
return num_samples_;}
151 inline int32_t NumTrees() {
return num_trees_;}
152 inline int32_t NumTrees(
int ensemble_num) {
return forests_[ensemble_num]->NumTrees();}
153 inline int32_t NumLeaves(
int ensemble_num) {
return forests_[ensemble_num]->NumLeaves();}
154 inline int32_t EnsembleTreeMaxDepth(
int ensemble_num,
int tree_num) {
return forests_[ensemble_num]->TreeMaxDepth(tree_num);}
155 inline double EnsembleAverageMaxDepth(
int ensemble_num) {
return forests_[ensemble_num]->AverageMaxDepth();}
156 inline double AverageMaxDepth() {
157 double numerator = 0.;
158 double denominator = 0.;
159 for (
int i = 0; i < num_samples_; i++) {
160 for (
int j = 0; j < num_trees_; j++) {
161 numerator +=
static_cast<double>(forests_[i]->TreeMaxDepth(j));
165 return numerator / denominator;
167 inline int32_t OutputDimension() {
return output_dimension_;}
168 inline int32_t OutputDimension(
int ensemble_num) {
return forests_[ensemble_num]->OutputDimension();}
169 inline bool IsLeafConstant() {
return is_leaf_constant_;}
170 inline bool IsLeafConstant(
int ensemble_num) {
return forests_[ensemble_num]->IsLeafConstant();}
171 inline bool IsExponentiated() {
return is_exponentiated_;}
172 inline bool IsExponentiated(
int ensemble_num) {
return forests_[ensemble_num]->IsExponentiated();}
173 inline bool AllRoots(
int ensemble_num) {
return forests_[ensemble_num]->AllRoots();}
174 inline void SetLeafValue(
int ensemble_num,
double leaf_value) {forests_[ensemble_num]->SetLeafValue(leaf_value);}
175 inline void SetLeafVector(
int ensemble_num, std::vector<double>& leaf_vector) {forests_[ensemble_num]->SetLeafVector(leaf_vector);}
176 inline void IncrementSampleCount() {num_samples_++;}
178 void SaveToJsonFile(std::string filename) {
179 nlohmann::json model_json = this->
to_json();
180 std::ofstream output_file(filename);
181 output_file << model_json << std::endl;
184 void LoadFromJsonFile(std::string filename) {
185 std::ifstream f(filename);
186 nlohmann::json file_tree_json = nlohmann::json::parse(f);
191 std::string DumpJsonString() {
192 nlohmann::json model_json = this->
to_json();
193 return model_json.dump();
196 void LoadFromJsonString(std::string& json_string) {
197 nlohmann::json file_tree_json = nlohmann::json::parse(json_string);
206 output_dimension_ = 0;
207 is_leaf_constant_ = 0;
208 initialized_ =
false;
214 void from_json(
const nlohmann::json& forest_container_json);
219 std::vector<std::unique_ptr<TreeEnsemble>> forests_;
222 int output_dimension_;
223 bool is_exponentiated_{
false};
224 bool is_leaf_constant_;
225 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.