Skip to content

Commit 5bfb9f7

Browse files
author
Perrine Letellier
committed
standardize the use of copy as a parameter of transform() method
1 parent 43c08fb commit 5bfb9f7

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

sklearn/preprocessing/data.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def partial_fit(self, X, y=None):
332332
self.data_range_ = data_range
333333
return self
334334

335-
def transform(self, X):
335+
def transform(self, X, copy=None):
336336
"""Scaling features of X according to feature_range.
337337
338338
Parameters
@@ -341,16 +341,16 @@ def transform(self, X):
341341
Input data that will be transformed.
342342
"""
343343
check_is_fitted(self, 'scale_')
344-
345-
X = check_array(X, copy=self.copy, ensure_2d=False, dtype=FLOAT_DTYPES)
344+
copy = copy if copy is not None else self.copy
345+
X = check_array(X, copy=copy, ensure_2d=False, dtype=FLOAT_DTYPES)
346346
if X.ndim == 1:
347347
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)
348348

349349
X *= self.scale_
350350
X += self.min_
351351
return X
352352

353-
def inverse_transform(self, X):
353+
def inverse_transform(self, X, copy=None):
354354
"""Undo the scaling of X according to feature_range.
355355
356356
Parameters
@@ -359,8 +359,8 @@ def inverse_transform(self, X):
359359
Input data that will be transformed. It cannot be sparse.
360360
"""
361361
check_is_fitted(self, 'scale_')
362-
363-
X = check_array(X, copy=self.copy, ensure_2d=False, dtype=FLOAT_DTYPES)
362+
copy = copy if copy is not None else self.copy
363+
X = check_array(X, copy=copy, ensure_2d=False, dtype=FLOAT_DTYPES)
364364
if X.ndim == 1:
365365
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)
366366

@@ -772,7 +772,7 @@ def partial_fit(self, X, y=None):
772772
self.scale_ = _handle_zeros_in_scale(max_abs)
773773
return self
774774

775-
def transform(self, X, y=None):
775+
def transform(self, X, y=None, copy=None):
776776
"""Scale the data
777777
778778
Parameters
@@ -781,7 +781,8 @@ def transform(self, X, y=None):
781781
The data that should be scaled.
782782
"""
783783
check_is_fitted(self, 'scale_')
784-
X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
784+
copy = copy if copy is not None else self.copy
785+
X = check_array(X, accept_sparse=('csr', 'csc'), copy=copy,
785786
ensure_2d=False, estimator=self, dtype=FLOAT_DTYPES)
786787

787788
if X.ndim == 1:
@@ -796,7 +797,7 @@ def transform(self, X, y=None):
796797
X /= self.scale_
797798
return X
798799

799-
def inverse_transform(self, X):
800+
def inverse_transform(self, X, copy=None):
800801
"""Scale back the data to the original representation
801802
802803
Parameters
@@ -805,6 +806,7 @@ def inverse_transform(self, X):
805806
The data that should be transformed back.
806807
"""
807808
check_is_fitted(self, 'scale_')
809+
copy = copy if copy is not None else self.copy
808810
X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
809811
ensure_2d=False, estimator=self, dtype=FLOAT_DTYPES)
810812
if X.ndim == 1:
@@ -934,9 +936,9 @@ def __init__(self, with_centering=True, with_scaling=True, copy=True):
934936
self.with_scaling = with_scaling
935937
self.copy = copy
936938

937-
def _check_array(self, X, copy):
939+
def _check_array(self, X, copy=False):
938940
"""Makes sure centering is not enabled for sparse matrices."""
939-
X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
941+
X = check_array(X, accept_sparse=('csr', 'csc'), copy=copy,
940942
ensure_2d=False, estimator=self, dtype=FLOAT_DTYPES)
941943

942944
if X.ndim == 1:
@@ -972,7 +974,7 @@ def fit(self, X, y=None):
972974
self.scale_ = _handle_zeros_in_scale(self.scale_, copy=False)
973975
return self
974976

975-
def transform(self, X, y=None):
977+
def transform(self, X, y=None, copy=None):
976978
"""Center and scale the data
977979
978980
Parameters
@@ -984,7 +986,8 @@ def transform(self, X, y=None):
984986
check_is_fitted(self, 'center_')
985987
if self.with_scaling:
986988
check_is_fitted(self, 'scale_')
987-
X = self._check_array(X, self.copy)
989+
copy = copy if copy is not None else self.copy
990+
X = self._check_array(X, copy)
988991
if X.ndim == 1:
989992
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)
990993

@@ -1001,7 +1004,7 @@ def transform(self, X, y=None):
10011004
X /= self.scale_
10021005
return X
10031006

1004-
def inverse_transform(self, X):
1007+
def inverse_transform(self, X, copy=None):
10051008
"""Scale back the data to the original representation
10061009
10071010
Parameters
@@ -1013,7 +1016,8 @@ def inverse_transform(self, X):
10131016
check_is_fitted(self, 'center_')
10141017
if self.with_scaling:
10151018
check_is_fitted(self, 'scale_')
1016-
X = self._check_array(X, self.copy)
1019+
copy = copy if copy is not None else self.copy
1020+
X = self._check_array(X, copy)
10171021
if X.ndim == 1:
10181022
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)
10191023

sklearn/preprocessing/imputation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def _dense_fit(self, X, strategy, missing_values, axis):
299299

300300
return most_frequent
301301

302-
def transform(self, X):
302+
def transform(self, X, copy=None):
303303
"""Impute all missing values in X.
304304
305305
Parameters
@@ -311,7 +311,8 @@ def transform(self, X):
311311
check_is_fitted(self, 'statistics_')
312312

313313
# Copy just once
314-
X = as_float_array(X, copy=self.copy, force_all_finite=False)
314+
copy = copy if copy is not None else self.copy
315+
X = as_float_array(X, copy=copy, force_all_finite=False)
315316

316317
# Since two different arrays can be provided in fit(X) and
317318
# transform(X), the imputation data need to be recomputed

0 commit comments

Comments
 (0)