ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to Feature Engineering
Chapter 1: The Role of Features in Machine Learning
Revisiting the Machine Learning Workflow
What Constitutes a Feature?
Impact of Feature Quality on Model Performance
Common Data Types and Their Challenges
Overview of Feature Engineering Tasks
Quiz for Chapter 1
Chapter 2: Handling Missing Data
Identifying Missing Values
Mechanisms of Missing Data (MCAR, MAR, MNAR)
Simple Imputation Strategies: Mean, Median, Mode
Creating Missing Value Indicators
Multivariate Imputation: KNN Imputer
Multivariate Imputation: Iterative Imputer
Comparing Imputation Methods
Hands-on Practical: Imputing Missing Data
Quiz for Chapter 2
Chapter 3: Encoding Categorical Features
Challenges with Categorical Data
Nominal vs. Ordinal Categories
One-Hot Encoding for Nominal Features
Ordinal Encoding for Ordered Features
Handling High Cardinality Features
Target Encoding (Mean Encoding)
Binary Encoding
Hashing Encoder
Comparing Encoding Methods
Hands-on Practical: Applying Encoding Techniques
Quiz for Chapter 3
Chapter 4: Feature Scaling and Transformation
The Need for Feature Scaling
Standardization (Z-score Scaling)
Normalization (Min-Max Scaling)
Scaling for Outliers
Log Transformation for Skewed Data
Box-Cox Transformation
Yeo-Johnson Transformation
Quantile Transformation
Choosing the Right Scaling/Transformation Method
Hands-on Practical: Scaling and Transforming Features
Quiz for Chapter 4
Chapter 5: Feature Creation
Motivation for Creating New Features
Interaction Features
Polynomial Features
Feature Creation from Date/Time Data
Binning Numerical Features
Domain-Specific Feature Engineering
Automated Feature Creation (Introduction)
Hands-on Practical: Engineering New Features
Quiz for Chapter 5
Chapter 6: Feature Selection
Importance of Feature Selection
Filter Methods Overview
Filter Methods: Variance Threshold
Filter Methods: Univariate Statistical Tests (ANOVA F-value, Chi-Squared)
Filter Methods: Correlation Analysis
Wrapper Methods Overview
Wrapper Methods: Recursive Feature Elimination (RFE)
Wrapper Methods: Sequential Feature Selection (SFS)
Embedded Methods Overview
Embedded Methods: Regularization (Lasso L1)
Embedded Methods: Tree-Based Feature Importance
Hands-on Practical: Selecting Features
Quiz for Chapter 6

Creating Missing Value Indicators

While techniques like mean, median, or mode imputation fill in the gaps left by missing data, they inherently discard one piece of information: the fact that a value was missing in the first place. Sometimes, the absence of data itself carries predictive strength. For instance, if customers who don't provide their age tend to behave differently, knowing that the age was missing could be a valuable signal for your model.

This is where missing value indicators come into play. An indicator is simply a new binary feature added to your dataset that flags whether the original value in a specific column was missing or not. Typically, it takes a value of 1 if the data was missing and 0 otherwise.

Why Use Indicators?

The primary motivation is to preserve the information conveyed by the pattern of missingness. Here are some scenarios:

  1. Missing Not At Random (MNAR): If the reason a value is missing is related to the target variable or other features in a way that simple imputation cannot capture, the indicator variable explicitly provides this signal to the model.
  2. Complementing Imputation: You can use indicators alongside imputation. First, create the indicator feature. Then, impute the missing values in the original column using mean, median, KNN, or another method. This approach gives the model both an estimated value and the knowledge that this value was originally missing. Tree-based models, like Random Forests or Gradient Boosting, can often effectively utilize this combination.
  3. Simplicity: Creating indicators is computationally inexpensive and straightforward to implement.

Implementation with Pandas

Creating indicator variables in Python using Pandas is direct. The .isnull() method returns a boolean Series (True where data is missing, False otherwise), which can then be converted to integers (1 for True, 0 for False).

Let's illustrate with an example. Suppose we have a DataFrame with missing values in the Age and Income columns:

import pandas as pd
import numpy as np

# Sample DataFrame
data = {'ID': [1, 2, 3, 4, 5],
        'Age': [25, 30, np.nan, 35, np.nan],
        'Income': [50000, 60000, 75000, np.nan, 90000],
        'Score': [85, 90, 78, 92, 88]}
df = pd.DataFrame(data)

print("Original DataFrame:")
print(df)

