Data Preparation for Machine Learning

Author: Jason Brownlee
File Type: pdf
Size: 3.12 MB
Language: English
Pages: 398

Data Preparation for Machine Learning: Data Cleaning, Feature Selection, and Data Transforms in Python

Introduction

Machine learning models are only as reliable as the data used to train them. A sophisticated algorithm cannot compensate for incomplete records, inconsistent values, irrelevant variables, duplicated observations, or poorly scaled features. 🧠📊

Data preparation is therefore one of the most important stages of a machine learning project. Before a model learns patterns, engineers and data scientists must transform raw information into a structured dataset that algorithms can interpret efficiently.

Image

Image

Image

Image

Image

A typical preparation workflow may include:

  • 🔍 Inspecting the original dataset
  • 🧹 Cleaning incorrect or missing information
  • 🗑️ Removing duplicates and unnecessary records
  • 🎯 Selecting useful features
  • 🔄 Transforming numerical and categorical variables
  • 📏 Scaling numerical features when appropriate
  • 🧪 Splitting data into training and testing sets
  • ⚙️ Building a reproducible preprocessing pipeline

Python is particularly popular for this work because libraries such as Pandas, NumPy, and Scikit-learn provide practical tools for handling almost every stage of preprocessing.

Image

Image

Image

Whether you are a student developing your first classification model or an engineer building a production forecasting system, understanding data preparation is essential.


Background Theory

Machine learning algorithms operate on structured representations of information. Real-world datasets, however, are rarely delivered in a perfectly usable form.

A customer dataset might contain age values written in different formats. A sensor dataset could contain missing measurements. A construction dataset might combine measurements recorded using different units. A financial dataset could contain extreme values caused by unusual transactions.

These problems create a gap between raw data and model-ready data.

The Data Preparation Pipeline

A robust workflow generally follows this sequence:

Raw Data → Inspection → Cleaning → Feature Selection → Transformation → Dataset Splitting → Model Training

The exact order can vary depending on the project. Importantly, operations that learn information from the data—such as calculating scaling parameters or imputing values—should normally be fitted using the training data rather than the complete dataset.

Why Preparation Matters

Poor preparation can cause:

  • Reduced predictive performance
  • Unstable models
  • Biased results
  • Excessive training time
  • Data leakage
  • Difficult-to-reproduce experiments
  • Incorrect business conclusions

Good preparation, by contrast, can make a relatively simple algorithm perform surprisingly well. 🚀


Definition

Data preparation for machine learning is the process of inspecting, cleaning, selecting, transforming, and organizing raw data so that it can be effectively and safely used by a machine learning algorithm.

It consists of several related activities.

Data Cleaning

Data cleaning identifies and corrects problems such as:

  • Missing values
  • Duplicate rows
  • Invalid categories
  • Incorrect data types
  • Impossible measurements
  • Inconsistent formatting
  • Suspicious outliers

Feature Selection

Feature selection determines which available variables should be provided to the model.

For example, a dataset may contain 100 columns, but only 20 may contain meaningful predictive information.

Data Transformation

Data transformation changes the representation of variables without necessarily changing their underlying meaning.

Examples include:

  • Scaling numerical features
  • Encoding categories
  • Transforming skewed distributions
  • Converting dates into useful variables
  • Normalizing specific measurements

Feature Engineering

Feature engineering goes one step further by creating new variables from existing information.

For example, an engineering dataset containing building dimensions could produce additional features representing ratios, areas, or other domain-specific characteristics.


Step-by-Step Data Preparation in Python

A practical preparation process can be organized into several stages.

Image

Image

Image

Image

Step 1: Load and Inspect the Dataset

Python’s Pandas library is commonly used to load tabular data.

import pandas as pd

data = pd.read_csv("dataset.csv")

print(data.head())
print(data.info())
print(data.describe())

The first objective is not to modify anything.

Instead, understand what you have.

Check:

  • Number of rows
  • Number of columns
  • Data types
  • Missing values
  • Numerical variables
  • Categorical variables
  • Potential target variable
  • Unusual values

Step 2: Identify Missing Values

Missing information is extremely common.

print(data.isnull().sum())

A missing value does not automatically mean that the entire row should be deleted.

Possible strategies include:

  • Removing rows
  • Removing problematic columns
  • Filling numerical values with a statistical estimate
  • Filling categorical values with the most appropriate category
  • Using more advanced imputation techniques

