mojolearn.IterativeImputer#

class mojolearn.IterativeImputer(estimator=None, *, missing_values=nan, sample_posterior=False, max_iter=10, tol=0.001, n_nearest_features=None, initial_strategy='mean', fill_value=None, imputation_order='ascending', skip_complete=False, min_value=-inf, max_value=inf, verbose=0, random_state=None, add_indicator=False, keep_empty_features=False)[source]#

Bases: _PrepBase

Impute missing values by modeling each feature from the others, round by round.

Follows scikit-learn’s IterativeImputer. Missing entries are first filled by a SimpleImputer with initial_strategy. Then, for up to max_iter rounds, each feature with missing values is regressed on the other features over its observed rows, and its missing entries are replaced by the predictions, clipped to [min_value, max_value]. Fit stops early when the largest change in a round falls below tol times the largest absolute observed value. Input and output are float32.

The default estimator is a Bayesian ridge regression (default priors, at most 300 iterations, tol 1e-3), as in scikit-learn.

Parameters:
  • estimator (object, default None) – Regressor with fit and predict, cloned for each step and run on the host. None uses the built-in Bayesian ridge regression.

  • missing_values (float, default NaN) – Value that marks a missing entry.

  • sample_posterior (bool, default False) – Draw each imputed value from the predictive normal distribution, truncated to [min_value, max_value], instead of using its mean. A custom estimator must then support predict(X, return_std=True) (ValueError otherwise).

  • max_iter (int, default 10) – Maximum number of imputation rounds.

  • tol (float, default 1e-3) – Stopping tolerance, relative to the largest absolute observed value.

  • n_nearest_features (int, default None) – Number of other features used to predict each feature, drawn without replacement with probability proportional to their absolute correlation with it. None uses all of them. Must be an int >= 1.

  • initial_strategy ({‘mean’, ‘median’, ‘most_frequent’, ‘constant’}, default ‘mean’) – Strategy of the initial SimpleImputer.

  • fill_value (float, default None) – Constant for initial_strategy='constant'.

  • imputation_order ({‘ascending’, ‘descending’, ‘roman’, ‘arabic’, ‘random’}, default ‘ascending’) – Order in which features are imputed in each round: by increasing or decreasing count of missing values, left to right, right to left, or a new random permutation each round.

  • skip_complete (bool, default False) – Skip features with no missing values in fit.

  • min_value (float or array-like of shape (n_features,), default -inf) – Lower bound for imputed values.

  • max_value (float or array-like of shape (n_features,), default inf) – Upper bound for imputed values.

  • verbose (int, default 0) – Accepted for scikit-learn compatibility.

  • random_state (int or None, default None) – Seed for the random order, the feature draws and the posterior samples. None uses seed 0. Draws use mojolearn’s own generator, so they differ from scikit-learn’s for the same seed.

  • add_indicator (bool, default False) – Append missing-indicator columns (1.0 where missing), as scikit-learn’s MissingIndicator.

  • keep_empty_features (bool, default False) – Keep columns that had no observed values in fit (passed to the initial imputer).

Variables:
  • initial_imputer_ (SimpleImputer) – Imputer used for the initial fill.

  • imputation_sequence_ (list of tuples) – The fitted steps in the order they are applied.

  • n_iter_ (int) – Number of rounds run.

  • n_features_with_missing_ (int) – Number of features with missing values in fit.

  • n_features_in_ (int) – Number of columns seen in fit.

  • numeric_mode_ (str) – Numeric mode the estimator was fitted in; transform runs in the same mode.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 5), dtype=np.float32)
>>> X[rng.random(X.shape) < 0.1] = np.nan
>>> Xt = mojolearn.IterativeImputer(max_iter=5).fit_transform(X)
__init__(estimator=None, *, missing_values=nan, sample_posterior=False, max_iter=10, tol=0.001, n_nearest_features=None, initial_strategy='mean', fill_value=None, imputation_order='ascending', skip_complete=False, min_value=-inf, max_value=inf, verbose=0, random_state=None, add_indicator=False, keep_empty_features=False)[source]#

Methods

fit(X[, y])

fit_transform(X[, y])

Fit to X, then transform X.

get_params([deep])

Return the constructor parameters as a dict.

set_params(**params)

Set constructor parameters.

transform(X)

fit_transform(X, y=None)[source]#

Fit to X, then transform X.

Returns:

array – The transformed data.