In sklearn GradientBoostingClassifier, when I use predict() to classify:
gbdt = GradientBoostingClassifier(n_estimators=7)
tree_preds = gbdt.predict(X)
gives different results compared to when I use:
leaves = gbdt.apply(X)
n_class = leaves.shape[2]
X_leaves = np.array([row.ravel() for row in leaves]) # N, n_trees * n_class
trees = self.gbdt.estimators_.ravel()
sums = []
for leaves in X_leaves:
sum_leaf_vals = [0] * n_class
for i, leaf in enumerate(leaves.ravel()):
class_idx = i % n_class
leaf_val = trees[i].tree_.value[int(leaf)][0][0]
sum_leaf_vals[class_idx] += leaf_val
sums.append(sum_leaf_vals)
sum_preds = [np.argmax(s) for s in sums]
print(tree_preds == sum_preds)
Any idea?
Edit: I find the reason by myself, the sklearn will add a base term estimator (init_estimator) to the additional tree sum, so if I add init='zero'
parameter to GradientBoostingClassifier(), all the predictions will match.