The correct choice depends on why the data is missing and how important the variable is.

Step 3: Remove Duplicates

Duplicate observations can distort the distribution of a dataset.

data = data.drop_duplicates()

However, engineers should verify whether repeated rows are genuinely duplicates. Some repeated observations may represent legitimate events.

Step 4: Correct Data Types

A numerical column may accidentally be imported as text.

data["temperature"] = pd.to_numeric(
    data["temperature"],
    errors="coerce"
)

Dates may also require conversion:

data["date"] = pd.to_datetime(data["date"])

Correct data types make subsequent analysis considerably easier.

Step 5: Investigate Outliers

Outliers are observations that differ substantially from the majority of records.

An outlier could represent:

  • A genuine unusual event
  • A measurement error
  • A data-entry mistake
  • Equipment failure
  • A rare but important situation

Do not automatically delete every outlier. ⚠️

In engineering, an extreme measurement may actually represent the most important operating condition.

Step 6: Separate Features and Target

Suppose the objective is to predict equipment failure.

The target might be:

failure_status

while potential features could include:

temperature
pressure
vibration
operating_hours

Keeping the target separate helps prevent accidental use of the answer as an input.

Step 7: Split the Dataset

A common workflow separates data into training and testing portions.

from sklearn.model_selection import train_test_split

X = data.drop("failure_status", axis=1)
y = data["failure_status"]

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.2,
    random_state=42
)

The test dataset should represent information the model has not seen during training.

Step 8: Select Useful Features

Feature selection can be based on:

  • Domain knowledge
  • Correlation analysis
  • Statistical tests
  • Model-based importance
  • Recursive feature elimination
  • Regularization
  • Mutual information

The objective is not necessarily to use the largest possible number of variables.

A smaller set of meaningful features can sometimes produce a faster, simpler, and more interpretable model.

Step 9: Transform Numerical Features

Some algorithms perform better when numerical variables exist on comparable scales.

Scikit-learn provides several options.

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

Notice the difference:

Training data: fit_transform()

Testing data: transform()

This distinction helps prevent information from the test set from influencing preprocessing.

Step 10: Encode Categorical Features

Machine learning algorithms generally require numerical representations.

For example:

Material
Steel
Concrete
Wood

can be represented using one-hot encoding.

from sklearn.preprocessing import OneHotEncoder

encoder = OneHotEncoder(handle_unknown="ignore")

The resulting representation allows an algorithm to process categorical information without incorrectly assuming that one category is numerically larger than another.


Comparison of Data Preparation Techniques

Different preprocessing techniques solve different problems.

TechniqueMain PurposeTypical Use
Missing-value imputationHandle incomplete recordsMedical, sensor, business data
Duplicate removalEliminate repeated observationsCustomer and transaction datasets
One-hot encodingConvert categoriesMachine learning with categorical data
StandardizationPut numerical features on comparable scalesDistance-based and gradient-based models
Min-max scalingRestrict values to a defined rangeNeural networks and specific algorithms
Feature selectionRemove irrelevant variablesHigh-dimensional datasets
Log transformationReduce strong skewFinancial and scientific data
Outlier analysisInvestigate unusual observationsSensors, finance, engineering

ImageImage

Image

Image

Feature Selection vs Feature Transformation

These two concepts are often confused.

Feature selection chooses existing variables.

For example:

Temperature
Pressure
Humidity
Voltage

might become:

Temperature
Pressure

The original variables remain unchanged.

Feature transformation changes the representation of variables.

For example:

Annual income

could be transformed into a scaled or logarithmic representation.

Therefore:

Selection decides which information to keep, while transformation decides how information should be represented.


Diagrams and Data Preparation Architecture

A reliable machine learning workflow can be visualized as:

                 RAW DATA
                     │
                     ▼
              ┌─────────────┐
              │   Inspect   │
              └──────┬──────┘
                     │
                     ▼
              ┌─────────────┐
              │    Clean    │
              │ Missing Data│
              │ Duplicates  │
              │ Invalid Data│
              └──────┬──────┘
                     │
                     ▼
              ┌─────────────┐
              │   Select    │
              │  Features   │
              └──────┬──────┘
                     │
                     ▼
              ┌─────────────┐
              │ Transform   │
              │ Scale/Encode│
              └──────┬──────┘
                     │
                     ▼
              ┌─────────────┐
              │ Train Model │
              └──────┬──────┘
                     │
                     ▼
              ┌─────────────┐
              │   Evaluate  │
              └─────────────┘

