Repository navigation
Description
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
[-]Gradient Boosting: Feature Importances do not sum to 1[/-][+]Bug in Gradient Boosting: Feature Importances do not sum to 1[/+]amueller commentedon Sep 13, 2016
thanks for the report. ping @pprett ?
neerajgangwar commentedon Oct 17, 2016
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_leafis changing to 1 in the first iteration for such trees.For estimator number 710
impurity = 0.00000010088593634382 min_impurity_split = 0.00000010000000000000For estimator number 711 (which has 0 feature importance sum)
impurity = 0.00000009983122597550 min_impurity_split = 0.00000010000000000000The 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
amueller commentedon Oct 17, 2016
ping @jmschrei maybe?
jmschrei commentedon Oct 17, 2016
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.
neerajgangwar commentedon Oct 19, 2016
So should these extra trees (containing only one node) be considered while computing feature importance?
amueller commentedon Oct 19, 2016
@Naereen skipping them would be fine I guess, and would make them sum to one again?
neerajgangwar commentedon Nov 20, 2016
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.
jeremiedbb commentedon Jul 2, 2018
on master, the first part of the issue seems fixed. This part of the code now returns 1.0 as expected.
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.jnothman commentedon Jul 3, 2018
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.
jeremiedbb commentedon Jul 3, 2018
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.