Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG Add tol to _make_unique to avoid inf values in IsotonicRegression #18639

Merged
merged 12 commits into from
Oct 27, 2020
11 changes: 4 additions & 7 deletions sklearn/_isotonic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ def _make_unique(np.ndarray[dtype=floating] X,
Assumes that X is ordered, so that all duplicates follow each other.
"""
unique_values = len(np.unique(X))
if unique_values == len(X):
return X, y, sample_weights

cdef np.ndarray[dtype=floating] y_out = np.empty(unique_values,
dtype=X.dtype)
Expand All @@ -90,13 +88,14 @@ def _make_unique(np.ndarray[dtype=floating] X,
cdef floating current_weight = 0
cdef floating y_old = 0
cdef int i = 0
cdef int current_count = 0
cdef int j
cdef floating x
cdef int n_samples = len(X)
cdef floating eps = np.finfo(X.dtype).eps
ogrisel marked this conversation as resolved.
Show resolved Hide resolved

for j in range(n_samples):
x = X[j]
if x != current_x:
if x - current_x >= eps:
# next unique value
x_out[i] = current_x
weights_out[i] = current_weight
Expand All @@ -105,13 +104,11 @@ def _make_unique(np.ndarray[dtype=floating] X,
current_x = x
current_weight = sample_weights[j]
current_y = y[j] * sample_weights[j]
current_count = 1
else:
current_weight += sample_weights[j]
current_y += y[j] * sample_weights[j]
current_count += 1

x_out[i] = current_x
weights_out[i] = current_weight
y_out[i] = current_y / current_weight
return x_out, y_out, weights_out
return x_out[:i+1], y_out[:i+1], weights_out[:i+1]
18 changes: 18 additions & 0 deletions sklearn/tests/test_isotonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,24 @@ def test_make_unique_dtype():
assert_array_equal(x, [2, 3, 5])


def test_make_unique_tol():
# Check that equality tolerance works
x = np.array([0, 1e-16, 1, 1+1e-14])
lucyleeow marked this conversation as resolved.
Show resolved Hide resolved
y = x.copy()
w = np.ones_like(x)
x, y, w = _make_unique(x, y, w)
assert_array_equal(x, [0, 1, 1+1e-14])
lucyleeow marked this conversation as resolved.
Show resolved Hide resolved


def test_isotonic_inf():
lucyleeow marked this conversation as resolved.
Show resolved Hide resolved
# Check that inf values are not returned
lucyleeow marked this conversation as resolved.
Show resolved Hide resolved
X = np.array([0., 4.1e-320, 4.4e-314, 1.])
y = np.array([0.42, 0.42, 0.44, 0.44])
ireg = IsotonicRegression().fit(X, y)
y_pred = ireg.predict(np.array([0, 2.1e-319, 5.4e-316, 1e-10]))
assert np.all(np.isfinite(y_pred))


lucyleeow marked this conversation as resolved.
Show resolved Hide resolved
@pytest.mark.parametrize("increasing", [True, False])
def test_isotonic_thresholds(increasing):
rng = np.random.RandomState(42)
Expand Down