IterativeImputer
- class sklearn.impute.IterativeImputer(estimator=None, *, missing_values=nan, sample_posterior=False, max_iter=10, tol=0.001, n_nearest_features=, , , , , , , , , ,
Multivariate imputer that estimates each feature from all the others.
A strategy for imputing missing values by modeling each feature with missing values as a function of other features in a round-robin fashion.
Read more in the User Guide.
The estimator to use at each step of the round-robin imputation.
If sample_posterior=True, the estimator must support
return_std in its predict method.
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
should be set to np.nan, since will be converted to .
To support imputation in inductive mode we store each feature’s estimator
during the fit phase, and predict without refitting (in order)
during the transform phase.
Features which contain all missing values at fit are discarded upon
transform.
Using defaults, the imputer scales in
where = max_iter, the number of samples and
the number of features. It thus becomes prohibitively costly when
the number of features increases. Setting
, or increasing
can help to reduce its computational cost.
Depending on the nature of missing values, simple imputers can be preferable in a prediction context.
>>> import numpy as np
>>> from sklearn.experimental import enable_iterative_imputer
>>> from sklearn.impute import IterativeImputer
>>> imp_mean = IterativeImputer(random_state=0)
>>> imp_mean.fit([[7,
For a more detailed example see Imputing missing values before building an estimator or Imputing missing values with variants of IterativeImputer.
Fit the imputer on X and return self.
Input data, where n_samples is the number of samples and
n_features is the number of features.
Fit the imputer on X and return the transformed X.
Input data, where n_samples is the number of samples and
is the number of features.
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.
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.
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.
Note that this is stochastic, and that if random_state is not fixed,
repeated calls, or permuted input, results will differ.
The input data to complete.
Imputing missing values with variants of IterativeImputer
Imputing missing values before building an estimator
np.nanWhether to sample from the (Gaussian) predictive posterior of the
fitted estimator for each imputation. Estimator must support
return_std in its predict method if set to True. Set to
True if using IterativeImputer for multiple imputations.
Maximum number of imputation rounds to perform before returning the
imputations computed during the final round. A round is a single
imputation of each feature with missing values. The stopping criterion
is met once max(abs(X_t - X_{t-1}))/max(abs(X[known_vals])) < tol,
where X_t is X at iteration t. Note that early stopping is only
applied if sample_posterior=False.
Tolerance of the stopping condition.
Number of other features to use to estimate the missing values of
each feature column. Nearness between features is measured using
the absolute correlation coefficient between each feature pair (after
initial imputation). To ensure coverage of features throughout the
imputation process, the neighbor features are not necessarily nearest,
but are drawn with probability proportional to correlation for each
imputed target feature. Can provide significant speed-up when the
number of features is huge. If None, all features will be used.
Which strategy to use to initialize the missing values. Same as the
strategy parameter in SimpleImputer.
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.
The order in which the features will be imputed. Possible values:
'ascending': From features with fewest missing values to most.
'descending': From features with most missing values to fewest.
'roman': Left to right.
'arabic': Right to left.
'random': A random order for each round.
If True then features with missing values during transform
which did not have any missing values during fit will be
imputed with the initial imputation method only. Set to True if you
have many features with no missing values at both fit and
transform time to save compute.
Minimum possible imputed value. Broadcast to shape (n_features,) if
scalar. If array-like, expects shape (n_features,), one min value for
each feature. The default is -np.inf.
Maximum possible imputed value. Broadcast to shape (n_features,) if
scalar. If array-like, expects shape (n_features,), one max value for
each feature. The default is np.inf.
Verbosity flag, controls the debug messages that are issued as functions are evaluated. The higher, the more verbose. Can be 0, 1, or 2.
The seed of the pseudo random number generator to use. Randomizes
selection of estimator features if n_nearest_features is not None,
the imputation_order if random, and the sampling from posterior if
sample_posterior=True. Use an integer for determinism.
See the Glossary.
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
initial_strategy="constant" in which case fill_value will be
used instead.
SimpleImputerImputer used to initialize the missing values.
Each tuple has (feat_idx, neighbor_feat_idx, estimator), where
feat_idx is the current feature to be imputed,
neighbor_feat_idx is the array of other features used to impute the
current feature, and estimator is the trained estimator used for
the imputation. Length is self.n_features_with_missing_ *
self.n_iter_.
Number of iteration rounds that occurred. Will be less than
self.max_iter if early stopping criterion was reached.
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.
Number of features with missing values.
MissingIndicatorIndicator used to add binary indicators for missing values.
None if add_indicator=False.
RandomState instance that is generated either from a seed, the random
number generator or by np.random.
n_nearest_features << n_featuresskip_complete=TruetolNot used, present for API consistency by convention.
Parameters routed to the fit method of the sub-estimator via the
metadata routing API.
Fitted estimator.
Not used, present for API consistency by convention.
Parameters routed to the fit method of the sub-estimator via the
metadata routing API.
The imputed input data.
["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.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.
The imputed input data.