Image

Image

Image

Using a Scikit-learn Pipeline

For professional projects, preprocessing can be organized into a reproducible pipeline.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

pipeline = Pipeline([
    ("scaler", StandardScaler()),
    ("model", LogisticRegression())
])

pipeline.fit(X_train, y_train)
predictions = pipeline.predict(X_test)

Pipelines reduce the risk of applying preprocessing inconsistently.


Practical Examples

Example 1: Customer Churn

Imagine a telecommunications company with information about customers.

The dataset contains:

  • Customer age
  • Subscription type
  • Monthly spending
  • Contract duration
  • Customer support contacts
  • Churn status

Some customers have missing spending information.

The preparation process could identify missing values, standardize numerical information, encode subscription categories, and remove irrelevant administrative fields.

The resulting dataset is more suitable for predicting which customers may leave.

Example 2: Predictive Maintenance

Consider an industrial machine monitored by sensors.

The system records:

  • Temperature
  • Pressure
  • Vibration
  • Rotational speed
  • Operating hours

Some sensor readings are missing because communication occasionally fails.

Instead of simply deleting every incomplete record, engineers can investigate the missing-data pattern and select an appropriate imputation strategy.

Feature selection can then determine which measurements contribute useful predictive information.

Example 3: Construction Engineering

A building-performance dataset could contain:

  • Building height
  • Floor area
  • Material type
  • Occupancy
  • Energy consumption
  • Outdoor temperature

Categorical material information must be encoded, numerical measurements may need scaling, and suspicious measurements should be investigated before training a prediction model.

This demonstrates why domain knowledge is extremely valuable during data preparation.


Real-World Applications

Data preparation is used across almost every engineering and technology sector.

Manufacturing

Factories use sensor data to predict equipment failures, optimize production, and identify abnormal operating conditions.

Civil Engineering

Machine learning can assist with structural monitoring, construction scheduling, material performance prediction, and infrastructure maintenance.

Energy Engineering

Power systems generate enormous quantities of measurements. Data preparation helps transform these readings into useful inputs for forecasting and anomaly detection.

Software Engineering

Machine learning systems can analyze application logs, performance metrics, user behavior, and defect information.

Finance

Financial systems must handle missing records, categorical variables, extreme values, changing distributions, and potentially noisy observations.

Healthcare Technology

Healthcare datasets often contain heterogeneous variables, incomplete records, and strict requirements around reliable preprocessing.


Common Mistakes

Cleaning Before Understanding

Deleting unusual observations immediately can remove valuable information.

Better approach: investigate why the observation is unusual.

Scaling Before Splitting

Calculating preprocessing parameters using the entire dataset can introduce information leakage.

Better approach: split first and fit preprocessing on training data.

Using Too Many Features

More columns do not automatically mean a better model.

Excessive features can increase complexity and noise.

Ignoring Categorical Variables

Treating categories as arbitrary numbers can create false relationships.

For example, encoding:

Concrete = 1
Steel = 2
Wood = 3

may incorrectly imply that Wood is mathematically greater than Concrete.

Deleting Too Much Data

Removing every row containing a missing value may drastically reduce the training dataset.

Ignoring Domain Knowledge

Automated techniques are useful, but engineers understand physical relationships and operational constraints that algorithms may not recognize.


Challenges and Solutions

ChallengePossible Solution
Large number of missing valuesInvestigate missingness and use appropriate imputation
High-dimensional datasetApply feature selection or dimensionality reduction
Mixed data typesUse separate numerical and categorical preprocessing
Strongly skewed featuresConsider suitable transformations
Extreme observationsInvestigate their origin before removal
Data leakageFit preprocessing only on training information
Inconsistent preprocessingUse reusable pipelines
Changing production dataMonitor distributions after deployment

The Challenge of Data Leakage

Data leakage is particularly dangerous.

Imagine preparing a complete dataset by calculating statistics from both training and test records and then splitting it afterward.

The model has indirectly gained information about the test dataset.

This can produce impressive evaluation results that fail in production.

The safer pattern is:

