Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 330ee7a

Browse files
glemaitrethomasjpfan
authored andcommitted
FIX make IsotonicRegression always predict NumPy arrays (#25500)
Co-authored-by: Thomas J. Fan <[email protected]>
1 parent e5ac79d commit 330ee7a

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed

doc/whats_new/v1.2.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,39 @@
22

33
.. currentmodule:: sklearn
44

5+
.. _changes_1_2_2:
6+
7+
Version 1.2.2
8+
=============
9+
10+
**In development**
11+
12+
The following estimators and functions, when fit with the same data and
13+
parameters, may produce different models from the previous version. This often
14+
occurs due to changes in the modelling logic (bug fixes or enhancements), or in
15+
random sampling procedures.
16+
17+
Changed models
18+
--------------
19+
20+
-
21+
22+
Changes impacting all modules
23+
-----------------------------
24+
25+
-
26+
27+
Changelog
28+
---------
29+
30+
:mod:`sklearn.isotonic`
31+
.......................
32+
33+
- |Fix| Fixes a bug in :class:`isotonic.IsotonicRegression` where
34+
:meth:`isotonic.IsotonicRegression.predict` would return a pandas DataFrame
35+
when the global configuration sets `transform_output="pandas"`.
36+
:pr:`25500` by :user:`Guillaume Lemaitre <glemaitre>`.
37+
538
.. _changes_1_2_1:
639

740
Version 1.2.1

sklearn/isotonic.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -360,23 +360,16 @@ def fit(self, X, y, sample_weight=None):
360360
self._build_f(X, y)
361361
return self
362362

363-
def transform(self, T):
364-
"""Transform new data by linear interpolation.
365-
366-
Parameters
367-
----------
368-
T : array-like of shape (n_samples,) or (n_samples, 1)
369-
Data to transform.
363+
def _transform(self, T):
364+
"""`_transform` is called by both `transform` and `predict` methods.
370365
371-
.. versionchanged:: 0.24
372-
Also accepts 2d array with 1 feature.
366+
Since `transform` is wrapped to output arrays of specific types (e.g.
367+
NumPy arrays, pandas DataFrame), we cannot make `predict` call `transform`
368+
directly.
373369
374-
Returns
375-
-------
376-
y_pred : ndarray of shape (n_samples,)
377-
The transformed data.
370+
The above behaviour could be changed in the future, if we decide to output
371+
other type of arrays when calling `predict`.
378372
"""
379-
380373
if hasattr(self, "X_thresholds_"):
381374
dtype = self.X_thresholds_.dtype
382375
else:
@@ -397,6 +390,24 @@ def transform(self, T):
397390

398391
return res
399392

393+
def transform(self, T):
394+
"""Transform new data by linear interpolation.
395+
396+
Parameters
397+
----------
398+
T : array-like of shape (n_samples,) or (n_samples, 1)
399+
Data to transform.
400+
401+
.. versionchanged:: 0.24
402+
Also accepts 2d array with 1 feature.
403+
404+
Returns
405+
-------
406+
y_pred : ndarray of shape (n_samples,)
407+
The transformed data.
408+
"""
409+
return self._transform(T)
410+
400411
def predict(self, T):
401412
"""Predict new data by linear interpolation.
402413
@@ -410,7 +421,7 @@ def predict(self, T):
410421
y_pred : ndarray of shape (n_samples,)
411422
Transformed data.
412423
"""
413-
return self.transform(T)
424+
return self._transform(T)
414425

415426
# We implement get_feature_names_out here instead of using
416427
# `ClassNamePrefixFeaturesOutMixin`` because `input_features` are ignored.

sklearn/tests/test_isotonic.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pytest
77

8+
import sklearn
89
from sklearn.datasets import make_regression
910
from sklearn.isotonic import (
1011
check_increasing,
@@ -680,3 +681,24 @@ def test_get_feature_names_out(shape):
680681
assert isinstance(names, np.ndarray)
681682
assert names.dtype == object
682683
assert_array_equal(["isotonicregression0"], names)
684+
685+
686+
def test_isotonic_regression_output_predict():
687+
"""Check that `predict` does return the expected output type.
688+
689+
We need to check that `transform` will output a DataFrame and a NumPy array
690+
when we set `transform_output` to `pandas`.
691+
692+
Non-regression test for:
693+
https://github.com/scikit-learn/scikit-learn/issues/25499
694+
"""
695+
pd = pytest.importorskip("pandas")
696+
X, y = make_regression(n_samples=10, n_features=1, random_state=42)
697+
regressor = IsotonicRegression()
698+
with sklearn.config_context(transform_output="pandas"):
699+
regressor.fit(X, y)
700+
X_trans = regressor.transform(X)
701+
y_pred = regressor.predict(X)
702+
703+
assert isinstance(X_trans, pd.DataFrame)
704+
assert isinstance(y_pred, np.ndarray)

0 commit comments

Comments
 (0)