Using scikit-learn sklearn, how to handle missing data for linear regression?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Scikit-learn’s ordinary linear regression models do not accept missing values in the input matrix, so you need to clean or impute them before fitting. The safest approach is usually not to fill values manually in a dataframe and hope for the best, but to build an explicit preprocessing pipeline that handles missing data during both training and prediction.
Why Imputation Belongs in the Pipeline
A common beginner pattern is to fill missing values in the training dataframe, fit a model, and then forget to apply the same logic to future data. That leads to inconsistent preprocessing and broken predictions.
Scikit-learn gives you a better pattern: combine an imputer and a regression model in a Pipeline.
Here the imputer learns replacement values from the training data and applies the same transformation during prediction.
Choosing an Imputation Strategy
The simplest imputation strategies are:
- '
meanfor numeric columns' - '
medianfor numeric columns with outliers' - '
most_frequentfor categorical columns' - '
constantwhen you want a fixed placeholder'
For linear regression, mean or median is often a reasonable baseline for numeric features:
There is no universal best choice. Use a strategy that matches the data and the missingness pattern.
Handling Mixed Numeric and Categorical Data
Real datasets often contain both numeric and categorical columns. In that case, use a ColumnTransformer so each group gets the right preprocessing.
This keeps the preprocessing logic explicit and consistent.
Should You Drop Missing Rows Instead
Dropping rows can be acceptable when:
- only a tiny fraction of rows are missing
- the missingness is close to random
- removing those rows does not distort the dataset
Example:
The problem is that this can throw away useful signal or bias the training data if the missingness is systematic. Imputation is usually safer than aggressive row deletion unless the dataset is large and the missingness is minor.
Add Missingness Indicators When It Matters
Sometimes the fact that a value is missing is itself predictive. For example, missing income data may correlate with a specific customer behavior.
Scikit-learn can add indicator columns:
That tells the model not only the filled-in numeric value, but also whether the original value was missing.
This can help linear models when missingness is informative rather than random.
Validate the Whole Pipeline
Because imputation learns values from training data, cross-validation should evaluate the entire pipeline, not a manually pre-imputed dataset created before the split.
Correct:
That ensures the imputer is fitted separately inside each training fold. If you impute before cross-validation, you leak information from validation data into training.
Common Pitfalls
The biggest pitfall is filling missing values before the train-test split. That leaks information from future or validation rows into the preprocessing step.
Another common mistake is using one imputation rule for every column without considering data type or distribution. Numeric and categorical features usually need different handling.
People also sometimes forget prediction-time behavior. If you fill missing values manually during training but not in production, the model pipeline is incomplete.
Finally, simple imputation is only a baseline. If a feature is mostly missing or if missingness is structurally meaningful, you may need better feature engineering rather than a different fill value.
Summary
- Linear regression in scikit-learn needs missing values handled before fitting.
- Use a
Pipelineso imputation and prediction stay tied together. - '
SimpleImputeris a common baseline for numeric and categorical features.' - '
ColumnTransformeris the right tool for mixed-type datasets.' - Fit and validate the full preprocessing pipeline to avoid data leakage.

