Skip to content

Commit

Permalink
[if-else] use bit operation for categorical features (#821)
Browse files Browse the repository at this point in the history
* bitset for if-else

* add const
  • Loading branch information
wxchan authored and guolinke committed Aug 16, 2017
1 parent a77920c commit f8d6db9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
21 changes: 4 additions & 17 deletions include/LightGBM/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,24 +238,11 @@ class Tree {
} else {
str_buf << "if (std::isnan(fval)) { int_fval = 0; } else { int_fval = static_cast<int>(fval); }";
}
str_buf << "if (";
int cat_idx = int(threshold_[node]);
// To-do: optimize this by using bitset
std::vector<int> cats;
for (int i = cat_boundaries_[cat_idx]; i < cat_boundaries_[cat_idx + 1]; ++i) {
for (int j = 0; j < 32; ++j) {
int cat = (i - cat_boundaries_[cat_idx]) * 32 + j;
if (Common::FindInBitset(cat_threshold_.data() + cat_boundaries_[cat_idx],
cat_boundaries_[cat_idx + 1] - cat_boundaries_[cat_idx], cat)) {
cats.push_back(cat);
}
}
}
str_buf << "int_fval >= 0 && (";
for (int i = 0; i < static_cast<int>(cats.size()) - 1; ++i) {
str_buf << "int_fval == " << cats[i] << " || ";
}
str_buf << "int_fval == " << cats.back() << ")) {";
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];
str_buf << " + int_fval / 32] >> (int_fval & 31)) & 1))) {";
return str_buf.str();
}

Expand Down
8 changes: 8 additions & 0 deletions src/io/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ std::string Tree::ToIfElse(int index, bool is_predict_leaf_index) {
if (num_leaves_ == 1) {
str_buf << "return 0";
} else {
str_buf << "const std::vector<uint32_t> cat_threshold = {";
for (size_t i = 0; i < cat_threshold_.size(); ++i) {
if (i != 0) {
str_buf << ",";
}
str_buf << cat_threshold_[i];
}
str_buf << "};";
// use this for the missing value conversion
str_buf << "double fval = 0.0f; ";
if (num_cat_ > 0) {
Expand Down

0 comments on commit f8d6db9

Please sign in to comment.