RandomSampleImputer

class feature_engine.imputation.RandomSampleImputer(variables=None, random_state=None, seed='general', seeding_method='add')[source]

The RandomSampleImputer() replaces missing data with a random sample extracted from the variables in the training set.

The RandomSampleImputer() works with both numerical and categorical variables.

Note

The Random samples used to replace missing values may vary from execution to execution. This may affect the results of your work. Thus, it is advisable to set a seed.

More details in the User Guide.

Parameters
variables: list, default=None

The list of variables to be imputed. If None, the imputer will select all variables in the train set.

random_state: int, str or list, default=None

The random_state can take an integer to set the seed when extracting the random samples. Alternatively, it can take a variable name or a list of variables, which values will be used to determine the seed, observation per observation.

seed: str, default=’general’

Indicates whether the seed should be set for each observation with missing values, or if one seed should be used to impute all observations in one go.

‘general’: one seed will be used to impute the entire dataframe. This is equivalent to setting the seed in pandas.sample(random_state).

‘observation’: the seed will be set for each observation using the values of the variables indicated in the random_state for that particular observation.

seeding_method: str, default=’add’

If more than one variable are indicated to seed the random sampling per observation, you can choose to combine those values as an addition or a multiplication. Can take the values ‘add’ or ‘multiply’.

Attributes
X_:

Copy of the training dataframe from which to extract the random samples.

variables_:

The group of variables that will be transformed.

n_features_in_:

The number of features in the train set used in fit.

Methods

fit:

Make a copy of the train set

transform:

Impute missing data.

fit_transform:

Fit to the data, then transform it.

fit(X, y=None)[source]

Makes a copy of the train set. Only stores a copy of the variables to impute. This copy is then used to randomly extract the values to fill the missing data during transform.

Parameters
X: pandas dataframe of shape = [n_samples, n_features]

The training dataset.

y: None

y is not needed in this imputation. You can pass None or y.

fit_transform(X, y=None, **fit_params)[source]

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters
Xarray-like of shape (n_samples, n_features)

Input samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).

**fit_paramsdict

Additional fit parameters.

Returns
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns
paramsdict

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 Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters
**paramsdict

Estimator parameters.

Returns
selfestimator instance

Estimator instance.

transform(X)[source]

Replace missing data with random values taken from the train set.

Parameters
X: pandas dataframe of shape = [n_samples, n_features]

The dataframe to be transformed.

Returns
X_new: pandas dataframe of shape = [n_samples, n_features]

The dataframe without missing values in the transformed variables.

rtype

DataFrame ..