From 1c546cd9b1c229cba6f076f7532fadc27066f63e Mon Sep 17 00:00:00 2001 From: Thomas J Fan Date: Wed, 20 Nov 2019 16:03:25 -0500 Subject: [PATCH] CLN Move gradient and hessian closer to for loop in hist GBDT (#15686) --- .../gradient_boosting.py | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/sklearn/ensemble/_hist_gradient_boosting/gradient_boosting.py b/sklearn/ensemble/_hist_gradient_boosting/gradient_boosting.py index 78dab9419d0f7..cb708ecc576e7 100644 --- a/sklearn/ensemble/_hist_gradient_boosting/gradient_boosting.py +++ b/sklearn/ensemble/_hist_gradient_boosting/gradient_boosting.py @@ -191,13 +191,6 @@ def fit(self, X, y): ) raw_predictions += self._baseline_prediction - # initialize gradients and hessians (empty arrays). - # shape = (n_trees_per_iteration, n_samples). - gradients, hessians = self.loss_.init_gradients_and_hessians( - n_samples=n_samples, - prediction_dim=self.n_trees_per_iteration_ - ) - # predictors is a matrix (list of lists) of TreePredictor objects # with shape (n_iter_, n_trees_per_iteration) self._predictors = predictors = [] @@ -278,17 +271,18 @@ def fit(self, X, y): X_binned_small_train, y_small_train = self._get_small_trainset( X_binned_train, y_train, self._random_seed) - # Initialize the gradients and hessians - gradients, hessians = self.loss_.init_gradients_and_hessians( - n_samples=n_samples, - prediction_dim=self.n_trees_per_iteration_ - ) - # Get the predictors from the previous fit predictors = self._predictors begin_at_stage = self.n_iter_ + # initialize gradients and hessians (empty arrays). + # shape = (n_trees_per_iteration, n_samples). + gradients, hessians = self.loss_.init_gradients_and_hessians( + n_samples=n_samples, + prediction_dim=self.n_trees_per_iteration_ + ) + for iteration in range(begin_at_stage, self.max_iter): if self.verbose: