Lensa ML

How Data Preprocessing Works

Cleaning and preparing data before it reaches your model

Step 1 of 6: Raw Data Is Messy

Missing values, different scales, weird formats

NameAgeIncomeCityScoreAlice2550kNYC82Bob?120kLA91Carol350.003NYC?Dave4285kChicago76Eve?1000kLA95Frank3167kChicago?
Missing values (?)
Outlier / bad entry
Categorical text

This table has all of these problems. Missing values leave gaps in Age and Score. Income sits in the hundreds of thousands while Age is in the tens, a scale difference, and it also holds suspect values: Carol's 0.003 looks like a data-entry error and Eve's 1,000k is an extreme outlier. Categorical text in City can't go straight into most algorithms. Data preprocessing fixes them before training.

Look over the six rows and the three colored problem types. Age is missing for Bob and Eve and Score for Carol and Frank, so four of six rows have a gap. Carol's income of 0.003 is a different kind of problem: a value that is present but almost certainly wrong, perhaps entered in the wrong unit. Empty cells are easy to find, while bad values need range checks. The Name column is a third issue. It identifies a person, so a model could memorize it without learning anything that generalizes, and it should not be a feature.

Step 2 of 6: Handling Missing Data

Fill, drop, or impute — strategies for gaps

Age column25R1NULL35R342R4NULL31R6

Dropping rows removes any row with missing values. Here that drops 2 of 6 rows, along with their Income and City values. Fine when few rows are affected.

Click Drop Row, Fill with Mean, and Interpolate and compare the bars for rows 2 and 5. Whether a strategy is safe depends on why the values are missing. If they are missing completely at random, dropping or mean-filling mostly costs precision. If the gap depends on the value itself, for example high earners skipping an income question, every simple fill biases the result. A common fix is to impute and add a 0/1 "was missing" column so the model can use the gap. The median is safer than the mean when a column has outliers.

Step 3 of 6: Feature Scaling

Bringing all features to the same range

12515007501000
Income
Score

Without scaling, Income (0-1000k) dominates Score (0-1). In a distance-based or gradient-based model, the bigger numbers swamp the smaller ones.

Click Raw, Min-Max, and Z-Score and watch the Income and Score lines move onto a shared axis. Income is in thousands, so Eve's 1,000 is $1 million while every other income is at most 120. For one extreme value like that, robust scaling, (x − median) / IQR, often beats both methods shown here, because the median and interquartile range ignore the tail. A log transform before scaling is another fix for skewed money columns. Whatever the method, its statistics come from the training rows and are reused unchanged on test rows.

Step 4 of 6: Encoding Categories

Turning words into numbers machines understand

Coloris_Redis_Blueis_Green→Red100Blue010Green001Red100Blue010

One-hot encoding creates a binary column for each category, so no false order is imposed: Red is not "greater than" Blue.

Click One-Hot Encoding and Ordinal Encoding and compare the output columns. One-hot turns a column with k categories into k binary columns, which becomes a problem when k is large: a ZIP code column could add thousands of mostly-zero columns. Linear models with an intercept usually drop one of the k columns, since the other k − 1 already determine it. For high-cardinality columns, target encoding replaces each category with its average target value, computed out of fold to avoid leakage. LightGBM and CatBoost can also handle categories natively.

Step 5 of 6: Train-Test Split

Never test on training data

Full Dataset (100%)

Training data teaches the model. Validation data tunes hyperparameters. Test data gives the final, unbiased score. If test data leaks into training, the score is overly optimistic and no longer measures generalization.

Click the Show button to see what happens without splitting, and compare the Train (70%), Val (15%), and Test (15%) bars. A random split is right for independent rows, but not for every dataset. Time series are split by date, so the model is always tested on a period after the one it learned from. Data with repeated entities, such as several scans of the same patient, is split by entity so no patient lands on both sides. With rare classes, a stratified split keeps the class ratio the same in every part.

Step 6 of 6: The Preprocessing Pipeline

Putting it all together in order

1RawData2Train/TestSplit3HandleMissing4ScaleFeatures5EncodeCategories6ReadyMessy, unprocessed inputPipeline stage 1 / 6

A preprocessing pipeline chains these steps. The split comes first, so every later step is fit on the training rows only and then applied unchanged to the test rows. scikit-learn's Pipeline enforces this.

Hover over each stage to read what it does while the highlight cycles through all six. A pipeline object separates fit from transform. Calling fit on the training set learns the imputation values, the scaling statistics, and the list of categories. Calling transform applies those exact numbers to any later data, including a single request in production. Running cross-validation on the whole pipeline refits every step inside each fold, which is the simplest way to rule out leakage. Categories that first appear at test time need a rule too, such as an all-zero one-hot row.

What is data preprocessing in machine learning?

Data preprocessing is the set of steps that turn raw records into clean numeric input a model can train on. It typically covers handling missing values, scaling numeric features, encoding categorical ones, and splitting the data into training, validation, and test sets. Each step is learned from the training data and then applied unchanged to new data.

Preprocessing decides much of a model's quality before any training starts. Distance-based models such as KNN and SVM give wrong answers on unscaled features, most libraries reject text categories and empty cells, and a careless split can make a weak model look excellent. In practice, tools such as scikit-learn's Pipeline and ColumnTransformer bundle these steps so the same transformations run in training, evaluation, and production.

Key ideas

  • Data preprocessing turns raw, messy records into clean numeric input a model can train on.
  • Missing values can be dropped or imputed, and the right choice depends on why they are missing.
  • Scaling puts features on comparable ranges, and outliers decide which scaling method works best.
  • One-hot encoding suits unordered categories, and ordinal encoding suits categories with a real order.
  • The data is split before any preprocessing step is fit, and every step is fit on training rows only.
  • A pipeline applies the same learned transformations to training, test, and production data.

Common questions

What is the difference between data cleaning and data preprocessing?

Data cleaning fixes errors in the records: duplicates, typos, impossible values, and inconsistent units. Preprocessing is broader and also includes transformations that are not fixes, such as scaling, encoding, and splitting. Cleaning usually happens once on the dataset, while preprocessing steps are fit on training data and reapplied to every new input.

What is data leakage?

Data leakage is when information that would not be available at prediction time reaches the model during training. Fitting a scaler on the full dataset before splitting, or using a feature computed from the target, are common examples. Leakage makes validation scores look better than real-world performance.

Do neural networks need data preprocessing?

Yes, though less feature engineering. Inputs are still scaled, for example by subtracting each color channel's mean and dividing by its standard deviation for images. Text is tokenized into integer IDs, and missing values still need a decision before training.

How should outliers be handled?

First check whether an outlier is an error or a real extreme value. Errors are fixed or removed. Real extremes can be kept and handled with a log transform, clipping at a high percentile, robust scaling, or a model such as a decision tree that is less sensitive to them.

Related explainers

References

  1. scikit-learn User Guide, "Preprocessing data"
  2. James, Witten, Hastie & Tibshirani, "An Introduction to Statistical Learning" (ISLR)
  3. Rubin (1976), "Inference and Missing Data", Biometrika

Last updated