EndTailImputer#
- class feature_engine.imputation.EndTailImputer(imputation_method='gaussian', tail='right', fold=3, variables=None)[source]#
The EndTailImputer() replaces missing data by a value at either tail of the distribution. It works only with numerical variables.
You can indicate the variables to impute in a list. Alternatively, the EndTailImputer() will automatically select all numerical variables.
The imputer first calculates the values at the end of the distribution for each variable (fit). The values at the end of the distribution are determined using the Gaussian limits, the the IQR proximity rule limits, or a factor of the maximum value:
- Gaussian limits:
right tail: mean + 3*std
left tail: mean - 3*std
- IQR limits:
right tail: 75th quantile + 3*IQR
left tail: 25th quantile - 3*IQR
where IQR is the inter-quartile range = 75th quantile - 25th quantile
- Maximum value:
right tail: max * 3
left tail: not applicable
You can change the factor that multiplies the std, IQR or the maximum value using the parameter
fold
(we usedfold=3
in the examples above).The imputer then replaces the missing data with the estimated values (transform).
More details in the User Guide.
- Parameters
- imputation_method: str, default=’gaussian’
Method to be used to find the replacement values. Can take ‘gaussian’, ‘iqr’ or ‘max’.
‘gaussian’: the imputer will use the Gaussian limits to find the values to replace missing data.
‘iqr’: the imputer will use the IQR limits to find the values to replace missing data.
‘max’: the imputer will use the maximum values to replace missing data. Note that if ‘max’ is passed, the parameter ‘tail’ is ignored.
- tail: str, default=’right’
Indicates if the values to replace missing data should be selected from the right or left tail of the variable distribution. Can take values ‘left’ or ‘right’.
- fold: int, default=3
Factor to multiply the std, the IQR or the Max values. Recommended values are 2 or 3 for Gaussian, or 1.5 or 3 for IQR.
- variables: list, default=None
The list of numerical variables to transform. If None, the transformer will automatically find and select all numerical variables.
- Attributes
- imputer_dict_:
Dictionary with the values to replace missing data in each variable.
- variables_:
The group of variables that will be transformed.
- feature_names_in_:
List with the names of features seen during
fit
.- n_features_in_:
The number of features in the train set used in fit.
Examples
>>> import pandas as pd >>> import numpy as np >>> from feature_engine.imputation import EndTailImputer >>> X = pd.DataFrame(dict(x1 = [np.nan,0.5, 0.5, 0,np.nan])) >>> eti = EndTailImputer(imputation_method='gaussian', tail='right', fold=3) >>> eti.fit(X) >>> eti.transform(X) x1 0 1.199359 1 0.500000 2 0.500000 3 0.000000 4 1.199359
Methods
fit:
Learn values to replace missing data.
fit_transform:
Fit to data, then transform it.
get_feature_names_out:
Get output feature names for transformation.
get_params:
Get parameters for this estimator.
set_params:
Set the parameters of this estimator.
transform:
Impute missing data.
- fit(X, y=None)[source]#
Learn the values at the end of the variable distribution.
- Parameters
- X: pandas dataframe of shape = [n_samples, n_features]
The training dataset.
- y: pandas Series, default=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
andy
with optional parametersfit_params
and returns a transformed version ofX
.- 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_feature_names_out(input_features=None)[source]#
Get output feature names for transformation. In other words, returns the variable names of transformed dataframe.
- Parameters
- input_featuresarray or list, default=None
This parameter exits only for compatibility with the Scikit-learn pipeline.
If
None
, thenfeature_names_in_
is used as feature names in.If an array or list, then
input_features
must matchfeature_names_in_
.
- Returns
- feature_names_out: list
Transformed feature names.
- 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 the learned parameters.
- Parameters
- X: pandas dataframe of shape = [n_samples, n_features]
The data to be transformed.
- Returns
- X_new: pandas dataframe of shape = [n_samples, n_features]
The dataframe without missing values in the selected variables.
- rtype
DataFrame
..