8000 fix: Huber loss function in gradient_boosting fails if negative_gradi… · r2k0/scikit-learn@c5ce49b · GitHub
[go: up one dir, main page]

Skip to content

Commit c5ce49b

Browse files
committed
fix: Huber loss function in gradient_boosting fails if negative_gradient is not called before __call__; now computes on-demand.
1 parent 6d09f05 commit c5ce49b

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

sklearn/ensemble/gradient_boosting.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ class HuberLossFunction(RegressionLossFunction):
245245
def __init__(self, n_classes, alpha=0.9):
246246
super(HuberLossFunction, self).__init__(n_classes)
247247
self.alpha = alpha
248+
self.gamma = None
248249

249250
def init_estimator(self):
250251
return QuantileEstimator(alpha=0.5)
@@ -253,6 +254,8 @@ def __call__(self, y, pred):
253254
pred = pred.ravel()
254255
diff = y - pred
255256
gamma = self.gamma
257+
if gamma is None:
258+
gamma = stats.scoreatpercentile(np.abs(diff), self.alpha * 100)
256259
gamma_mask = np.abs(diff) <= gamma
257260
sq_loss = np.sum(0.5 * diff[gamma_mask] ** 2.0)
258261
lin_loss = np.sum(gamma * (np.abs(diff[~gamma_mask]) - gamma / 2.0))

sklearn/ensemble/tests/test_gradient_boosting.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,15 @@ def test_boston():
153153
"""Check consistency on dataset boston house prices with least squares
154154
and least absolute deviation. """
155155
for loss in ("ls", "lad", "huber"):
156-
clf = GradientBoostingRegressor(n_estimators=100, loss=loss,
157-
max_depth=4,
158-
min_samples_split=1, random_state=1)
159-
assert_raises(ValueError, clf.predict, boston.data)
160-
clf.fit(boston.data, boston.target)
161-
y_pred = clf.predict(boston.data)
162-
mse = mean_squared_error(boston.target, y_pred)
163-
assert mse < 6.0, "Failed with loss %s and mse = %.4f" % (loss, mse)
156+
for subsample in (1.0, 0.5):
157+
clf = GradientBoostingRegressor(n_estimators=100, loss=loss,
158+
max_depth=4, subsample=subsample,
159+
min_samples_split=1, random_state=1)
160+
assert_raises(ValueError, clf.predict, boston.data)
161+
clf.fit(boston.data, boston.target)
162+
y_pred = clf.predict(boston.data)
163+
mse = mean_squared_error(boston.target, y_pred)
164+
assert mse < 6.0, "Failed with loss %s and mse = %.4f" % (loss, mse)
164165

165166

166167
def test_iris():

0 commit comments

Comments
 (0)
0