AddMissingIndicator

The AddMissingIndicator() adds a binary variable indicating if observations are missing (missing indicator). It adds a missing indicator for both categorical and numerical variables. A list of variables for which to add a missing indicator can be passed, or the imputer will automatically select all variables.

The imputer has the option to select if binary variables should be added to all variables, or only to those that show missing data in the train set, by setting the option how=’missing_only’.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

import feature_engine.missing_data_imputers as mdi

# Load dataset
data = pd.read_csv('houseprice.csv')


# Separate into train and test sets
X_train, X_test, y_train, y_test = train_test_split(
data.drop(['Id', 'SalePrice'], axis=1), data['SalePrice'], test_size=0.3, random_state=0)

# set up the imputer
addBinary_imputer = mdi.AddMissingIndicator(
    variables=['Alley', 'MasVnrType', 'LotFrontage', 'MasVnrArea'])

# fit the imputer
addBinary_imputer.fit(X_train)

# transform the data
train_t = addBinary_imputer.transform(X_train)
test_t = addBinary_imputer.transform(X_test)

train_t[['Alley_na', 'MasVnrType_na', 'LotFrontage_na', 'MasVnrArea_na']].head()
../_images/missingindicator.png

API Reference

class feature_engine.missing_data_imputers.AddMissingIndicator(how='missing_only', variables=None)[source]

The AddMissingIndicator() adds an additional column or binary variable that indicates if data is missing.

AddMissingIndicator() will add as many missing indicators as variables indicated by the user, or variables with missing data in the train set.

The AddMissingIndicator() works for both numerical and categorical variables. The user can pass a list with the variables for which the missing indicators should be added as a list. Alternatively, the imputer will select and add missing indicators to all variables in the training set that show missing data.

Parameters
  • how (string, defatul='missing_only') –

    Indicates if missing indicators should be added to variables with missing data or to all variables.

    missing_only: indicators will be created only for those variables that showed missing data during fit.

    all: indicators will be created for all variables

  • variables (list, default=None) – The list of variables to be imputed. If None, the imputer will find and select all variables with missing data. Note: the transformer will first select all variables or all user entered variables and if how=missing_only, it will re-select from the original group only those that show missing data in during fit.

fit(X, y=None)[source]

Learns the variables for which the missing indicators will be created.

Parameters
  • X (pandas dataframe of shape = [n_samples, n_features]) – The training input samples.

  • y (None) – y is not needed in this imputation. You can pass None or y.

variables\_

the lit of variables for which the missing indicator will be created.

Type

list

transform(X)[source]

Adds the binary missing indicators.

Parameters

X (pandas dataframe of shape = [n_samples, n_features]) – The dataframe to be transformed.

Returns

X_transformed – The dataframe containing the additional binary variables. Binary variables are named with the original variable name plus ‘_na’.

Return type

pandas dataframe of shape = [n_samples, n_features]