Skip to content

Bug in Gradient Boosting: Feature Importances do not sum to 1 #7406

Closed

Description

@dsc888

Description

I found conditions when Feature Importance values do not add up to 1 in ensemble tree methods, like Gradient Boosting Trees or AdaBoost Trees.

This error occurs once the ensemble reaches a large number of estimators. The exact conditions depend variously. For example, the error shows up sooner with a smaller amount of training samples. Or, if the depth of the tree is large.

When this error appears, the predicted value seems to have converged. But it’s unclear if the error is causing the predicted value not to change with more estimators. In fact, the feature importance sum goes lower and lower with more estimators thereafter.

Consequently, it's questionable if the tree ensemble code is functioning as expected.

Here's sample code to reproduce this:

import numpy as np
from sklearn import datasets
from sklearn.ensemble import GradientBoostingRegressor

boston = datasets.load_boston()
X, Y = (boston.data, boston.target)

n_estimators = 720
# Note: From 712 onwards, the feature importance sum is less than 1

params = {'n_estimators': n_estimators, 'max_depth': 6, 'learning_rate': 0.1}
clf = GradientBoostingRegressor(**params)
clf.fit(X, Y)

feature_importance_sum = np.sum(clf.feature_importances_)
print("At n_estimators = %i, feature importance sum = %f" % (n_estimators , feature_importance_sum))

Output:

At n_estimators = 720, feature importance sum = 0.987500

In fact, if we examine the tree at each staged prediction, we'll see that the feature importance goes to 0 after we hit a certain number of estimators. (For the code above, it's 712.)

Here's code to describe what I mean:

for i, tree in enumerate(clf.estimators_):
    feature_importance_sum = np.sum(tree[0].feature_importances_)
    print("At n_estimators = %i, feature importance sum = %f" % (i , feature_importance_sum))

Output:

...
At n_estimators = 707, feature importance sum = 1.000000
At n_estimators = 708, feature importance sum = 1.000000
At n_estimators = 709, feature importance sum = 1.000000
At n_estimators = 710, feature importance sum = 1.000000
At n_estimators = 711, feature importance sum = 0.000000
At n_estimators = 712, feature importance sum = 0.000000
At n_estimators = 713, feature importance sum = 0.000000
At n_estimators = 714, feature importance sum = 0.000000
At n_estimators = 715, feature importance sum = 0.000000
At n_estimators = 716, feature importance sum = 0.000000
At n_estimators = 717, feature importance sum = 0.000000
At n_estimators = 718, feature importance sum = 0.000000
...

I wonder if we’re hitting some floating point calculation error.

BTW, I've posted this issue on the mailing list Link. There aren't a lot of discussion, but others seem to think there's a bug here too.

Hope we can get this fixed or clarified.

Thank you!
-Doug

Versions

Windows-7;'Python', '2.7.9 ;'NumPy', '1.9.2';'SciPy', '0.15.1';'Scikit-Learn', '0.16.1'

Activity

  1. changed the title [-]Gradient Boosting: Feature Importances do not sum to 1[/-] [+]Bug in Gradient Boosting: Feature Importances do not sum to 1[/+] on Sep 13, 2016
  2. amueller commented on Sep 13, 2016

    @amueller
    Member

    thanks for the report. ping @pprett ?

  3. modified the milestone: 0.19 on Sep 29, 2016
  4. neerajgangwar commented on Oct 17, 2016

    @neerajgangwar
    Contributor

    Observation:
    The estimators with feature importance sum 0 have only 1 node which is being caused by the following code
    is_leaf = (is_leaf or (impurity <= min_impurity_split))

    is_leaf is changing to 1 in the first iteration for such trees.

    For estimator number 710
    impurity = 0.00000010088593634382 min_impurity_split = 0.00000010000000000000

    For estimator number 711 (which has 0 feature importance sum)
    impurity = 0.00000009983122597550 min_impurity_split = 0.00000010000000000000

    The same behavior is there for all subsequent estimators.

    Versions:
    Ubuntu 16.04, Python: 2.7.10, Numpy: 1.11.2, Scipy: 0.18.1, Scikit-Learn: 0.19.dev0

  5. amueller commented on Oct 17, 2016

    @amueller
    Member

    ping @jmschrei maybe?

  6. jmschrei commented on Oct 17, 2016

    @jmschrei
    Member

    If the gini impurity at the first node is that low, you're not gaining anything by having more trees. I am unsure if this is a bug, except that it should maybe only look at the impurity of trees where a split improves things.

  7. neerajgangwar commented on Oct 19, 2016

    @neerajgangwar
    Contributor

    So should these extra trees (containing only one node) be considered while computing feature importance?

  8. amueller commented on Oct 19, 2016

    @amueller
    Member

    @Naereen skipping them would be fine I guess, and would make them sum to one again?

  9. neerajgangwar commented on Nov 20, 2016

    @neerajgangwar
    Contributor

    IMO, while computing trees, when gini impurity of the first node of a tree is lower than the threshold, the program shouldn't move forward. The actual number of trees computed should be communicated to the user. In example above, program should stop at 711 and number of estimators should be 710.

  10. modified the milestones: 0.20, 0.19 on Jun 14, 2017
  11. modified the milestones: 0.20, 0.21 on Jun 13, 2018
  12. jeremiedbb commented on Jul 2, 2018

    @jeremiedbb
    Member

    on master, the first part of the issue seems fixed. This part of the code now returns 1.0 as expected.

    feature_importance_sum = np.sum(clf.feature_importances_)
    print("At n_estimators = %i, feature importance sum = %f" % (n_estimators , feature_importance_sum))

    It has been fixed in #11176.
    However, for the second part of the issue, the problem is still present. the trees that add up to 0.0 actually contain only 1 node and feature_importances_ does not really make sens for those trees. Maybe something should be done to avoid having those trees.

  13. jnothman commented on Jul 3, 2018

    @jnothman
    Member

    By 1 node, you mean just a single leaf node, or a single split node? Yes, a tree with a single leaf node should not be contributing to feature importances.

  14. jeremiedbb commented on Jul 3, 2018

    @jeremiedbb
    Member

    1 is the value returned by node_count. According to the docstring, it's internal nodes + leaves. So those trees are just a single leaf node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions