Skip to content

Commit b646a81

Browse files
HunterMcGushionqinhanmin2014
authored andcommitted
API Update quantile_transform copy default to True (scikit-learn#13459)
1 parent e4ea963 commit b646a81

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

doc/whats_new/v0.21.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ Support for Python 3.4 and below has been officially dropped.
276276
broken when X was of dtype bool.
277277
:issue:`13328` by `Alexandre Gramfort`_.
278278

279-
- |API| The use of :class:`linear_model.lars_path` with ``X=None``
279+
- |API| The use of :class:`linear_model.lars_path` with ``X=None``
280280
while passing ``Gram`` is deprecated in version 0.21 and will be removed
281281
in version 0.23. Use :class:`linear_model.lars_path_gram` instead.
282282
:issue:`11699` by :user:`Kuai Yu <yukuairoy>`.
@@ -462,6 +462,13 @@ Support for Python 3.4 and below has been officially dropped.
462462
useless or resulting in a wrong approximation of the cumulative distribution
463463
function estimator. :issue:`13333` by :user:`Albert Thomas <albertcthomas>`.
464464

465+
- |API| The default value of `copy` in :func:`preprocessing.quantile_transform`
466+
will change from False to True in 0.23 in order to make it more consistent
467+
with the default `copy` values of other functions in
468+
:mod:`preprocessing.data` and prevent unexpected side effects by modifying
469+
the value of `X` inplace.
470+
:issue:`13459` by :user:`Hunter McGushion <HunterMcGushion>`.
471+
465472
:mod:`sklearn.svm`
466473
..................
467474

sklearn/preprocessing/data.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,7 +2431,7 @@ def quantile_transform(X, axis=0, n_quantiles=1000,
24312431
ignore_implicit_zeros=False,
24322432
subsample=int(1e5),
24332433
random_state=None,
2434-
copy=False):
2434+
copy="warn"):
24352435
"""Transform features using quantiles information.
24362436
24372437
This method transforms the features to follow a uniform or a normal
@@ -2489,9 +2489,18 @@ def quantile_transform(X, axis=0, n_quantiles=1000,
24892489
by np.random. Note that this is used by subsampling and smoothing
24902490
noise.
24912491
2492-
copy : boolean, optional, (default=True)
2492+
copy : boolean, optional, (default="warn")
24932493
Set to False to perform inplace transformation and avoid a copy (if the
2494-
input is already a numpy array).
2494+
input is already a numpy array). If True, a copy of `X` is transformed,
2495+
leaving the original `X` unchanged
2496+
2497+
.. deprecated:: 0.21
2498+
The default value of parameter `copy` will be changed from False
2499+
to True in 0.23. The current default of False is being changed to
2500+
make it more consistent with the default `copy` values of other
2501+
functions in :mod:`sklearn.preprocessing.data`. Furthermore, the
2502+
current default of False may have unexpected side effects by
2503+
modifying the value of `X` inplace
24952504
24962505
Returns
24972506
-------
@@ -2504,7 +2513,7 @@ def quantile_transform(X, axis=0, n_quantiles=1000,
25042513
>>> from sklearn.preprocessing import quantile_transform
25052514
>>> rng = np.random.RandomState(0)
25062515
>>> X = np.sort(rng.normal(loc=0.5, scale=0.25, size=(25, 1)), axis=0)
2507-
>>> quantile_transform(X, n_quantiles=10, random_state=0)
2516+
>>> quantile_transform(X, n_quantiles=10, random_state=0, copy=True)
25082517
... # doctest: +ELLIPSIS
25092518
array([...])
25102519
@@ -2529,6 +2538,17 @@ def quantile_transform(X, axis=0, n_quantiles=1000,
25292538
see :ref:`examples/preprocessing/plot_all_scaling.py
25302539
<sphx_glr_auto_examples_preprocessing_plot_all_scaling.py>`.
25312540
"""
2541+
if copy == "warn":
2542+
warnings.warn("The default value of `copy` will change from False to "
2543+
"True in 0.23 in order to make it more consistent with "
2544+
"the default `copy` values of other functions in "
2545+
":mod:`sklearn.preprocessing.data` and prevent "
2546+
"unexpected side effects by modifying the value of `X` "
2547+
"inplace. To avoid inplace modifications of `X`, it is "
2548+
"recommended to explicitly set `copy=True`",
2549+
FutureWarning)
2550+
copy = False
2551+
25322552
n = QuantileTransformer(n_quantiles=n_quantiles,
25332553
output_distribution=output_distribution,
25342554
subsample=subsample,

sklearn/preprocessing/tests/test_data.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,7 @@ def test_quantile_transform_sparse_toy():
14471447
assert_array_almost_equal(X.toarray(), X_trans_inv.toarray())
14481448

14491449

1450+
@pytest.mark.filterwarnings("ignore: The default value of `copy`") # 0.23
14501451
def test_quantile_transform_axis1():
14511452
X = np.array([[0, 25, 50, 75, 100],
14521453
[2, 4, 6, 8, 10],
@@ -1526,6 +1527,18 @@ def test_quantile_transform_nan():
15261527
assert not np.isnan(transformer.quantiles_[:, 1:]).any()
15271528

15281529

1530+
def test_deprecated_quantile_transform_copy():
1531+
future_message = ("The default value of `copy` will change from False to "
1532+
"True in 0.23 in order to make it more consistent with "
1533+
"the default `copy` values of other functions in "
1534+
":mod:`sklearn.preprocessing.data` and prevent "
1535+
"unexpected side effects by modifying the value of `X` "
1536+
"inplace. To avoid inplace modifications of `X`, it is "
1537+
"recommended to explicitly set `copy=True`")
1538+
assert_warns_message(FutureWarning, future_message, quantile_transform,
1539+
np.array([[0, 1], [0, 0.5], [1, 0]]))
1540+
1541+
15291542
def test_robust_scaler_invalid_range():
15301543
for range_ in [
15311544
(-1, 90),
@@ -2141,6 +2154,7 @@ def test_fit_cold_start():
21412154
scaler.fit_transform(X_2d)
21422155

21432156

2157+
@pytest.mark.filterwarnings("ignore: The default value of `copy`") # 0.23
21442158
def test_quantile_transform_valid_axis():
21452159
X = np.array([[0, 25, 50, 75, 100],
21462160
[2, 4, 6, 8, 10],

0 commit comments

Comments
 (0)