sklearn.impute
.IterativeImputer¶

class
sklearn.impute.
IterativeImputer
(estimator=None, missing_values=nan, sample_posterior=False, max_iter=10, tol=0.001, n_nearest_features=None, initial_strategy=’mean’, imputation_order=’ascending’, min_value=None, max_value=None, verbose=0, random_state=None)[source]¶ 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 roundrobin fashion.
Read more in the User Guide.
Parameters:  estimator : estimator object, default=BayesianRidge()
The estimator to use at each step of the roundrobin imputation. If
sample_posterior
is True, the estimator must supportreturn_std
in itspredict
method. missing_values : int, np.nan, optional (default=np.nan)
The placeholder for the missing values. All occurrences of
missing_values
will be imputed. sample_posterior : boolean, default=False
Whether to sample from the (Gaussian) predictive posterior of the fitted estimator for each imputation. Estimator must support
return_std
in itspredict
method if set toTrue
. Set toTrue
if usingIterativeImputer
for multiple imputations. max_iter : int, optional (default=10)
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
abs(max(X_t  X_{t1}))/abs(max(X[known_vals]))
< tol, whereX_t
isX
at iterationt. Note that early stopping is only applied if ``sample_posterior=False`
. tol : float, optional (default=1e3)
Tolerance of the stopping condition.
 n_nearest_features : int, optional (default=None)
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 speedup when the number of features is huge. If
None
, all features will be used. initial_strategy : str, optional (default=”mean”)
Which strategy to use to initialize the missing values. Same as the
strategy
parameter insklearn.impute.SimpleImputer
Valid values: {“mean”, “median”, “most_frequent”, or “constant”}. imputation_order : str, optional (default=”ascending”)
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.
 min_value : float, optional (default=None)
Minimum possible imputed value. Default of
None
will set minimum to negative infinity. max_value : float, optional (default=None)
Maximum possible imputed value. Default of
None
will set maximum to positive infinity. verbose : int, optional (default=0)
Verbosity flag, controls the debug messages that are issued as functions are evaluated. The higher, the more verbose. Can be 0, 1, or 2.
 random_state : int, RandomState instance or None, optional (default=None)
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
ifrandom
, and the sampling from posterior ifsample_posterior
is True. Use an integer for determinism. See the Glossary.
Attributes:  initial_imputer_ : object of type
sklearn.impute.SimpleImputer
Imputer used to initialize the missing values.
 imputation_sequence_ : list of tuples
Each tuple has
(feat_idx, neighbor_feat_idx, estimator)
, wherefeat_idx
is the current feature to be imputed,neighbor_feat_idx
is the array of other features used to impute the current feature, andestimator
is the trained estimator used for the imputation. Length isself.n_features_with_missing_ * self.n_iter_
. n_iter_ : int
Number of iteration rounds that occurred. Will be less than
self.max_iter
if early stopping criterion was reached. n_features_with_missing_ : int
Number of features with missing values.
See also
SimpleImputer
 Univariate imputation of missing values.
Notes
To support imputation in inductive mode we store each feature’s estimator during the
fit
phase, and predict without refitting (in order) during thetransform
phase.Features which contain all missing values at
fit
are discarded upontransform
.Features with missing values during
transform
which did not have any missing values duringfit
will be imputed with the initial imputation method only.References
[1] Stef van Buuren, Karin GroothuisOudshoorn (2011). “mice: Multivariate Imputation by Chained Equations in R”. Journal of Statistical Software 45: 167. [2] S. F. Buck, (1960). “A Method of Estimation of Missing Values in Multivariate Data Suitable for use with an Electronic Computer”. Journal of the Royal Statistical Society 22(2): 302306. Methods
fit
(X[, y])Fits the imputer on X and return self. fit_transform
(X[, y])Fits the imputer on X and return the transformed X. get_params
([deep])Get parameters for this estimator. set_params
(**params)Set the parameters of this estimator. transform
(X)Imputes all missing values in X. 
__init__
(estimator=None, missing_values=nan, sample_posterior=False, max_iter=10, tol=0.001, n_nearest_features=None, initial_strategy=’mean’, imputation_order=’ascending’, min_value=None, max_value=None, verbose=0, random_state=None)[source]¶

fit
(X, y=None)[source]¶ Fits the imputer on X and return self.
Parameters:  X : arraylike, shape (n_samples, n_features)
Input data, where “n_samples” is the number of samples and “n_features” is the number of features.
 y : ignored
Returns:  self : object
Returns self.

fit_transform
(X, y=None)[source]¶ Fits the imputer on X and return the transformed X.
Parameters:  X : arraylike, shape (n_samples, n_features)
Input data, where “n_samples” is the number of samples and “n_features” is the number of features.
 y : ignored.
Returns:  Xt : arraylike, shape (n_samples, n_features)
The imputed input data.

get_params
(deep=True)[source]¶ Get parameters for this estimator.
Parameters:  deep : boolean, optional
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns:  params : mapping of string to any
Parameter names mapped to their values.

set_params
(**params)[source]¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object.Returns:  self

transform
(X)[source]¶ Imputes all missing values in X.
Note that this is stochastic, and that if random_state is not fixed, repeated calls, or permuted input, will yield different results.
Parameters:  X : arraylike, shape = [n_samples, n_features]
The input data to complete.
Returns:  Xt : arraylike, shape (n_samples, n_features)
The imputed input data.