scikit-learn filling missing values by random sampling
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