# Identify columns with missing values
cols_with_na = df.columns[df.isnull().any()].tolist()

# Create indicator features
for col in cols_with_na:
    df[col + '_missing_indicator'] = df[col].isnull().astype(int)

print("\nDataFrame with Missing Value Indicators:")
print(df)

Executing this code produces the following output:

Original DataFrame:
   ID   Age   Income  Score
0   1  25.0  50000.0     85
1   2  30.0  60000.0     90
2   3   NaN  75000.0     78
3   4  35.0      NaN     92
4   5   NaN  90000.0     88

DataFrame with Missing Value Indicators:
   ID   Age   Income  Score  Age_missing_indicator  Income_missing_indicator
0   1  25.0  50000.0     85                      0                         0
1   2  30.0  60000.0     90                      0                         0
2   3   NaN  75000.0     78                      1                         0
3   4  35.0      NaN     92                      0                         1
4   5   NaN  90000.0     88                      1                         0

As you can see, two new columns, Age_missing_indicator and Income_missing_indicator, have been added. They contain a 1 wherever the corresponding value in the original Age or Income column was NaN, and 0 otherwise.

Combining Indicators with Imputation

A common and often effective strategy is to use indicators in conjunction with an imputation method.

  1. Create Indicators: Add binary indicator columns for features with missing values, as shown above.
  2. Perform Imputation: Apply an imputation technique (e.g., mean, median, mode, KNNImputer, IterativeImputer) to fill the NaN values in the original columns.
from sklearn.impute import SimpleImputer

# Assume df is the DataFrame after adding indicators
# Impute missing values in the original 'Age' column using the median
median_imputer_age = SimpleImputer(strategy='median')
df['Age'] = median_imputer_age.fit_transform(df[['Age']])

# Impute missing values in the original 'Income' column using the mean
mean_imputer_income = SimpleImputer(strategy='mean')
df['Income'] = mean_imputer_income.fit_transform(df[['Income']])

print("\nDataFrame After Imputation (with Indicators):")
print(df)

Resulting DataFrame:

DataFrame After Imputation (with Indicators):
   ID   Age   Income  Score  Age_missing_indicator  Income_missing_indicator
0   1  25.0  50000.0     85                      0                         0
1   2  30.0  60000.0     90                      0                         0
2   3  30.0  75000.0     78                      1                         0
3   4  35.0  68750.0     92                      0                         1
4   5  30.0  90000.0     88                      1                         0

Now, the Age and Income columns have no missing values, but the corresponding _missing_indicator columns retain the information about where the original missing values were located. Your machine learning model can potentially learn from both the imputed value and the pattern of missingness.

Approaches

While useful, keep these points in mind:

  • Dimensionality: Adding indicators increases the number of features in your dataset. This might be a concern for models sensitive to high dimensionality if you have many columns with missing data.
  • Model Choice: Linear models might struggle to interpret the combined information from an imputed value and its indicator unless interaction terms are explicitly created. Tree-based models generally handle this interaction more naturally.
  • MCAR: If data is truly Missing Completely At Random (MCAR), the indicator variable might not provide any additional predictive value, as the missingness pattern is purely random and unrelated to other variables or the target.

Creating missing value indicators is a valuable technique in your feature engineering toolkit. It's simple to implement and ensures that potentially valuable information about data missingness isn't lost during the imputation process. Use it, especially when you suspect the missingness pattern itself might be informative or when using tree-based models.

  • Feature Engineering for Machine Learning, Alice Zheng, Amanda Casari, 2018 (O'Reilly Media) - This book provides practical strategies for creating effective features, including methods for handling missing data and the use of indicator variables.
  • Imputation of missing values, scikit-learn developers, 2023 - Official documentation for the sklearn.impute module, describing various imputation strategies and the add_indicator parameter.
  • Applied Predictive Modeling, Max Kuhn and Kjell Johnson, 2013 (Springer) DOI: 10.1007/978-1-4614-6849-3 - This book offers practical guidance on building predictive models, with sections on data preprocessing and handling missing values.
  • A Review on Missing Data Imputation in Machine Learning, Md. Imran Hossain, Md. Asraful Islam, Md. Abdul Mukit, Sheikh Abujar, and Mohammad Shidujaman, 2021 SN Computer Science, Vol. 2 (Springer) DOI: 10.1007/s42979-021-00762-7 - This review article surveys various missing data imputation techniques, offering insights into scenarios where indicator variables are beneficial.

© 2026 ApX Machine Learning