Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update tree to if-else #5422

Merged
merged 4 commits into from
Aug 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/io/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,35 +523,32 @@ std::string Tree::NumericalDecisionIfElse(int node) const {
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
uint8_t missing_type = GetMissingType(decision_type_[node]);
bool default_left = GetDecisionType(decision_type_[node], kDefaultLeftMask);
if (missing_type == MissingType::None
|| (missing_type == MissingType::Zero && default_left && kZeroThreshold < threshold_[node])) {
str_buf << "if (fval <= " << threshold_[node] << ") {";
} else if (missing_type == MissingType::Zero) {
if (missing_type != MissingType::NaN) {
str_buf << "if (std::isnan(fval)) fval = 0.0;";
}
if (missing_type == MissingType::Zero) {
if (default_left) {
str_buf << "if (fval <= " << threshold_[node] << " || Tree::IsZero(fval)" << " || std::isnan(fval)) {";
str_buf << "if (Tree::IsZero(fval)) {";
} else {
str_buf << "if (fval <= " << threshold_[node] << " && !Tree::IsZero(fval)" << " && !std::isnan(fval)) {";
str_buf << "if (!Tree::IsZero(fval)) {";
}
} else {
} else if (missing_type == MissingType::NaN) {
if (default_left) {
str_buf << "if (fval <= " << threshold_[node] << " || std::isnan(fval)) {";
str_buf << "if (std::isnan(fval)) {";
} else {
str_buf << "if (fval <= " << threshold_[node] << " && !std::isnan(fval)) {";
str_buf << "if (!std::isnan(fval)) {";
}
} else {
str_buf << "if (fval <= " << threshold_[node] << ") {";
}
return str_buf.str();
}

std::string Tree::CategoricalDecisionIfElse(int node) const {
uint8_t missing_type = GetMissingType(decision_type_[node]);
std::stringstream str_buf;
Common::C_stringstream(str_buf);
if (missing_type == MissingType::NaN) {
str_buf << "if (std::isnan(fval)) { int_fval = -1; } else { int_fval = static_cast<int>(fval); }";
} else {
str_buf << "if (std::isnan(fval)) { int_fval = 0; } else { int_fval = static_cast<int>(fval); }";
}
int cat_idx = static_cast<int>(threshold_[node]);
str_buf << "if (std::isnan(fval)) { int_fval = -1; } else { int_fval = static_cast<int>(fval); }";
str_buf << "if (int_fval >= 0 && int_fval < 32 * (";
str_buf << cat_boundaries_[cat_idx + 1] - cat_boundaries_[cat_idx];
str_buf << ") && (((cat_threshold[" << cat_boundaries_[cat_idx];
Expand Down