SimpleImputer
- class sklearn.impute.SimpleImputer(*, missing_values=nan, strategy='mean', fill_value=None, copy=True, add_indicator=False, keep_empty_features=
Univariate imputer for completing missing values with simple strategies.
Replace missing values using a descriptive statistic (e.g. mean, median, or most frequent) along each column, or using a constant value.
Read more in the User Guide.
The placeholder for the missing values. All occurrences of
missing_values will be imputed. For pandas’ dataframes with
nullable integer dtypes with missing values, missing_values
can be set to either np.nan or pd.NA.
The imputation strategy.
If “mean”, then replace missing values using the mean along each column. Can only be used with numeric data.
If “median”, then replace missing values using the median along each column. Can only be used with numeric data.
If “most_frequent”, then replace missing using the most frequent value along each column. Can be used with strings or numeric data. If there is more than one such value, only the smallest is returned.
If “constant”, then replace missing values with fill_value. Can be used with strings or numeric data.
If an instance of Callable, then replace missing values using the scalar statistic returned by running the callable over a dense 1d array containing non-missing values of each column.
When strategy == “constant”, fill_value is used to replace all
occurrences of missing_values. For string or object data types,
fill_value must be a string.
If None, fill_value will be 0 when imputing numerical
data and “missing_value” for strings or object data types.
If True, a copy of X will be created. If False, imputation will
be done in-place whenever possible. Note that, in the following cases,
a new copy will always be made, even if copy=False:
If X is not an array of floating values;
If X is encoded as a CSR matrix;
If add_indicator=True.
If True, a MissingIndicator transform will stack onto output
of the imputer’s transform. This allows a predictive estimator
to account for missingness despite imputation. If a feature has no
missing values at fit/train time, the feature won’t appear on
the missing indicator even if there are missing values at
transform/test time.
If True, features that consist exclusively of missing values when
fit is called are returned in results when transform is called.
The imputed value is always 0 except when strategy="constant"
in which case fill_value will be used instead.
The imputation fill value for each feature.
Computing statistics can result in np.nan values.
During transform, features corresponding to np.nan
statistics will be discarded.
MissingIndicatorIndicator used to add binary indicators for missing values.
None if add_indicator=False.
Columns which only contained missing values at fit are discarded
upon transform if strategy is not "constant".
In a prediction context, simple imputation usually performs poorly when
associated with a weak learner. However, with a powerful learner, it can
lead to as good or better performance than complex imputation such as
IterativeImputer or KNNImputer.
>>> import numpy as np
>>> from sklearn.impute import SimpleImputer
>>> imp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')
>>> imp_mean.fit([[7, 2
For a more detailed example see Imputing missing values before building an estimator.
Fit the imputer on X.
Input data, where n_samples is the number of samples and
n_features is the number of features.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params
and returns a transformed version of X.
Get output feature names for transformation.
Input features.
If input_features is None, then feature_names_in_ is
used as feature names in. If is not defined,
then the following input feature names are generated:
.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Convert the data back to the original representation.
Inverts the transform operation performed on an array.
This operation can only be performed after SimpleImputer is
instantiated with add_indicator=True.
Note that inverse_transform can only invert the transform in
features that have binary indicators for missing values. If a feature
has no missing values at fit time, the feature won’t have a binary
indicator, and the imputation done at transform time won’t be
inverted.
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and .
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as Pipeline). The latter have
parameters of the form <component>__<parameter> so that it’s
possible to update each component of a nested object.
Estimator parameters.
Impute all missing values in X.
The input data to complete.
X with imputed values.
Imputing missing values with variants of IterativeImputer
Imputing missing values before building an estimator
Permutation Importance vs Random Forest Feature Importance (MDI)
Number of features seen during fit.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
Not used, present here for API consistency by convention.
Fitted estimator.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Pass only if the estimator accepts additional params in its fit method.
Transformed array.
["x0", "x1", ..., "x(n_features_in_ - 1)"]If input_features is an array-like, then input_features must
match feature_names_in_ if feature_names_in_ is defined.
Transformed feature names.
Added in version 0.24.
The imputed data to be reverted to original data. It has to be an augmented array of imputed data and the missing indicator mask.
The original X with missing values as it was prior
to imputation.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.