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

ENH use Scipy isotonic_regression #28897

Merged
merged 5 commits into from May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions benchmarks/bench_isotonic.py
Expand Up @@ -13,7 +13,7 @@

import argparse
import gc
from datetime import datetime
from timeit import default_timer

import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -52,9 +52,9 @@ def bench_isotonic_regression(Y):
"""
gc.collect()

tstart = datetime.now()
tstart = default_timer()
isotonic_regression(Y)
return (datetime.now() - tstart).total_seconds()
return default_timer() - tstart


if __name__ == "__main__":
Expand Down
23 changes: 16 additions & 7 deletions sklearn/isotonic.py
Expand Up @@ -8,13 +8,14 @@
from numbers import Real

import numpy as np
from scipy import interpolate
from scipy import interpolate, optimize
from scipy.stats import spearmanr

from ._isotonic import _inplace_contiguous_isotonic_regression, _make_unique
from .base import BaseEstimator, RegressorMixin, TransformerMixin, _fit_context
from .utils import check_array, check_consistent_length
from .utils._param_validation import Interval, StrOptions, validate_params
from .utils.fixes import parse_version, sp_version
from .utils.validation import _check_sample_weight, check_is_fitted

__all__ = ["check_increasing", "isotonic_regression", "IsotonicRegression"]
Expand Down Expand Up @@ -151,21 +152,29 @@ def isotonic_regression(
array([2.75 , 2.75 , 2.75 , 2.75 , 7.33...,
7.33..., 7.33..., 7.33..., 7.33..., 7.33...])
"""
order = np.s_[:] if increasing else np.s_[::-1]
y = check_array(y, ensure_2d=False, input_name="y", dtype=[np.float64, np.float32])
y = np.array(y[order], dtype=y.dtype)
sample_weight = _check_sample_weight(sample_weight, y, dtype=y.dtype, copy=True)
sample_weight = np.ascontiguousarray(sample_weight[order])
if sp_version >= parse_version("1.12.0"):
lorentzenchr marked this conversation as resolved.
Show resolved Hide resolved
res = optimize.isotonic_regression(
y=y, weights=sample_weight, increasing=increasing
)
y = np.asarray(res.x, dtype=y.dtype)
else:
# TODO: remove this branch when Scipy 1.12 is the minimum supported version
# Also remove _inplace_contiguous_isotonic_regression.
order = np.s_[:] if increasing else np.s_[::-1]
y = np.array(y[order], dtype=y.dtype)
sample_weight = _check_sample_weight(sample_weight, y, dtype=y.dtype, copy=True)
sample_weight = np.ascontiguousarray(sample_weight[order])
_inplace_contiguous_isotonic_regression(y, sample_weight)

_inplace_contiguous_isotonic_regression(y, sample_weight)
if y_min is not None or y_max is not None:
# Older versions of np.clip don't accept None as a bound, so use np.inf
if y_min is None:
y_min = -np.inf
if y_max is None:
y_max = np.inf
np.clip(y, y_min, y_max, y)
return y[order]
return y


class IsotonicRegression(RegressorMixin, TransformerMixin, BaseEstimator):
Expand Down
26 changes: 13 additions & 13 deletions sklearn/tests/test_isotonic.py
Expand Up @@ -502,25 +502,25 @@ def test_isotonic_copy_before_fit():
copy.copy(ir)


def test_isotonic_dtype():
@pytest.mark.parametrize("dtype", [np.int32, np.int64, np.float32, np.float64])
def test_isotonic_dtype(dtype):
y = [2, 1, 4, 3, 5]
weights = np.array([0.9, 0.9, 0.9, 0.9, 0.9], dtype=np.float64)
reg = IsotonicRegression()

for dtype in (np.int32, np.int64, np.float32, np.float64):
for sample_weight in (None, weights.astype(np.float32), weights):
y_np = np.array(y, dtype=dtype)
expected_dtype = check_array(
y_np, dtype=[np.float64, np.float32], ensure_2d=False
).dtype
for sample_weight in (None, weights.astype(np.float32), weights):
y_np = np.array(y, dtype=dtype)
expected_dtype = check_array(
y_np, dtype=[np.float64, np.float32], ensure_2d=False
).dtype

res = isotonic_regression(y_np, sample_weight=sample_weight)
assert res.dtype == expected_dtype
res = isotonic_regression(y_np, sample_weight=sample_weight)
assert res.dtype == expected_dtype

X = np.arange(len(y)).astype(dtype)
reg.fit(X, y_np, sample_weight=sample_weight)
res = reg.predict(X)
assert res.dtype == expected_dtype
X = np.arange(len(y)).astype(dtype)
reg.fit(X, y_np, sample_weight=sample_weight)
res = reg.predict(X)
assert res.dtype == expected_dtype


@pytest.mark.parametrize("y_dtype", [np.int32, np.int64, np.float32, np.float64])
Expand Down