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

Expose information about linear trees in JSON format #4186

Closed
StrikerRUS opened this issue Apr 16, 2021 · 2 comments
Closed

Expose information about linear trees in JSON format #4186

StrikerRUS opened this issue Apr 16, 2021 · 2 comments

Comments

@StrikerRUS
Copy link
Collaborator

Refer to #3299 (comment) for the initial discussion.

Here is the code how linear trees are dumped into txt format

LightGBM/src/io/tree.cpp

Lines 378 to 405 in fba18e4

str_buf << "is_linear=" << is_linear_ << '\n';
if (is_linear_) {
str_buf << "leaf_const="
<< ArrayToString<true>(leaf_const_, num_leaves_) << '\n';
std::vector<int> num_feat(num_leaves_);
for (int i = 0; i < num_leaves_; ++i) {
num_feat[i] = static_cast<int>(leaf_coeff_[i].size());
}
str_buf << "num_features="
<< ArrayToString(num_feat, num_leaves_) << '\n';
str_buf << "leaf_features=";
for (int i = 0; i < num_leaves_; ++i) {
if (num_feat[i] > 0) {
str_buf << ArrayToString(leaf_features_[i], leaf_features_[i].size()) << ' ';
}
str_buf << ' ';
}
str_buf << '\n';
str_buf << "leaf_coeff=";
for (int i = 0; i < num_leaves_; ++i) {
if (num_feat[i] > 0) {
str_buf << ArrayToString<true>(leaf_coeff_[i], leaf_coeff_[i].size()) << ' ';
}
str_buf << ' ';
}
str_buf << '\n';
}

The same information should be dumped in NodeToJSON function

std::string Tree::NodeToJSON(int index) const {

Special attention should be paid to the case of uno-leaf tree:

LightGBM/src/io/tree.cpp

Lines 419 to 423 in fba18e4

if (num_leaves_ == 1) {
str_buf << "\"tree_structure\":{" << "\"leaf_value\":" << leaf_value_[0] << "}" << '\n';
} else {
str_buf << "\"tree_structure\":" << NodeToJSON(0) << '\n';
}

@StrikerRUS
Copy link
Collaborator Author

This is a sub-issue of #2604.

@StrikerRUS
Copy link
Collaborator Author

@btrotta Could help with this please?

btrotta added a commit that referenced this issue Jun 3, 2021
* Add linear leaf models to json output

* Add closing bracket

* Move test into test_engine.py and add asserts

* Update tests/python_package_test/test_engine.py

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* Update tests/python_package_test/test_engine.py

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* Update tests/python_package_test/test_engine.py

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant