@@ -432,8 +432,8 @@ def transform(self, X):
432
432
"""
433
433
check_is_fitted (self )
434
434
435
- X = check_array (X , copy = self .copy , dtype = FLOAT_DTYPES ,
436
- force_all_finite = "allow-nan" )
435
+ X = self . _validate_data (X , copy = self .copy , dtype = FLOAT_DTYPES ,
436
+ force_all_finite = "allow-nan" , reset = False )
437
437
438
438
X *= self .scale_
439
439
X += self .min_
@@ -760,9 +760,10 @@ def partial_fit(self, X, y=None, sample_weight=None):
760
760
self : object
761
761
Fitted scaler.
762
762
"""
763
+ first_call = not hasattr (self , "n_samples_seen_" )
763
764
X = self ._validate_data (X , accept_sparse = ('csr' , 'csc' ),
764
765
estimator = self , dtype = FLOAT_DTYPES ,
765
- force_all_finite = 'allow-nan' )
766
+ force_all_finite = 'allow-nan' , reset = first_call )
766
767
767
768
if sample_weight is not None :
768
769
sample_weight = _check_sample_weight (sample_weight , X ,
@@ -1097,9 +1098,10 @@ def transform(self, X):
1097
1098
Transformed array.
1098
1099
"""
1099
1100
check_is_fitted (self )
1100
- X = check_array (X , accept_sparse = ('csr' , 'csc' ), copy = self .copy ,
1101
- estimator = self , dtype = FLOAT_DTYPES ,
1102
- force_all_finite = 'allow-nan' )
1101
+ X = self ._validate_data (X , accept_sparse = ('csr' , 'csc' ),
1102
+ copy = self .copy , reset = False ,
1103
+ estimator = self , dtype = FLOAT_DTYPES ,
1104
+ force_all_finite = 'allow-nan' )
1103
1105
1104
1106
if sparse .issparse (X ):
1105
1107
inplace_column_scale (X , 1.0 / self .scale_ )
@@ -1398,9 +1400,10 @@ def transform(self, X):
1398
1400
Transformed array.
1399
1401
"""
1400
1402
check_is_fitted (self )
1401
- X = check_array (X , accept_sparse = ('csr' , 'csc' ), copy = self .copy ,
1402
- estimator = self , dtype = FLOAT_DTYPES ,
1403
- force_all_finite = 'allow-nan' )
1403
+ X = self ._validate_data (X , accept_sparse = ('csr' , 'csc' ),
1404
+ copy = self .copy , estimator = self ,
1405
+ dtype = FLOAT_DTYPES , reset = False ,
1406
+ force_all_finite = 'allow-nan' )
1404
1407
1405
1408
if sparse .issparse (X ):
1406
1409
if self .with_scaling :
@@ -1735,8 +1738,8 @@ def transform(self, X):
1735
1738
"""
1736
1739
check_is_fitted (self )
1737
1740
1738
- X = check_array (X , order = 'F' , dtype = FLOAT_DTYPES ,
1739
- accept_sparse = ('csr' , 'csc' ))
1741
+ X = self . _validate_data (X , order = 'F' , dtype = FLOAT_DTYPES , reset = False ,
1742
+ accept_sparse = ('csr' , 'csc' ))
1740
1743
1741
1744
n_samples , n_features = X .shape
1742
1745
@@ -2038,7 +2041,7 @@ def transform(self, X, copy=None):
2038
2041
Transformed array.
2039
2042
"""
2040
2043
copy = copy if copy is not None else self .copy
2041
- X = check_array (X , accept_sparse = 'csr' )
2044
+ X = self . _validate_data (X , accept_sparse = 'csr' , reset = False )
2042
2045
return normalize (X , norm = self .norm , axis = 1 , copy = copy )
2043
2046
2044
2047
def _more_tags (self ):
@@ -2195,7 +2198,11 @@ def transform(self, X, copy=None):
2195
2198
Transformed array.
2196
2199
"""
2197
2200
copy = copy if copy is not None else self .copy
2198
- return binarize (X , threshold = self .threshold , copy = copy )
2201
+ # TODO: This should be refactored because binarize also calls
2202
+ # check_array
2203
+ X = self ._validate_data (X , accept_sparse = ['csr' , 'csc' ], copy = copy ,
2204
+ reset = False )
2205
+ return binarize (X , threshold = self .threshold , copy = False )
2199
2206
2200
2207
def _more_tags (self ):
2201
2208
return {'stateless' : True }
@@ -2291,7 +2298,7 @@ def transform(self, K, copy=True):
2291
2298
"""
2292
2299
check_is_fitted (self )
2293
2300
2294
- K = check_array (K , copy = copy , dtype = FLOAT_DTYPES )
2301
+ K = self . _validate_data (K , copy = copy , dtype = FLOAT_DTYPES , reset = False )
2295
2302
2296
2303
K_pred_cols = (np .sum (K , axis = 1 ) /
2297
2304
self .K_fit_rows_ .shape [0 ])[:, np .newaxis ]
@@ -2689,16 +2696,7 @@ def _transform_col(self, X_col, quantiles, inverse):
2689
2696
def _check_inputs (self , X , in_fit , accept_sparse_negative = False ,
2690
2697
copy = False ):
2691
2698
"""Check inputs before fit and transform."""
2692
- # In theory reset should be equal to `in_fit`, but there are tests
2693
- # checking the input number of feature and they expect a specific
2694
- # string, which is not the same one raised by check_n_features. So we
2695
- # don't check n_features_in_ here for now (it's done with adhoc code in
2696
- # the estimator anyway).
2697
- # TODO: set reset=in_fit when addressing reset in
2698
- # predict/transform/etc.
2699
- reset = True
2700
-
2701
- X = self ._validate_data (X , reset = reset ,
2699
+ X = self ._validate_data (X , reset = in_fit ,
2702
2700
accept_sparse = 'csc' , copy = copy ,
2703
2701
dtype = FLOAT_DTYPES ,
2704
2702
force_all_finite = 'allow-nan' )
@@ -2718,16 +2716,6 @@ def _check_inputs(self, X, in_fit, accept_sparse_negative=False,
2718
2716
2719
2717
return X
2720
2718
2721
- def _check_is_fitted (self , X ):
2722
- """Check the inputs before transforming."""
2723
- check_is_fitted (self )
2724
- # check that the dimension of X are adequate with the fitted data
2725
- if X .shape [1 ] != self .quantiles_ .shape [1 ]:
2726
- raise ValueError ('X does not have the same number of features as'
2727
- ' the previously fitted data. Got {} instead of'
2728
- ' {}.' .format (X .shape [1 ],
2729
- self .quantiles_ .shape [1 ]))
2730
-
2731
2719
def _transform (self , X , inverse = False ):
2732
2720
"""Forward and inverse transform.
2733
2721
@@ -2777,8 +2765,8 @@ def transform(self, X):
2777
2765
Xt : {ndarray, sparse matrix} of shape (n_samples, n_features)
2778
2766
The projected data.
2779
2767
"""
2768
+ check_is_fitted (self )
2780
2769
X = self ._check_inputs (X , in_fit = False , copy = self .copy )
2781
- self ._check_is_fitted (X )
2782
2770
2783
2771
return self ._transform (X , inverse = False )
2784
2772
@@ -2798,9 +2786,9 @@ def inverse_transform(self, X):
2798
2786
Xt : {ndarray, sparse matrix} of (n_samples, n_features)
2799
2787
The projected data.
2800
2788
"""
2789
+ check_is_fitted (self )
2801
2790
X = self ._check_inputs (X , in_fit = False , accept_sparse_negative = True ,
2802
2791
copy = self .copy )
2803
- self ._check_is_fitted (X )
2804
2792
2805
2793
return self ._transform (X , inverse = True )
2806
2794
@@ -3262,6 +3250,10 @@ def _check_input(self, X, in_fit, check_positive=False, check_shape=False,
3262
3250
----------
3263
3251
X : array-like of shape (n_samples, n_features)
3264
3252
3253
+ in_fit : bool
3254
+ Whether or not `_check_input` is called from `fit` or other
3255
+ methods, e.g. `predict`, `transform`, etc.
3256
+
3265
3257
check_positive : bool, default=False
3266
3258
If True, check that all data is positive and non-zero (only if
3267
3259
``self.method=='box-cox'``).
@@ -3273,7 +3265,8 @@ def _check_input(self, X, in_fit, check_positive=False, check_shape=False,
3273
3265
If True, check that the transformation method is valid.
3274
3266
"""
3275
3267
X = self ._validate_data (X , ensure_2d = True , dtype = FLOAT_DTYPES ,
3276
- copy = self .copy , force_all_finite = 'allow-nan' )
3268
+ copy = self .copy , force_all_finite = 'allow-nan' ,
3269
+ reset = in_fit )
3277
3270
3278
3271
with np .warnings .catch_warnings ():
3279
3272
np .warnings .filterwarnings (
0 commit comments