0
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ You can answer your own question by writing an Answer. $\endgroup$
    – Sycorax
    Commented Aug 6, 2020 at 14:56

1 Answer 1

1
$\begingroup$

OP added an edit to the post intsead of writing an answer. It's reproduced here.

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.