Handling Missing Data
title: Handling Missing Data keywords: [handling missing data, handling missing data in python, python data analysis, dataidea, machine learning, data preprocessing, sklearn data preprocessing] description: n this notebook, we look into the top four missing data imputation methods author: Juma Shafara date: "2024-03"

Introduction:
Missing data is a common hurdle in data analysis, impacting the reliability of insights drawn from datasets. Python offers a range of solutions to address this issue, some of which we discussed in the earlier weeks. In this notebook, we look into the top four missing data imputation methods:
- SimpleImputer
- KNNImputer
- IterativeImputer
- Datawig
We'll explore these essential techniques, using sklearn and the weather dataset.
loadDataset allows us to load datasets inbuilt in the dataidea library
Let's demonstrate how to use the top three missing data imputation methods—SimpleImputer, KNNImputer, and IterativeImputer—using the simple weather dataset.
SimpleImputer from scikit-learn:
- Usage: SimpleImputer is a straightforward method for imputing missing values by replacing them with a constant, mean, median, or most frequent value along each column.
- Pros:
- Easy to use and understand.
- Can handle both numerical and categorical data.
- Offers flexibility with different imputation strategies.
- Cons:
- It doesn't consider relationships between features.
- May not be the best choice for datasets with complex patterns of missingness.
- Example:
Let's have a look at the outcome
Exercise:
- Try out the SimpleImputer with different imputation strategies like mode, constant
- Choose and try some imputation techniques on categorical data
KNNImputer from scikit-learn:
- Usage:
- KNNImputer imputes missing values using k-nearest neighbors, replacing them with the mean value of the nearest neighbors.
- You can read more about the KNNImputer from the sklearn official docs site
- Pros:
- Considers relationships between features, making it suitable for datasets with complex patterns of missingness.
- Can handle both numerical and categorical data.
- Cons:
- Computationally expensive for large datasets.
- Requires careful selection of the number of neighbors (k).
Note!
By default, the KNNImputer uses 'nan' values as missing data and the 'nan_euclidean' metric to calculate the distances between values.
- Example:
If we take a look at the outcome
Filling a single column independently using the KNNImputer
To use the KNNImputer for a single independ column, you can use the index as the other column instead, this will result into equal euclidean distances resulting into the use of the physical neighbors in the data table.
Exercise
- Try out the KNNImputer with different numbers of neighbors and compare the results
- Findo out how to use KNNImputer to fill categorical data
IterativeImputer from scikit-learn:
- Usage: IterativeImputer models each feature with missing values as a function of other features and uses that estimate for imputation. It iteratively estimates the missing values.
- Pros:
- Takes into account relationships between features, making it suitable for datasets with complex missing patterns.
- More robust than SimpleImputer for handling missing data.
- Cons:
- Can be computationally intensive and slower than SimpleImputer.
- Requires careful tuning of model parameters.
- Example:
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
iterative_imputer = IterativeImputer()
temp_wind_iterative_imputed = iterative_imputer.fit_transform(temp_wind)
temp_wind_iterative_imputed_df = pd.DataFrame(temp_wind_iterative_imputed, columns=temp_wind.columns)
temp_wind_iterative_imputed_df
You can also choose an estimator of your choice, let's try a Linear Regression model
from sklearn.linear_model import LinearRegression
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
# set estimator to an instance of a model
iterative_imputer = IterativeImputer(estimator=LinearRegression())
temp_wind_iterative_imputed = iterative_imputer.fit_transform(temp_wind)
temp_wind_iterative_imputed_df = pd.DataFrame(temp_wind_iterative_imputed, columns=temp_wind.columns)
temp_wind_iterative_imputed_df
Datawig:
Datawig is a library specifically designed for imputing missing values in tabular data using deep learning models.
These top imputation methods offer different trade-offs in terms of computational complexity, handling of missing data patterns, and ease of use. The choice between them depends on the specific characteristics of the dataset and the requirements of the analysis.
Homework
- Try out these techniques for categorical data
Don't miss out on any updates and developments! Subscribe to the DATAIDEA Newsletter it's easy and safe.