# Buggy code
def _update_terminal_region(
    self, tree, terminal_regions, leaf, X, y, residual, pred, sample_weight
):
    """Make a single Newton-Raphson step.

    our node estimate is given by:

        sum(w * (y - prob)) / sum(w * prob * (1 - prob))

    we take advantage that: y - prob = residual
    """
    terminal_region = np.where(terminal_regions == leaf)[0]
    residual = residual.take(terminal_region, axis=0)
    y = y.take(terminal_region, axis=0)
    sample_weight = sample_weight.take(terminal_region, axis=0)

    numerator = np.sum(sample_weight * residual)
    denominator = np.sum(sample_weight * (y - residual) * (1 - y + residual))

    if denominator == 0.0:
        tree.value[leaf, 0, 0] = 0.0
    else:
        tree.value[leaf, 0, 0] = numerator / denominator


# Traceback (ValueError)
Traceback (most recent call last):
File "double_cv.py", line 68, in <module>
double_cv(**vars(args))
File "double_cv.py", line 49, in double_cv
rs.fit(X_train, y_train)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/model_selection/_search.py", line 1185, in fit
return self._fit(X, y, groups, sampled_params)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/model_selection/_search.py", line 562, in _fit
for parameters in parameter_iterable
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 758, in __call__
while self.dispatch_one_batch(iterator):
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 608, in dispatch_one_batch
self._dispatch(tasks)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 571, in _dispatch
job = self._backend.apply_async(batch, callback=cb)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 109, in apply_async
result = ImmediateResult(func)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 322, in __init__
self.results = batch()
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 131, in __call__
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 260, in _fit_and_score
test_score = _score(estimator, X_test, y_test, scorer)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 287, in _score
score = scorer(estimator, X_test, y_test)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/metrics/scorer.py", line 196, in __call__
return self._sign * self._score_func(y, y_pred, **self._kwargs)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/metrics/ranking.py", line 260, in roc_auc_score
sample_weight=sample_weight)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/metrics/base.py", line 84, in _average_binary_score
return binary_metric(y_true, y_score, sample_weight=sample_weight)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/metrics/ranking.py", line 255, in _binary_roc_auc_score
sample_weight=sample_weight)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/metrics/ranking.py", line 505, in roc_curve
y_true, y_score, pos_label=pos_label, sample_weight=sample_weight)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/metrics/ranking.py", line 301, in _binary_clf_curve
assert_all_finite(y_score)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/utils/validation.py", line 65, in assert_all_finite
_assert_all_finite(X.data if sp.issparse(X) else X)
File "/home/podda/.venvs/vermont/local/lib/python2.7/site-packages/sklearn/utils/validation.py", line 58, in _assert_all_finite
" or a value too large for %r." % X.dtype)
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

# Fixed code
def _update_terminal_region(
    self, tree, terminal_regions, leaf, X, y, residual, pred, sample_weight
):
    """Make a single Newton-Raphson step.

    our node estimate is given by:

        sum(w * (y - prob)) / sum(w * prob * (1 - prob))

    we take advantage that: y - prob = residual
    """
    terminal_region = np.where(terminal_regions == leaf)[0]
    residual = residual.take(terminal_region, axis=0)
    y = y.take(terminal_region, axis=0)
    sample_weight = sample_weight.take(terminal_region, axis=0)

    numerator = np.sum(sample_weight * residual)
    denominator = np.sum(sample_weight * (y - residual) * (1 - y + residual))

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