8000 [MRG + 1] Fix gradient boosting overflow and various other float comp… · scikit-learn/scikit-learn@919b4a8 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 919b4a8

Browse files
chenhe95amueller
authored andcommitted
[MRG + 1] Fix gradient boosting overflow and various other float comparison on == (#7970)
* reintroduced isclose() and flake8 fixes to fixes.py * changed == 0.0 to isclose(...) * example changes * changed back to abs() < epsilon * flake8 convention on file * reverted flake8 fixes * reverted flake8 fixes (2) * np.finfo(np.float32).tiny instead of hard coded epsilon 1e-150 * reverted to 1e-150 * whats new modified
1 parent 426f441 commit 919b4a8

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

doc/whats_new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ Bug fixes
185185
``download_if_missing`` keyword. This was fixed in :issue:`7944` by
186186
:user:`Ralf Gommers <rgommers>`.
187187

188+
- Fixed a bug in :class:`sklearn.ensemble.GradientBoostingClassifier`
189+
and :class:`sklearn.ensemble.GradientBoostingRegressor`
190+
where a float being compared to ``0.0`` using ``==`` caused a divide by zero
191+
error. This was fixed in :issue:`7970` by :user:`He Chen <chenhe95>`.
188192

189193
- Fix a bug regarding fitting :class:`sklearn.cluster.KMeans` with a
190194
sparse array X and initial centroids, where X's means were unnecessarily

sklearn/ensemble/gradient_boosting.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ def _update_terminal_region(self, tree, terminal_regions, leaf, X, y,
510510
numerator = np.sum(sample_weight * residual)
511511
denominator = np.sum(sample_weight * (y - residual) * (1 - y + residual))
512512

513-
if denominator == 0.0:
513+
# prevents overflow and division by zero
514+
if abs(denominator) < 1e-150:
514515
tree.value[leaf, 0, 0] = 0.0
515516
else:
516517
tree.value[leaf, 0, 0] = numerator / denominator
@@ -576,7 +577,8 @@ def _update_terminal_region(self, tree, terminal_regions, leaf, X, y,
576577
denominator = np.sum(sample_weight * (y - residual) *
577578
(1.0 - y + residual))
578579

579-
if denominator == 0.0:
580+
# prevents overflow and division by zero
581+
if abs(denominator) < 1e-150:
580582
tree.value[leaf, 0, 0] = 0.0
581583
else:
582584
tree.value[leaf, 0, 0] = numerator / denominator
@@ -633,7 +635,8 @@ def _update_terminal_region(self, tree, terminal_regions, leaf, X, y,
633635
numerator = np.sum(y_ * sample_weight * np.exp(-y_ * pred))
634636
denominator = np.sum(sample_weight * np.exp(-y_ * pred))
635637

636-
if denominator == 0.0:
638+
# prevents overflow and division by zero
639+
if abs(denominator) < 1e-150:
637640
tree.value[leaf, 0, 0] = 0.0
638641
else:
639642
tree.value[leaf, 0, 0] = numerator / denominator

0 commit comments

Comments
 (0)
0