Raw Dataset
     ↓
Train/Test Split
     ↓
Fit preprocessing on Training Data
     ↓
Transform Training + Test Data
     ↓
Train Model
     ↓
Evaluate

Case Study: Predicting Machine Failure

Consider an industrial manufacturer that wants to predict machine failures before they happen.

The raw dataset contains several months of sensor information.

Initial Problem

Engineers discover:

  • Missing temperature readings
  • Duplicate sensor records
  • Several categorical machine types
  • Strongly different numerical ranges
  • A small number of extreme vibration measurements
  • Variables unrelated to machine failure

Preparation Strategy

First, duplicate records are investigated and legitimate duplicates are retained while accidental duplicates are removed.

Next, missing sensor values are analyzed.

The categorical machine-type variable is encoded.

Numerical variables are scaled where appropriate.

The extreme vibration readings are investigated rather than automatically deleted. Engineers discover that some represent genuine high-load operating conditions.

Feature selection removes variables that contain little useful predictive information.

Finally, the complete workflow is implemented using reproducible preprocessing components.

Result

The important improvement is not simply a higher model score.

The resulting system becomes:

  • Easier to maintain
  • Easier to evaluate
  • Less vulnerable to inconsistent preprocessing
  • More interpretable
  • Better suited to production deployment

This illustrates an important engineering principle:

Good machine learning begins with good data engineering. ⚙️


Essential Tips

Start With Data Profiling

Before writing sophisticated machine learning code, understand the dataset.

Document Every Transformation

Keep a record of why variables were removed, modified, encoded, or transformed.

Preserve Raw Data

Never overwrite the original dataset. Keep an untouched source version.

Use Pipelines

Pipelines make preprocessing more consistent and reproducible.

Validate Assumptions

Ask whether a missing value, outlier, or unusual category has a meaningful real-world explanation.

Avoid Data Leakage

Always consider whether information from validation or test data is influencing training.

Combine Automation With Domain Knowledge

Automated feature selection is powerful, but engineering expertise can identify relationships that purely statistical methods overlook.

Monitor Production Data

A preprocessing strategy that works today may become less effective when operating conditions change.


FAQs

What is data preparation in machine learning?

Data preparation is the process of cleaning, organizing, selecting, and transforming raw data so that it can be safely and effectively used by a machine learning model.

Why is data cleaning important?

Data cleaning reduces problems caused by missing, duplicated, inconsistent, invalid, or incorrectly formatted information. Better-quality inputs generally make machine learning workflows more reliable.

What is feature selection?

Feature selection is the process of identifying the most useful variables and removing features that are irrelevant, redundant, or potentially harmful to model performance.

What is the difference between feature selection and feature engineering?

Feature selection chooses existing variables, while feature engineering creates or modifies variables to provide a more useful representation of the underlying problem.

Should missing values always be removed?

No. Removing incomplete records is only one possible strategy. Depending on the dataset, imputation or other approaches may preserve valuable information.

Why should categorical variables be encoded?

Many machine learning algorithms require numerical inputs. Encoding converts categorical information into representations that algorithms can process appropriately.

Why is data leakage dangerous?

Data leakage allows information that should be unavailable during training to influence the model. This can create misleadingly strong evaluation results and poor real-world performance.

Is Python good for data preparation?

Yes. Python provides a mature ecosystem including Pandas for data manipulation, NumPy for numerical operations, and Scikit-learn for preprocessing and machine learning workflows.


Conclusion

Data preparation is not a minor preliminary task—it is a central engineering stage of every successful machine learning project. 🧠⚙️

Cleaning improves data quality, feature selection reduces unnecessary complexity, and transformations create representations that algorithms can process effectively. Python makes these operations accessible through a powerful ecosystem of data-science libraries.

A reliable workflow should therefore move systematically from inspection → cleaning → feature selection → transformation → validation → modeling.

For beginners, the most important lesson is simple: do not rush to train the model. Understand the data first.

For experienced professionals, the focus should extend toward reproducibility, leakage prevention, pipeline design, monitoring, and domain-specific validation.

Ultimately, the strongest machine learning systems are built not only with sophisticated algorithms, but with carefully prepared, well-understood, and responsibly transformed data. 🚀📊

Unlock exclusive content
Enjoy all premium content by watching a short ad
Preparing ad...
BY ADX360