Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion mlfromscratch/examples/gradient_boosting_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from mlfromscratch.utils import train_test_split, standardize, to_categorical
from mlfromscratch.utils import mean_squared_error, accuracy_score, Plot
from mlfromscratch.utils.loss_functions import SquareLoss
from mlfromscratch.utils.misc import bar_widgets
from mlfromscratch.supervised_learning import GradientBoostingRegressor

Expand Down
4 changes: 3 additions & 1 deletion mlfromscratch/supervised_learning/gradient_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, n_estimators, learning_rate, min_samples_split,
self.max_depth = max_depth
self.regression = regression
self.bar = progressbar.ProgressBar(widgets=bar_widgets)
self.initial_prediction = None

# Square loss for regression
# Log loss for classification
Expand All @@ -58,6 +59,7 @@ def __init__(self, n_estimators, learning_rate, min_samples_split,


def fit(self, X, y):
self.initial_prediction = np.mean(y, axis=0)
y_pred = np.full(np.shape(y), np.mean(y, axis=0))
for i in self.bar(range(self.n_estimators)):
gradient = self.loss.gradient(y, y_pred)
Expand All @@ -73,7 +75,7 @@ def predict(self, X):
for tree in self.trees:
update = tree.predict(X)
update = np.multiply(self.learning_rate, update)
y_pred = -update if not y_pred.any() else y_pred - update
y_pred = self.initial_prediction - update if not y_pred.any() else y_pred - update

if not self.regression:
# Turn into probability distribution
Expand Down