def fit(self, X, y=None):
        """Fit the imputer on X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Input data, where ``n_samples`` is the number of samples and
            ``n_features`` is the number of features.

        Returns
        -------
        self : _SimpleImputer
        """
        X = self._validate_input(X)

        # default fill_value is 0 for numerical input and "missing_value"
        # otherwise
        if self.fill_value is None:
            if X.dtype.kind in ("i", "u", "f"):
                fill_value = 0
            else:
                fill_value = "missing_value"
        else:
            fill_value = self.fill_value

        # fill_value should be numerical in case of numerical input
        if (self.strategy == "constant" and
                X.dtype.kind in ("i", "u", "f") and
                not isinstance(fill_value, numbers.Real)):
            raise ValueError("'fill_value'={0} is invalid. Expected a "
                             "numerical value when imputing numerical "
                             "data".format(fill_value))

        if sparse.issparse(X):
            # missing_values = 0 not allowed with sparse data as it would
            # force densification
            if self.missing_values == 0:
                raise ValueError("Imputation not possible when missing_values "
                                 "== 0 and input is sparse. Provide a dense "
                                 "array instead.")
            else:
                self.statistics_ = self._sparse_fit(X,
                                                    self.strategy,
                                                    self.missing_values,
                                                    fill_value)
        else:
            self.statistics_ = self._dense_fit(X,
                                               self.strategy,
                                               self.missing_values,
                                               fill_value)

        return self
def fit ( self , X , y = None ) : X = self . _validate_input ( X ) if self . fill_value is None : if X . dtype . kind in ( "i" , "u" , "f" ) : fill_value = 0 else : fill_value = "missing_value" else : fill_value = self . fill_value if ( self . strategy == "constant" and X . dtype . kind in ( "i" , "u" , "f" ) and not isinstance ( fill_value , numbers . Real ) ) : raise ValueError ( "'fill_value'={0} is invalid. Expected a " "numerical value when imputing numerical " "data" . format ( fill_value ) ) if sparse . issparse ( X ) : if self . missing_values == 0 : raise ValueError ( "Imputation not possible when missing_values " "== 0 and input is sparse. Provide a dense " "array instead." ) else : self . statistics_ = self . _sparse_fit ( X , self . strategy , self . missing_values , fill_value ) else : self . statistics_ = self . _dense_fit ( X , self . strategy , self . missing_values , fill_value ) return self
Fit the imputer on X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Input data, where ``n_samples`` is the number of samples and
            ``n_features`` is the number of features.

        Returns
        -------
        self : _SimpleImputer