Skip to content

[python-package] plot_importance(): raise clearer exception when model has 0 splits - #7160

Merged
jameslamb merged 3 commits into
lightgbm-org:masterfrom
juanf-0gravity:fix/issue-6865
Mar 1, 2026
Merged

jameslamb merged 3 commits into
lightgbm-org:masterfrom
juanf-0gravity:fix/issue-6865

Conversation

@juanf-0gravity

Copy link
Copy Markdown
Contributor

Fixes #6865

When a model has 0 splits (e.g. too few samples for LightGBM to find useful features), all feature importances are zero. With ignore_zero=True (the default), the filtering removes everything and zip(*tuples) crashes with ValueError: not enough values to unpack.

Added a guard that raises a clear ValueError with a message pointing users to ignore_zero=False. Also added a test that verifies both the error case and the ignore_zero=False fallback.

@jameslamb tagging you as suggested.

@juanf-0gravity

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@jameslamb jameslamb added the fix label Feb 21, 2026
@jameslamb jameslamb changed the title [python-package] fix plot_importance() crash when model has 0 splits [python-package] plot_importance(): raise clearer exception when model has 0 splits Feb 21, 2026

@jameslamb jameslamb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one small suggestion, otherwise this looks great to me.

Thanks so much for taking the time to contribute!

Comment on lines +133 to +136
raise ValueError(
"No non-zero feature importances found. The model may have no splits. "
"Use ignore_zero=False to show all features."
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for using "may" here, I think that's right.

There are 2 ways I can think of that you can end up in this situation:

  1. using importance_type="split" and there are 0 splits
  2. using importance_type="gain" and there are splits, but the gain is so small that it's == 0 due to numerical precision issues

This error message is great.

@jameslamb

Copy link
Copy Markdown
Member

Thanks for pushing 58a2b2c !

In the future, we'd appreciate if you please comment on the review threads with something like "fixed in 58a2b2c" or similar, acknowledging them. That makes it clearer that you believe you've addressed everything and this is ready for another review.

@jameslamb jameslamb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good, thanks so much for taking the time to contribute!

I'll merge this if CI passes.

@juanf-0gravity

Copy link
Copy Markdown
Contributor Author

Thanks for the review, I'll keep that in mind for future contributions.

@jameslamb
jameslamb merged commit 55a2696 into lightgbm-org:master Mar 1, 2026
75 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[python-package] plot_importance() fails when model has 0 splits

2 participants