Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Data preprocessing for machine learning

Data preprocessing for machine learning takes raw, messy rows and turns them into a clean structured dataset a model can learn from. Impute the gaps, fence the outliers, scale the numbers, encode the labels, then split — in that order — so the algorithm sees signal instead of NaN, mixed date formats, and leaked test-set statistics.

If you meant image preprocessing (resize / denoise / window), that is what is image preprocessing. If you meant intensity scale on a volume, that is image normalization in medical imaging. If you meant changing voxel spacing, that is resampling in medical imaging.

What the job is

Raw data pulled from a CSV, a warehouse, or a form is almost never ready. Dates as "10-25-2023", "25/10/23", and "Oct 25, 2023" in one column. Empty cells as NaN or null. Age in years next to income in dollars. A single typo that records $1,000,000 instead of $1,000. Algorithms are literal. They do not guess.

Data cleaning is one course. Data preprocessing is the whole meal: clean, transform (scale / encode), reduce or engineer, then split. That is the one-line definition this page uses. Cleaning alone does not give you a train/test split or a fitted scaler.

  • Clean. Missing values, duplicates, inconsistent formats, IQR fences.
  • Transform. Scale numbers to a common range. Encode categories so the model sees integers, not “Red”.
  • Engineer / reduce. New columns from old ones, or drop the ones that do not help.
  • Split. Train / validation / test. Fit every transformer on train only.

Imputation

Empty cells crash some estimators and quietly bias others. Fill them on purpose.

  • Mean / median. Numeric. Use the median when outliers are in the column — the mean follows the extreme.
  • Mode. Categorical. The most frequent label.
  • Constant. 0 or -1 when “missing” is itself a signal the model should see.

Start there. KNN or a mini-model to predict the gap is a later step, for when a large share of the column is gone and a mean would invent a fake centre.

Outliers — IQR fences

An outlier is a point that does not fit. Typo, or a real extreme. Either way it can drag a distance-based model. The Interquartile Range fence is the usual first pass:

  1. Q1 = 25th percentile. Q3 = 75th.
  2. IQR = Q3 − Q1.
  3. Fence: Q1 − 1.5 * IQR to Q3 + 1.5 * IQR.
  4. Outside that range: drop, cap at the fence, or transform (log) so it hurts less.

Do not auto-drop in fraud or rare-event work. Those “outliers” are often the thing you are trying to find.

Feature scaling

Age (18–90) next to income ($30,000–$250,000) makes KNN, SVM, and most nets treat income as the only feature that exists. Put both on one scale.

Technique Method Output range When Outliers
Standardization (StandardScaler) Subtract mean, divide by standard deviation Mean 0, sd 1. No fixed min/max Roughly Gaussian data. SVM, logistic regression Less sensitive than min-max; extremes still move the mean
Normalization (MinMaxScaler) Squeeze to a fixed range Usually [0, 1] or [−1, 1] Nets and KNN that want bounded input One extreme compresses everyone else

Standardization is the safer default. Min-max when the architecture wants 0–1 and you have already fenced the extremes.

One-hot vs label encoding

Models take numbers. “Red” / “Green” / “Blue” is not a number. Encoding is the translation. The only question: does the order mean something?

  • Nominal (no rank: colour, country, brand) → one-hot. New binary columns is_Red, is_Green, is_Blue. One 1, the rest 0. The model cannot invent “Blue > Red”.
  • Ordinal (a real rank: Small / Medium / Large, Low / High) → label encoding. One integer column: 0, 1, 2. The order is the signal.

Hundreds of cities as one-hot explodes the width (the curse of dimensionality). Then you hash, target-encode, or drop the rare labels — you do not label-encode a country and teach the model that Canada is “more” than Austria.

Feature engineering and the split

A timestamp is a weak column. Hour-of-day, day-of-week, month are usually stronger. Two columns multiplied (bedrooms × floor area) can be a better living-space feature than either alone. That is engineering, not cleaning.

Then split. Typical cut: 70–80% train, optional validation, 10–20% test locked until the end. The test set is the exam. You do not peek.

Data leakage

Leakage is when test-set information contaminates training. The usual way: you fit a scaler or an imputer on the whole table, then split. The train set has already seen the test mean. Scores look brilliant. Production collapses.

Split first, preprocess second.

  1. Split the raw table.
  2. Fit the imputer / scaler / encoder on train only.
  3. Transform train and test with those fitted objects.

FAQ

Is data cleaning the same as data preprocessing?

No. Cleaning is one course — fix or drop bad cells, duplicates, wild formats. Preprocessing is the whole meal: clean, then scale, encode, engineer, and split. You can clean a table and still leak the test set.

Which step matters most?

The one that fixes the biggest hole in this table. Missing values and scale are the two that most often tank a model on their own. Distance-based estimators without scaling are not “a bit worse”; they are wrong.

Which imputation?

A few numeric gaps → median (if outliers) or mean. Categories → mode. A large hole → KNN or a model, or drop the column. Start simple. Mean / median / mode is a baseline, not a failure.

Can preprocessing introduce bias?

Yes. Filling income with the global mean can erase real group gaps. Dropping “outliers” in fraud removes the events you wanted. Document every choice. Ask whether it still represents the world the model will see.

How do I stop leakage?

Split first. Fit on train. Transform both. That is the whole rule.

PYCAD does the imaging side of a pipeline when the table is scans, not rows. Case studies.

We build custom medical imaging platforms — advanced DICOM viewers, AI segmentation, and the clinical systems around them.

Get in Touch

Copyright © 2026 PYCAD. All Rights Reserved.