Chapter 6
Data Preparation and Deep-Learning Workflow
Diploma in Artificial Intelligence and Machine Learning
Basics Deep Learning and Neural Networks
•
Diploma
Data Preparation and Deep-Learning Workflow
Introduction
Deep-learning performance depends not only on neural-network architecture. It is strongly determined by the quality, representativeness and preparation of the data used to train the model. A sophisticated network trained on duplicated, biased, incorrectly labelled or improperly scaled data may perform worse than a simple model trained on a carefully designed dataset.
Data preparation is therefore an engineering and scientific activity. It includes acquiring data, understanding its structure, correcting errors, transforming variables, creating suitable representations, dividing observations into independent subsets, and documenting every operation. In deep learning, these activities must be integrated into a reproducible workflow so that training results can be repeated and evaluation remains trustworthy.
A central principle is that information from the test set must not influence model development. If test information is used during preprocessing, feature selection, augmentation or hyperparameter tuning, the reported performance may be optimistically biased. This problem is called data leakage.
Learning Objectives
By the end of this chapter, the learner should be able to:
Describe the stages of a deep-learning data workflow.
Inspect and clean tabular, image and text data.
Encode categorical and textual information appropriately.
Normalize and standardize numerical inputs.
Explain data augmentation and its limitations.
Partition data into training, validation and test sets.
Detect leakage, class imbalance and poor data quality.
Design a reproducible preprocessing pipeline.
Evaluate ethical, privacy and governance concerns in dataset preparation.
1. The Deep-Learning Data Workflow
A practical workflow usually contains the following stages:
Define the task and target variable.
Acquire and document data.
Audit quality, structure and representativeness.
Split data into development and final evaluation sets.
Fit preprocessing operations using training data only.
Transform validation and test data using the fitted operations.
Apply suitable augmentation to training data.
Train the model and monitor validation performance.
Evaluate once, or sparingly, on the untouched test set.
Package preprocessing and model logic for deployment.
Leakage-safe deep-learning data workflow
Figure: Leakage-safe deep-learning data workflow
Figure: A leakage-safe deep-learning workflow. Preprocessing parameters are learned from training data and then applied consistently to other partitions.
1.1 Defining the learning problem
The target must be specified before preparation begins. In classification, the target is a class label; in regression, it is a continuous value; in segmentation, each pixel or region may have a label; and in sequence forecasting, the target is associated with a future time period. The unit of analysis must also be clear. For example, a medical study may contain many records from the same patient. Treating each record as independent can produce an unrealistically easy test set if records from one patient occur in both training and test partitions.
The data dictionary should record variable names, meanings, types, units, allowed values, missing-value codes, source systems and ownership. This documentation is valuable for both debugging and governance.
2. Data Acquisition and Quality Audit
Data may be obtained from databases, sensors, application logs, surveys, public repositories or human annotation. Acquisition should record the source, collection date, sampling process, consent conditions, licensing restrictions and known limitations.
A quality audit examines:
completeness: whether required values are present;
validity: whether values obey permitted formats and ranges;
representativeness: whether important populations or operating conditions are included.
Missing data require diagnosis rather than automatic replacement. A blank may mean that a measurement was not taken, a sensor failed, a participant declined to answer, or the value is not applicable. Common strategies include deletion, constant replacement, mean or median imputation, model-based imputation and an explicit missing category. The choice depends on the variable, missingness mechanism and application risk.
For a numerical feature x, training-set mean imputation replaces a missing value with the mean calculated from observed training values:
xmissing ← μtrain = (1/nobs) ∑i=1nobs xi
Here, xi are observed training values and nobs is their count. This method is simple but reduces variation and can distort relationships. The mean must not be calculated using validation or test observations.
Outliers should be investigated, not automatically deleted. An extreme value may be an error, a rare but genuine event, or evidence that the model must handle an unusual operating condition. Robust approaches include domain-based bounds, logarithmic transformation, clipping and robust scaling. Each decision should be recorded.
Duplicate records can cause leakage, particularly when near-duplicates appear in different partitions. In image datasets, multiple frames from the same video are not independent examples. In text datasets, copied documents or repeated templates can similarly inflate performance.
3. Data Representation and Encoding
Neural networks operate on numerical tensors, but raw information may be categorical, textual, visual or sequential.
3.1 Numerical variables
Numerical values should retain meaningful units during storage. A temperature recorded in degrees Celsius and a mass recorded in kilograms should not be treated as interchangeable merely because both are numbers. Unit conversion should occur before modelling when necessary.
3.2 Categorical variables
Nominal categories have no natural order, such as colour or product type. One-hot encoding represents a category with a binary vector. If a variable has k categories, each observation is represented by a vector of length k, with one position equal to 1 and the others equal to 0. For high-cardinality variables, one-hot encoding may create very wide inputs; embeddings or carefully designed grouping may be more suitable.
Ordinal categories have an order, such as low, medium and high. Integer encoding can be appropriate only when the ordering is meaningful and the numerical distances do not create a misleading interpretation. Neural-network embeddings are often useful for categorical inputs because they learn compact continuous representations, but they still require careful validation and sufficient data.
Unknown categories at inference time should be handled explicitly, for example with an “unknown” token. A deployed system should not fail simply because a new product code or vocabulary item appears.
3.3 Images
Images are commonly represented as tensors with height, width and channels. Pixel values may be stored as integers from 0 to 255 and converted to floating-point values. Colour-channel order, image orientation, aspect ratio and resolution must be standardized. Resizing can distort objects; cropping can remove important evidence. These choices should reflect the task.
3.4 Text and sequences
Text preparation may include normalization, tokenization, vocabulary construction, padding and truncation. The vocabulary must be built from the training corpus only when the goal is a strict leakage-safe evaluation. Padding makes sequences equal in length, while masking prevents padding tokens from influencing computations. Truncation can discard useful information and should be monitored.
4. Scaling and Normalization
Different numerical features may have very different ranges. Scaling can improve optimization because gradients are often easier to manage when input magnitudes are comparable.
4.1 Standardization
Standardization transforms a feature using its training-set mean and standard deviation:
zi = (xi − μtrain) / σtrain
Here, xi is the original value, μtrain is the training mean, and σtrain is the training standard deviation. The result is dimensionless. This operation is appropriate when a feature is reasonably represented by its centre and spread. If σtrain = 0, the feature is constant in the training data and should usually be removed or handled separately.
4.2 Min–max scaling
Min–max scaling maps a feature to an interval, commonly [0, 1]:
The denominator must be non-zero. This method is sensitive to extreme values and may produce values outside [0, 1] for future observations beyond the training range. It is often useful for bounded inputs, but it is not universally superior to standardization.
4.3 Robust scaling
A robust transformation uses the median and interquartile range:
x'i = (xi − mediantrain) / IQRtrain
The interquartile range is IQR = Q3 − Q1, where Q1 and Q3 are the first and third quartiles. Robust scaling is less affected by extreme observations, although it does not correct errors or guarantee a particular distribution.
Worked Example: Training-only standardization
Suppose the training values for a feature are 10, 12, 14 and 16. Their mean is 13. The population-form standard deviation is 2.236, while the sample standard deviation is approximately 2.582. A preprocessing library must specify which convention it uses. Using the population form, the standardized value for 16 is:
z = (16 − 13) / 2.236 ≈ 1.34
The same training mean and standard deviation must be used to transform validation and test records. Recomputing them separately changes the coordinate system and can conceal distribution differences.
5. Partitioning Data Correctly
The training set is used to fit model parameters. The validation set supports model selection, hyperparameter tuning, threshold selection and early stopping. The test set estimates final generalization performance after development decisions are complete.
A random partition is reasonable when observations are independent and identically distributed enough for the intended use. Stratified splitting preserves approximate class proportions in classification. Grouped splitting keeps all observations from the same person, device, household or document source in one partition. Time-based splitting trains on earlier observations and evaluates on later observations when deployment is temporal.
For a dataset of N examples and proportions p_train, p_validation and p_test, the counts approximately satisfy:
Ntrain + Nvalidation + Ntest = N
and
ptrain + pvalidation + ptest = 1
These are bookkeeping identities, not guarantees of statistical validity. The partition strategy must represent how the model will be used.
Preprocessing, feature selection, vocabulary creation, oversampling and augmentation policy design should be fitted or decided using training data only. A test set should be stored with restricted access and used as a final audit rather than as a development dashboard.
6. Data Augmentation
Augmentation creates additional training examples through transformations that preserve the target label. In image classification, examples include small rotations, translations, crops, brightness changes and horizontal flips when the task permits them. In audio, controlled time shifts or background noise may be appropriate. In text, augmentation is more difficult because synonym substitution or paraphrasing can change meaning.
An augmentation is valid only if it preserves the semantic label. A horizontal flip may be safe for a vehicle-recognition task but unsafe for text recognition or medical images where laterality matters. Excessive transformations can produce unrealistic examples and harm learning.
Augmentation should normally be applied to training examples during training. Validation and test data should reflect the intended evaluation distribution. Test-time augmentation can be used in advanced systems, but it must be specified in advance and evaluated without turning the test set into a tuning resource.
7. Class Imbalance and Sampling
Class imbalance occurs when some classes are much more frequent than others. Accuracy can then be misleading. If 95% of examples belong to class A, a model that always predicts A achieves 95% accuracy but has no useful ability to identify class B.
Responses include class-weighted loss, stratified sampling, oversampling the minority class, undersampling the majority class and collecting more representative data. Oversampling must be performed after splitting; otherwise, a duplicated minority example may appear in both training and test sets.
For a binary classification problem, a weighted cross-entropy loss can be written as:
L = −[w1 y log(p) + w0 (1 − y) log(1 − p)]
Here, y ∈ {0,1} is the true label, p is the predicted probability of class 1, and w1 and w0 are non-negative class weights. The logarithm is natural logarithm in most implementations. Weights increase the penalty for errors on selected classes; they do not create new information.
Evaluation should include class-specific precision, recall, F1-score, confusion matrices and, where appropriate, precision–recall curves. The metric must reflect the cost of false positives and false negatives.
8. Leakage Prevention
Leakage occurs when information unavailable at prediction time, or information derived from evaluation data, influences training or model selection. Common examples include:
scaling before splitting;
imputing with statistics from the complete dataset;
selecting features using labels from the test set;
placing records from one patient in multiple partitions;
using future values in a forecasting feature;
augmenting before splitting;
tuning repeatedly against the test set;
including a target-derived field that would not exist at inference time.
Leakage can be subtle. A hospital identifier may act as a proxy for the label if particular hospitals use different diagnostic practices. A timestamp may reveal the outcome because data were recorded after an intervention. Leakage analysis therefore requires domain knowledge, not only code inspection.
A robust implementation fits a preprocessing object on training data, stores its parameters, and applies the same object to validation, test and production inputs. Pipeline systems should make it difficult to call fitting operations on evaluation data accidentally.
9. Reproducibility and Operational Design
Reproducibility means that another person can reconstruct the dataset, transformations, training configuration and evaluation process. Important controls include:
fixed and recorded random seeds where feasible;
versioned source data and labels;
immutable test partitions;
documented software and hardware environments;
configuration files for preprocessing and training;
data and model version identifiers;
checksums or hashes for important files;
experiment tracking;
logging of sample counts and class distributions;
automated validation tests.
A seed does not guarantee identical results across all hardware, libraries or parallel execution modes. Reproducibility should therefore be described at an appropriate level: exact reproducibility, statistical reproducibility or procedural reproducibility.
A production pipeline should treat preprocessing as part of the model. If training data are standardized but production inputs are not, the deployed network receives a different distribution. Serialization of the complete pipeline reduces this risk.
A manufacturing company wants to predict whether a pump will fail within the next seven days using vibration, temperature, pressure and maintenance records. Data are collected every hour from 200 pumps over two years.
Problem
An initial model reports 98% accuracy. However, maintenance engineers doubt the result because failures are rare and multiple hourly records come from the same pump. The dataset also contains a field called “repair completed,” which is entered after a failure.
Analysis
The high accuracy is partly caused by class imbalance. More seriously, the repair field is a post-event variable and cannot be known seven days before failure. Random row-wise splitting also places measurements from the same pump in training and test sets. This allows the model to learn pump-specific signatures rather than general failure patterns.
Approach and solution
The team removes post-event fields and defines the prediction time precisely. It uses a time-based split: earlier months for training, a later period for validation, and the most recent period for testing. All records from a pump are kept within the appropriate temporal partition, and an additional grouped analysis checks performance on pumps not seen during training.
Missing sensor values are imputed using training-set statistics, with missingness indicators added when sensor failure may itself be informative. Numerical variables are standardized using training parameters. The minority failure class receives a weighted loss. Evaluation reports recall, precision and the number of false alarms, rather than accuracy alone.
Outcome
Test accuracy falls to 91%, but the result is more credible. Recall for impending failures improves to a level acceptable to engineers, and the false-alarm rate is incorporated into maintenance scheduling. The organization deploys the preprocessing and model as one versioned pipeline and monitors performance by pump type and operating environment.
Learning points
The case demonstrates that lower measured performance can represent better scientific validity. Correct target timing, leakage prevention, temporal evaluation and operationally relevant metrics are more valuable than an inflated accuracy score.
11. Ethical, Privacy and Governance Considerations
Data preparation can affect people even when the final system is technical. Personal data should be collected and processed under applicable law, contractual requirements and organizational policy. Data minimization limits collection to information necessary for the stated purpose. Sensitive attributes may require stronger access control, consent procedures, anonymization or pseudonymization.
Removing demographic variables does not automatically remove bias. Other features may act as proxies, and under-representation may reduce performance for particular groups. Dataset documentation should describe population coverage, exclusions, labelling procedures and known limitations.
Labels may encode human judgement and institutional practices. A model trained on historical decisions can reproduce unfair patterns. Quality review should therefore include subgroup performance, error analysis and a process for contesting harmful outcomes. Security controls are also important because datasets may contain credentials, location information or other confidential material.
12. Contemporary Developments and Best Practices
Modern workflows increasingly use data-centric development, in which systematic improvement of labels, coverage and quality is treated as a primary engineering activity. Data versioning, automated drift detection and feature stores support operational consistency. Synthetic data and generative augmentation can increase coverage, but synthetic examples must be validated for realism, privacy risk and unwanted bias.
For responsible practice:
Define the prediction unit and deployment context.
Audit data before modelling.
Split before fitting transformations or creating duplicates.
Match the split strategy to deployment: random, grouped or temporal.
Fit preprocessing on training data only.
Apply augmentation only when label preservation is justified.
Report class-specific and operational metrics.
Version data, code, transformations and models together.
Test subgroup performance and monitor drift.
Protect privacy and document limitations.
Chapter Summary
Data preparation is a core component of deep-learning engineering. A reliable workflow begins with a clearly defined prediction task and a documented data source. Quality audits identify missingness, invalid values, duplicates, outliers and representativeness problems. Numerical features may be standardized, min–max scaled or robustly scaled, but all fitted parameters must come from training data only. Categorical, image, text and sequential data require representations appropriate to their structure.
Training, validation and test partitions serve different purposes. The partition method must reflect independence, grouping and time. Leakage can arise through preprocessing, duplication, target-derived variables, future information and repeated test-set tuning. Class imbalance requires suitable sampling, weighted losses and metrics beyond accuracy. Augmentation is useful only when transformations preserve the target meaning. Reproducible pipelines version data, transformations, code and experiments, while ethical governance addresses privacy, bias, consent, security and subgroup performance.
Key Terms
Data leakage: Use of information that should be unavailable during training or evaluation.
Data augmentation: Creation of altered training examples that preserve the original label.
Data dictionary: Documentation describing variables, types, units, meanings and valid values.
Imputation: Replacement of missing values using a defined method.
Standardization: Transformation using a feature mean and standard deviation.
Min–max scaling: Mapping a feature to a specified numerical interval.
Robust scaling: Scaling based on the median and interquartile range.
One-hot encoding: Representation of a categorical value by a binary indicator vector.
Embedding: Learned dense numerical representation of a discrete item.
Training set: Data used to fit model parameters and preprocessing parameters.
Validation set: Data used for model selection and tuning.
Test set: Held-out data used for final performance estimation.
Class imbalance: Unequal representation of target classes.
Stratified split: Partition that approximately preserves class proportions.
Grouped split: Partition that keeps related entities in one subset.
Temporal split: Partition based on time, usually training on past and testing on future data.
Reproducibility: Ability to reconstruct results from recorded data, code and settings.
Data drift: Change in data distributions or relationships after deployment.
Review Questions
Short Answer Questions
What are the different purposes of the training, validation and test sets?
Why must scaling parameters be calculated from training data only?
Give two examples of data augmentation for images and state one limitation.
What is class imbalance, and why can accuracy be misleading under imbalance?
Distinguish between one-hot encoding and an embedding.
Long Answer Questions
Explain a complete leakage-safe workflow for preparing data for a neural network.
Compare standardization, min–max scaling and robust scaling, including appropriate use cases.
Discuss how missing values, outliers and duplicates should be investigated and handled.
Explain random, stratified, grouped and temporal data partitioning and identify when each is appropriate.
Discuss ethical, privacy and governance issues that may arise during deep-learning data preparation.
Analytical / Application Questions
A fraud dataset contains 1% fraudulent transactions. Design an evaluation and training strategy that addresses imbalance without contaminating the test set.
A dataset contains ten measurements per patient, and a random split produces excellent performance. Explain why this may be unreliable and propose a revised experiment.
A forecasting model uses a feature updated after a machine failure. Identify the leakage mechanism and redesign the feature-generation and validation process.
Multiple Choice Questions with Answers
Question 1
Which dataset should be used to calculate the mean and standard deviation for standardization?
A. The complete dataset before splitting
B. The training set only
C. The validation set only
D. The test set only
Correct Answer: B. The training set only
Question 2
Which partition strategy is most appropriate when future deployment predicts later time periods?
A. Random row-wise splitting
B. Alphabetical splitting
C. Temporal splitting
D. Splitting by feature magnitude
Correct Answer: C. Temporal splitting
Question 3
What is a major risk of applying oversampling before dividing data into training and test sets?
A. The labels become continuous
B. The neural network loses all features
C. Duplicated examples may occur in both partitions
D. The test set becomes larger than the dataset
Correct Answer: C. Duplicated examples may occur in both partitions
Question 4
For a feature with training mean μ_train and standard deviation σ_train, which expression gives standardization?
A. (x + μtrain) / σtrain
B. (x − μtrain) / σtrain
C. (x − σtrain) / μtrain
D. x × μtrain × σtrain
Correct Answer: B. (x − μtrain) / σtrain
Question 5
Which statement about image augmentation is correct?
A. Every geometric transformation preserves the label
B. Augmentation should normally be applied to the test set first
C. An augmentation is suitable only when it preserves task meaning
D. Augmentation eliminates the need for representative data
Correct Answer: C. An augmentation is suitable only when it preserves task meaning
Question 6
What is the main purpose of a validation set?
A. To fit the final deployed model without training data
B. To select models and tune hyperparameters during development
C. To replace the data dictionary
D. To guarantee that the test set contains no errors
Correct Answer: B. To select models and tune hyperparameters during development
Question 7
Which field is most likely to cause target leakage in a system predicting failure seven days in advance?
A. Ambient temperature at prediction time
B. Vibration measured before prediction time
C. Repair completed after the failure
D. Pump type known before prediction time
Correct Answer: C. Repair completed after the failure
Question 8
Which metric is especially useful for evaluating detection of a rare positive class?
A. Only overall accuracy
B. Recall for the positive class
C. Number of input features
D. Training-set size alone
Correct Answer: B. Recall for the positive class
Question 9
What does one-hot encoding represent?
A. A category as a binary indicator vector
B. An image as a time series
C. A missing value as the mean of the test set
D. A continuous variable as a class-weighted loss
Correct Answer: A. A category as a binary indicator vector
Question 10
Why should preprocessing and the model be versioned together for deployment?
A. To ensure production inputs receive transformations consistent with training
B. To make all classes equally frequent automatically
C. To remove the need for testing
D. To guarantee perfect accuracy
Correct Answer: A. To ensure production inputs receive transformations consistent with training
Basics Deep Learning and Neural Networks
Chapter 6
of 14