scikit-learn
data imputation
missing values
random sampling
machine learning

scikit-learn filling missing values by random sampling

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Scikit-learn is a popular Python library widely used in the realm of machine learning due to its ease of use and rich functionalities for building predictive models. One common challenge data scientists face when working with real-world datasets is handling missing values. Missing data can introduce bias or lead to inaccurate analysis and model predictions if not treated properly. One straightforward yet effective technique to impute missing values is by using random sampling. This article will delve into the use of random sampling to fill missing values using scikit-learn and illustrate it with examples.

Motivation for Imputation with Random Sampling

Missing values in datasets can arise due to various reasons such as data entry errors, loss of data, or a survey participant skipping questions. Random sampling imputation is valuable as it helps to:

  • Maintain the original distribution of the data, particularly for numerical features.
  • Provide a non-deterministic approach which can be beneficial during cross-validation or when combined with ensemble methods.
  • Serve as a simple baseline imputation strategy to compare against more sophisticated methods.

Implementing Random Sampling Imputation with Scikit-learn

To implement missing value imputation via random sampling in scikit-learn, we can use the `SimpleImputer` class or custom transformations that support random selection strategies. The following sections showcase these techniques.

Simple Imputer Setup

While `SimpleImputer` in scikit-learn 0.24 introduced a basic strategy of replacing missing values with the 'mean', 'median', 'most_frequent', or 'constant', it does not directly support random sampling. However, we can extend this class or create a custom transformer:

Custom Transformer for Random Sampling

  • Ensure that consistent treatment is applied across the train, validation, and test sets to avoid data leakage.
  • Consider integrating random sampling with ensemble models (like Random Forest) to assess model robustness across varied imputations.
  • K-Nearest Neighbors Imputation: Fills missing values with averages from similar instances.
  • Iterative Imputation: PREDICTS missing values iteratively using statistical models.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.