@@ -432,8 +432,8 @@ def transform(self, X):
432432 """
433433 check_is_fitted (self )
434434
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 )
437437
438438 X *= self .scale_
439439 X += self .min_
@@ -760,9 +760,10 @@ def partial_fit(self, X, y=None, sample_weight=None):
760760 self : object
761761 Fitted scaler.
762762 """
763+ first_call = not hasattr (self , "n_samples_seen_" )
763764 X = self ._validate_data (X , accept_sparse = ('csr' , 'csc' ),
764765 estimator = self , dtype = FLOAT_DTYPES ,
765- force_all_finite = 'allow-nan' )
766+ force_all_finite = 'allow-nan' , reset = first_call )
766767
767768 if sample_weight is not None :
768769 sample_weight = _check_sample_weight (sample_weight , X ,
@@ -1097,9 +1098,10 @@ def transform(self, X):
10971098 Transformed array.
10981099 """
10991100 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' )
11031105
11041106 if sparse .issparse (X ):
11051107 inplace_column_scale (X , 1.0 / self .scale_ )
@@ -1398,9 +1400,10 @@ def transform(self, X):
13981400 Transformed array.
13991401 """
14001402 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' )
14041407
14051408 if sparse .issparse (X ):
14061409 if self .with_scaling :
@@ -1735,8 +1738,8 @@ def transform(self, X):
17351738 """
17361739 check_is_fitted (self )
17371740
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' ))
17401743
17411744 n_samples , n_features = X .shape
17421745
@@ -2038,7 +2041,7 @@ def transform(self, X, copy=None):
20382041 Transformed array.
20392042 """
20402043 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 )
20422045 return normalize (X , norm = self .norm , axis = 1 , copy = copy )
20432046
20442047 def _more_tags (self ):
@@ -2195,7 +2198,11 @@ def transform(self, X, copy=None):
21952198 Transformed array.
21962199 """
21972200 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 )
21992206
22002207 def _more_tags (self ):
22012208 return {'stateless' : True }
@@ -2291,7 +2298,7 @@ def transform(self, K, copy=True):
22912298 """
22922299 check_is_fitted (self )
22932300
2294- K = check_array (K , copy = copy , dtype = FLOAT_DTYPES )
2301+ K = self . _validate_data (K , copy = copy , dtype = FLOAT_DTYPES , reset = False )
22952302
22962303 K_pred_cols = (np .sum (K , axis = 1 ) /
22972304 self .K_fit_rows_ .shape [0 ])[:, np .newaxis ]
@@ -2689,16 +2696,7 @@ def _transform_col(self, X_col, quantiles, inverse):
26892696 def _check_inputs (self , X , in_fit , accept_sparse_negative = False ,
26902697 copy = False ):
26912698 """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 ,
27022700 accept_sparse = 'csc' , copy = copy ,
27032701 dtype = FLOAT_DTYPES ,
27042702 force_all_finite = 'allow-nan' )
@@ -2718,16 +2716,6 @@ def _check_inputs(self, X, in_fit, accept_sparse_negative=False,
27182716
27192717 return X
27202718
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-
27312719 def _transform (self , X , inverse = False ):
27322720 """Forward and inverse transform.
27332721
@@ -2777,8 +2765,8 @@ def transform(self, X):
27772765 Xt : {ndarray, sparse matrix} of shape (n_samples, n_features)
27782766 The projected data.
27792767 """
2768+ check_is_fitted (self )
27802769 X = self ._check_inputs (X , in_fit = False , copy = self .copy )
2781- self ._check_is_fitted (X )
27822770
27832771 return self ._transform (X , inverse = False )
27842772
@@ -2798,9 +2786,9 @@ def inverse_transform(self, X):
27982786 Xt : {ndarray, sparse matrix} of (n_samples, n_features)
27992787 The projected data.
28002788 """
2789+ check_is_fitted (self )
28012790 X = self ._check_inputs (X , in_fit = False , accept_sparse_negative = True ,
28022791 copy = self .copy )
2803- self ._check_is_fitted (X )
28042792
28052793 return self ._transform (X , inverse = True )
28062794
@@ -3262,6 +3250,10 @@ def _check_input(self, X, in_fit, check_positive=False, check_shape=False,
32623250 ----------
32633251 X : array-like of shape (n_samples, n_features)
32643252
3253+ in_fit : bool
3254+ Whether or not `_check_input` is called from `fit` or other
3255+ methods, e.g. `predict`, `transform`, etc.
3256+
32653257 check_positive : bool, default=False
32663258 If True, check that all data is positive and non-zero (only if
32673259 ``self.method=='box-cox'``).
@@ -3273,7 +3265,8 @@ def _check_input(self, X, in_fit, check_positive=False, check_shape=False,
32733265 If True, check that the transformation method is valid.
32743266 """
32753267 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 )
32773270
32783271 with np .warnings .catch_warnings ():
32793272 np .warnings .filterwarnings (
0 commit comments