Data Preparation, Feature Engineering and Tensor PipelinesIntroductionMachine-learning systems learn from representations of data rather than from raw information alone. A model may be mathematically sophisticated, yet perform poorly when its inputs contain inconsistent units, missing values, duplicated records, target leakage, sampling bias, or an unsuitable representation. Data preparation is therefore not a preliminary clerical activity; it is part of model design and a major determinant of validity, fairness, reproducibility, and operational performance. This chapter examines the complete pathway from raw structured or unstructured data to training-ready tensors. It explains how to inspect and clean data, engineer and encode features, scale numerical variables, divide data into reliable subsets, address imbalance and leakage, and construct reproducible input pipelines using TensorFlow and PyTorch. The emphasis is on sound reasoning: every transformation should be justified by the data-generating problem and fitted without allowing evaluation information to influence training. Learning ObjectivesBy the end of this chapter, students should be able to: 1. Describe the stages of a robust data-preparation workflow. 2. Identify errors, missingness, outliers, imbalance, bias, and leakage. 3. Select appropriate transformations for numerical, categorical, temporal, textual, and image data. 4. Explain and apply encoding and scaling methods. 5. Design train, validation, and test splits that reflect the intended deployment setting. 6. Construct reproducible TensorFlow `tf.data` and PyTorch `DataLoader` pipelines. 7. Evaluate the practical, ethical, and governance consequences of preparation decisions. 1. Data as a Learning RepresentationA dataset is commonly represented as observations and variables. For supervised learning, an observation may be written as \[ (\mathbf{x}_i, y_i), \quad i=1,\ldots,n, \] where \(\mathbf{x}_i\) is the vector of input features for observation \(i\), \(y_i\) is its target, and \(n\) is the number of observations. This notation assumes that observations can be meaningfully indexed and that the chosen target is available during training. In production, the model will receive \(\mathbf{x}\) without observing \(y\), so preparation must reproduce the information available at prediction time. A useful preparation workflow consists of the following stages: 1. Define the prediction task and unit of analysis. Decide what one row represents, what is being predicted, and when prediction occurs. 2. Acquire and audit data. Record sources, schemas, formats, permissions, timestamps, and data lineage. 3. Profile quality. Examine types, ranges, duplicates, missingness, class frequencies, and relationships between fields. 4. Clean and transform. Correct errors, resolve inconsistent units, handle missing values, and remove or justify anomalous records. 5. Engineer and select features. Create representations that are available at inference and relevant to the task. 6. Split data. Create independent training, validation, and test sets using a design appropriate to time, groups, and classes. 7. Fit preprocessing on training data. Apply the learned transformations to validation, test, and production data. 8. Convert to tensors and batch. Construct efficient, deterministic, monitored input pipelines. The distinction between a parameter and a hyperparameter is important. A parameter is learned from training data, such as a mean used for standardization. A hyperparameter is chosen by the practitioner, such as batch size or the number of hashing buckets. Both can cause leakage if selected using the test set. 2. Data Inspection and Quality Assessment2.1 Schema and semantic checksA schema describes field names, data types, allowable values, units, and relationships. A column labelled `age` should not contain negative values; a temperature field must specify Celsius or Fahrenheit; an identifier should not automatically be treated as a predictive numerical feature. Semantic validation is stronger than merely checking whether a value can be parsed. Typical checks include: - expected columns and data types;
- permitted categories and code meanings;
- minimum and maximum values;
- uniqueness constraints for identifiers;
- referential integrity between tables;
- timestamp ordering and time zones;
- duplicate and near-duplicate records;
- whether each feature would be available at prediction time.
A data dictionary should document definitions, units, collection method, missing-value codes, ownership, and sensitivity. Values such as `-1`, `999`, or `unknown` may be legitimate codes or disguised missing values; they must not be treated automatically as ordinary measurements. 2.2 Missing dataMissingness can arise because a sensor failed, a question was not applicable, a value was withheld, or a process selectively recorded information. The pattern itself may contain information. Three useful conceptual categories are: - MCAR (missing completely at random): missingness is unrelated to observed and unobserved values.
- MAR (missing at random): missingness may depend on observed variables.
- MNAR (missing not at random): missingness depends on the unobserved value or another unobserved process.
These categories are assumptions about the data-generating mechanism, not labels that can always be proven from the dataset. In practice, inspect missingness by feature, group, time period, and target class. Add a missingness indicator when the fact that a value was absent may be predictive, but consider whether it encodes a sensitive operational practice. For numerical data, common approaches include median imputation, model-based imputation, and domain-specific replacement. For categorical data, a separate `Unknown` category is often preferable to silently using the mode. Imputation statistics must be calculated on the training set only. If the training median for a feature is 42, that value can be applied to validation and test records; their medians must not be used to modify the training process. Imputation does not recover truth. It introduces assumptions and can reduce variability. A missingness audit and sensitivity analysis are therefore preferable to treating imputation as a neutral operation. 2.3 Outliers and inconsistent observationsAn outlier is an observation unusual relative to a selected reference distribution. It may be a measurement error, a rare but valid case, or evidence that the population contains multiple subgroups. Removing all outliers can eliminate precisely the cases that matter in fraud, safety, or medical diagnosis. Investigate outliers using domain limits, robust statistics, visualizations, and source records. Possible responses include correction, transformation, winsorization, robust scaling, separate modelling, or retention. The choice should be documented. A z-score rule based on mean and standard deviation is unreliable for highly skewed data and does not establish that a value is erroneous. 3. Feature Engineering and RepresentationFeature engineering converts raw fields into variables that make relevant structure accessible to a model. It can improve accuracy, reduce computational requirements, increase interpretability, and encode domain knowledge. It can also introduce leakage, unfair proxies, and instability. 3.1 Numerical featuresNumerical variables may be transformed to address skew, meaningful scale, or nonlinear relationships. For a positive variable \(x\), a logarithmic transformation such as \(x' = \log(1+x)\) is defined for \(x\geq0\) and compresses large values. It is not appropriate without modification for negative values. Other transformations include square roots, clipping based on training-set limits, ratios, rates, and differences. Feature construction must respect units and denominators. For example, a rate such as defects per 1,000 units is meaningful only when the denominator is measured consistently and is not zero. Aggregates such as “number of purchases in the previous 30 days” require a time boundary that excludes events occurring after the prediction time. 3.2 Categorical featuresCategorical variables represent labels rather than quantities. Treating `small`, `medium`, and `large` as 1, 2, and 3 imposes equal spacing unless that ordering is substantively justified. One-hot encoding represents a category with a binary vector. For a variable with \(k\) categories, one-hot encoding produces \(k\) indicators when an intercept is not used, or commonly \(k-1\) indicators in a linear model with an intercept to avoid exact linear dependence. Neural networks can use all \(k\) indicators without the same regression-identifiability concern. Ordinal encoding is suitable when categories have a defensible order, but numeric distances should not automatically be interpreted as meaningful. Embeddings map categories to learned dense vectors. They are useful when there are many categories and sufficient training data, but rare categories require careful handling. Unseen categories should map to a reserved unknown index rather than causing an inference failure. Hashing maps tokens or categories into a fixed number of buckets. It avoids storing a complete vocabulary but can produce collisions, in which distinct categories share a representation. The number of buckets is a design choice and should be evaluated empirically. 3.3 Dates, time, and sequencesDates should rarely be supplied as arbitrary integers. Useful features may include year, month, day of week, elapsed time, holiday status, or time since an event. Cyclic variables can be represented using sine and cosine. For a period \(P\) and value \(t\), \[ x_{\sin}=\sin(2\pi t/P), \qquad x_{\cos}=\cos(2\pi t/P). \] These features place the beginning and end of a cycle near one another. The period must be known or defensibly selected; a monthly cycle is not the same as a yearly cycle. Time-dependent tasks require chronological reasoning. Randomly mixing future observations into training can create unrealistic performance estimates. Lagged features must be computed using only information available before the prediction timestamp. Rolling statistics must exclude the current target event where appropriate. 3.4 Text and imagesRaw text is converted into tokens, character sequences, or subword units. A vocabulary is built from training data, with reserved tokens for padding and unknown items. Tokenization, truncation length, and padding policy affect both information retention and computational cost. Text normalization should not remove meaningful distinctions, such as negation or technical symbols. Images are commonly represented as tensors with height, width, and channels. Preparation may include resizing, normalization, channel conversion, and augmentation. Augmentations should preserve the label. Horizontal flipping, for example, may be inappropriate when left-right orientation determines the class. Augmentation must be applied to training data only unless the evaluation protocol explicitly defines otherwise. 4. Scaling and Numerical ConditioningScaling is particularly important for distance-based methods, gradient optimization, and regularized models. It is less essential for many tree-based methods, although consistent preprocessing can still simplify pipelines. Standardization transforms a feature using \[ z = \frac{x-\mu}{\sigma}, \] where \(\mu\) is the training-set mean and \(\sigma\) is the training-set standard deviation. It assumes \(\sigma>0\). A constant feature has zero variance and must be removed or handled separately. Standardization produces mean zero and unit variance on the data used to estimate \(\mu\) and \(\sigma\), but it does not guarantee a normal distribution or remove outliers. Min–max scaling maps a feature to a selected interval, commonly [0,1]: \[ x' = \frac{x-x_{\min}}{x_{\max}-x_{\min}}. \] This requires \(x_{\max}>x_{\min}\) and is sensitive to extreme values. Values outside the training range may map below 0 or above 1 unless clipping is explicitly applied. Robust scaling uses statistics such as the median and interquartile range and is less affected by extreme observations. Scaling choices should be recorded as part of the model artifact, because the trained model and its transformation parameters form one deployable system. Worked Example: Training-only standardizationSuppose the training values for a feature are 10, 12, and 14. Their mean is \[ \mu=(10+12+14)/3=12. \] Using the population standard deviation for this preprocessing illustration, \[ \sigma=\sqrt{[(10-12)^2+(12-12)^2+(14-12)^2]/3} =\sqrt{8/3}\approx1.633. \] A validation value of 16 is transformed using the training statistics: \[ z=(16-12)/1.633\approx2.45. \] The validation value must not be included when recalculating the mean or standard deviation. In statistical estimation, a sample standard deviation with denominator \(n-1\) may instead be used; whichever convention a library uses must be applied consistently. 5. Splitting Data and Preventing LeakageA training set is used to fit parameters, a validation set supports model and hyperparameter decisions, and a test set provides a final estimate after those decisions are complete. Repeatedly inspecting test performance turns the test set into a second validation set. For independent, similarly distributed observations, a random split may be reasonable. For classification, stratification can preserve approximate class proportions. For grouped observations, all records belonging to one person, household, patient, or device should generally remain in one split. Otherwise, the model may recognize the entity rather than learn a generalizable pattern. For temporal data, use chronological splits or rolling evaluation. The test period should represent the future deployment period. A random split can allow future information to influence training and overstate performance. Target leakage occurs when a feature contains information unavailable at prediction time or is derived from the target. Examples include a diagnosis code entered after a clinical outcome, a “refund issued” field used to predict whether a transaction will be refunded, or normalization statistics computed across train and test data. Leakage can produce exceptionally high validation scores and poor deployment performance. A practical leakage audit asks: - When exactly is each feature created?
- Who or what creates it?
- Could its value change after the prediction time?
- Was any preprocessing fitted using evaluation records?
- Are duplicate entities present across splits?
- Were labels or post-outcome fields used indirectly?
6. Imbalance, Bias, and Evaluation DesignClass imbalance occurs when one class is much more frequent than another. Accuracy can then be misleading. If 98% of cases are negative, a classifier that always predicts negative achieves 98% accuracy but has no ability to identify positives. Responses include stratified sampling, class-weighted losses, carefully designed resampling, threshold selection, and suitable metrics. Oversampling must be performed within the training data; duplicating minority examples before splitting can place copies in both training and test sets. Synthetic methods must also be applied only to training data and validated for plausibility. For binary classification, a confusion matrix contains true positives (TP), true negatives (TN), false positives (FP), and false negatives (FN). Precision and recall are \[ \text{Precision}=\frac{TP}{TP+FP}, \qquad \text{Recall}=\frac{TP}{TP+FN}, \] when the denominators are nonzero. Precision asks how many predicted positives are correct; recall asks how many actual positives are detected. The F1 score is the harmonic mean, \[ F_1=\frac{2PR}{P+R}, \] where \(P\) and \(R\) denote precision and recall and both are positive. Metric selection should reflect the cost of errors, not a desire to report the most favourable value. Bias can enter through under-representation, measurement differences, historical decisions, proxy variables, or label definitions. Removing a protected attribute does not necessarily remove bias: postcode, language, or purchasing history may act as proxies. Assess performance across relevant groups, document limitations, and involve domain and affected-community expertise. Fairness metrics can conflict, particularly when base rates differ, so a single numerical criterion cannot settle every policy question. Privacy and governance also matter. Data minimization, access control, retention limits, consent or another lawful basis, de-identification where appropriate, and audit trails should be built into the pipeline. Sensitive data should not be copied into notebooks or logs unnecessarily. 7. Reproducible Tensor PipelinesA tensor pipeline should make data transformations explicit, repeatable, testable, and efficient. Reproducibility includes fixed random seeds where feasible, versioned raw data or immutable snapshots, recorded preprocessing parameters, fixed vocabulary and label mappings, environment specifications, and documented split assignments. Seeds do not guarantee identical results across all hardware and software versions, particularly with parallel operations. 7.1 TensorFlow `tf.data`TensorFlow’s `tf.data.Dataset` represents a sequence of elements and supports mapping, shuffling, batching, caching, and prefetching. A typical supervised pipeline is: ```python import tensorflow as tf train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train)) train_ds = train_ds.shuffle( buffer_size=len(X_train), seed=42, reshuffle_each_iteration=True ) train_ds = train_ds.batch(32).prefetch(tf.data.AUTOTUNE) valid_ds = ( tf.data.Dataset.from_tensor_slices((X_valid, y_valid)) .batch(32) .prefetch(tf.data.AUTOTUNE) ) ``` Training data is normally shuffled; validation and test data generally are not required to be shuffled. `batch` groups examples, while `prefetch` prepares future batches while the model processes the current batch. `cache` can improve speed when the transformed dataset fits in memory, but caching must not accidentally preserve a training-only or stale state. For large files, `from_tensor_slices` may be unsuitable because it assumes data is already available in memory; file-based datasets or generators may be preferable. TensorFlow preprocessing layers such as `Normalization`, `StringLookup`, `IntegerLookup`, and `TextVectorization` can be adapted on training data and included in a model. This reduces the chance that training and serving use different transformations: ```python normalizer = tf.keras.layers.Normalization(axis=-1) normalizer.adapt(X_train) # training data only inputs = tf.keras.Input(shape=(X_train.shape[1],)) x = normalizer(inputs) ``` The adaptation operation estimates preprocessing parameters; it must not be performed on validation or test data. 7.2 PyTorch `Dataset` and `DataLoader`In PyTorch, a `Dataset` defines how an indexed example is retrieved, and a `DataLoader` handles batching and iteration: ```python from torch.utils.data import TensorDataset, DataLoader import torch train_dataset = TensorDataset( torch.as_tensor(X_train, dtype=torch.float32), torch.as_tensor(y_train, dtype=torch.long) ) train_loader = DataLoader( train_dataset, batch_size=32, shuffle=True, num_workers=0, pin_memory=True ) ``` The target dtype must match the loss function. For example, class indices used with `CrossEntropyLoss` are ordinarily integer class labels, while regression targets are commonly floating point tensors. A custom dataset is appropriate when records must be read from disk, decoded, augmented, or tokenized on demand. Weighted sampling can address training imbalance, but it should be used deliberately. A `WeightedRandomSampler` changes the observed training distribution; evaluation loaders should retain the natural evaluation distribution unless the evaluation question explicitly concerns another distribution. If class weighting is also applied in the loss, the correction may be excessive. PyTorch reproducibility requires attention to seeds for Python, NumPy, PyTorch, workers, and potentially CUDA operations. Multiple workers improve throughput but can complicate deterministic ordering and random augmentation. Record the configuration rather than assuming that a seed alone is sufficient. 8. Illustrative Case: Predicting Customer ChurnThe following is a fictional illustrative case. A subscription company wants to predict whether a customer will cancel during the next 30 days, using information available at the end of each day. Background and problemThe raw warehouse contains customer identifiers, plan type, monthly charge, support contacts, payment history, cancellation date, and event logs. The initial dataset has repeated daily records, missing income values, and a field called `retention_offer_accepted`. AnalysisThe unit of analysis is defined as one customer-day. The label is cancellation within the following 30 days. The retention-offer field is rejected because the offer may be made after a risk assessment and is not available for all customers at the prediction time. This is potential target leakage or a treatment variable. Customer records must not be randomly distributed across train and test if the model is evaluated on future customers or future dates; a chronological split is selected. Exploration reveals that support contacts are highly right-skewed, plan type is categorical, and payment failures are more common among customers with missing billing information. Missingness indicators are retained after a governance review because they describe the company’s recording process, but their use is documented as a possible proxy for service access. Approach and solutionThe company creates lagged counts of support contacts and payment failures using only events before the prediction time. Monthly charge is standardized using training-period statistics. Plan type is one-hot encoded with an unknown category. The data is divided into earlier training dates, a subsequent validation period, and a final future test period. Class weights are estimated from the training set because cancellation is relatively infrequent. The pipeline validates schemas, stores the feature-generation code in version control, records the split boundaries, and exports the preprocessing parameters with the model. Evaluation reports recall and precision at an operational threshold, rather than accuracy alone, and compares results across customer regions. OutcomeThe first model showed an implausibly high validation score because the leakage field was included. After removal, performance decreased but remained useful and was more stable on the future test period. Regional analysis identified lower recall for customers whose support interactions were recorded through a separate legacy system. The company therefore treats the model as decision support, investigates data-quality differences, and avoids automatically denying service or imposing penalties based on the prediction. Learning pointsThe case demonstrates that a lower score can represent a more trustworthy model, that time boundaries are part of feature design, and that fairness review may reveal data-collection problems rather than merely model defects. It also shows why a pipeline must include governance decisions, not only tensor conversion. 9. Best Practices and LimitationsA professional pipeline should: - define the prediction event, target, and information cutoff;
- maintain a data dictionary and lineage record;
- separate fitting from transforming;
- fit vocabularies, imputers, scalers, and encoders on training data only;
- preserve unknown and missing categories explicitly;
- use group-aware or temporal splits when required;
- test for duplicates and leakage;
- report performance by class and relevant subgroup;
- version data, code, environments, and preprocessing artifacts;
- monitor drift, missingness, category growth, and production failures;
- make transformations consistent between training and inference.
No preparation strategy is universally best. Imputation can distort distributions, scaling can be harmful when applied incorrectly, augmentation can change labels, embeddings can memorize rare entities, and resampling can alter probability calibration. Data drift may invalidate training statistics after deployment. Preparation is therefore an iterative engineering and scientific process requiring monitoring and re-evaluation. Chapter SummaryData preparation determines how a learning system represents reality. A reliable workflow begins by defining the unit of analysis, target, prediction time, and data schema. Quality assessment must address types, units, duplicates, missingness, outliers, and inconsistent records. Feature engineering can use transformations, categorical encodings, temporal features, text tokenization, image preprocessing, and domain aggregates, but every feature must be available at inference time. Scaling methods such as standardization and min–max scaling require parameters learned from training data only. Train, validation, and test splits must reflect independence, groups, or time, and target leakage must be actively investigated. Imbalance requires appropriate sampling, weighting, thresholds, and metrics. Bias and privacy risks arise from collection processes and proxy variables as well as from algorithms. TensorFlow `tf.data` and PyTorch `DataLoader` provide composable mechanisms for batching, shuffling, preprocessing, and efficient delivery. Reproducibility requires versioned data and code, recorded transformations, controlled randomness, and consistent deployment artifacts. The central professional principle is that preprocessing is part of the model and must be designed, tested, governed, and monitored accordingly. Key TermsData leakage: Use of information unavailable at prediction time or improperly shared across evaluation boundaries. Data lineage: A record of where data originated, how it was transformed, and how it moved through a system. Feature engineering: Construction or transformation of input variables to represent useful predictive structure. Imputation: Replacement or modelling of missing values. MCAR: Missing completely at random; missingness is unrelated to observed and unobserved values. MAR: Missing at random conditional on observed information. MNAR: Missing not at random; missingness depends on unobserved information or its process. One-hot encoding: Representation of categories using binary indicator variables. Embedding: A learned dense vector representation of a discrete item. Standardization: Transformation using a training mean and standard deviation. Min–max scaling: Rescaling based on training minimum and maximum values. Stratification: Splitting or sampling to preserve approximate class proportions. Group-aware split: A split that keeps related entities in the same subset. Class imbalance: Unequal frequency of target classes. Precision: The proportion of predicted positives that are true positives. Recall: The proportion of actual positives detected by the model. Tensor: A multidimensional numerical array used by machine-learning frameworks. Batch: A group of examples processed together. Data drift: Change in input distributions or relationships after deployment. DataLoader: A PyTorch utility for iterating over datasets in batches. Review QuestionsShort Answer Questions1. Why must preprocessing statistics such as means and medians be fitted on the training set only? 2. Distinguish one-hot encoding from ordinal encoding. 3. What is target leakage? Give one example. 4. Why can accuracy be misleading for an imbalanced classification problem? 5. State two purposes of batching and prefetching in a tensor pipeline. Long Answer Questions1. Explain a complete data-preparation workflow from schema inspection to deployment, including quality and governance checks. 2. Compare standardization, min–max scaling, and robust scaling, including their assumptions and limitations. 3. Discuss how temporal, grouped, and random splitting differ and when each is appropriate. 4. Explain how missingness, outliers, and categorical variables can be handled in a production pipeline. 5. Compare TensorFlow `tf.data` with PyTorch `Dataset` and `DataLoader` in terms of composition, preprocessing, batching, and reproducibility. Analytical / Application Questions1. A fraud dataset contains 1% confirmed fraud cases. Design an evaluation and training strategy that addresses imbalance without contaminating the test set. 2. A hospital model uses a feature recorded after discharge and reports near-perfect validation performance. Analyze the likely problem and propose a corrected experimental design. 3. Design features and a split strategy for predicting daily equipment failure using sensor readings and maintenance logs. Explain how you would prevent future information from entering the features. Multiple Choice Questions with AnswersQuestion 1Which statistic should be used to standardize a validation example? A. The validation-set mean and standard deviation B. The test-set mean and standard deviation C. The training-set mean and standard deviation D. The global mean and standard deviation from all records Correct Answer: C. The training-set mean and standard deviation Question 2Which situation is an example of target leakage? A. Encoding plan type as one-hot indicators B. Using a post-discharge field to predict a discharge outcome C. Removing a constant feature D. Batching examples before training Correct Answer: B. Using a post-discharge field to predict a discharge outcome Question 3Which split is generally most appropriate for evaluating a model intended to predict future events? A. A chronological split B. A split based only on row order after random sorting C. A split that duplicates records across subsets D. A test set selected using the target outcome Correct Answer: A. A chronological split Question 4What does recall measure in binary classification? A. The proportion of predicted positives that are correct B. The proportion of actual positives that are detected C. The proportion of all predictions that are correct D. The proportion of actual negatives that are detected Correct Answer: B. The proportion of actual positives that are detected Question 5Which encoding is most suitable when categories have a defensible order? A. Ordinal encoding B. Random floating-point encoding C. Timestamp encoding D. Image normalization Correct Answer: A. Ordinal encoding Question 6What is a principal risk of fitting a min–max scaler on the complete dataset before splitting? A. It necessarily removes all outliers B. It prevents batching C. It allows evaluation data to influence preprocessing parameters D. It converts categorical variables into images Correct Answer: C. It allows evaluation data to influence preprocessing parameters Question 7In a TensorFlow input pipeline, what does prefetch primarily support? A. Deleting missing values B. Preparing future batches while the current batch is processed C. Encoding labels as categories D. Calculating the test-set target Correct Answer: B. Preparing future batches while the current batch is processed Question 8Which PyTorch component is primarily responsible for batching and iteration over a dataset? A. DataLoader B. Tensor scalar C. Optimizer D. Activation function Correct Answer: A. DataLoader Question 9Why can a model with 98% accuracy still be ineffective on a dataset with 2% positive cases? A. Accuracy is always undefined for classification B. It may always predict the majority negative class C. The positive class must contain 98% of examples D. Accuracy measures only computational speed Correct Answer: B. It may always predict the majority negative class Question 10For a cyclic variable with period P, which representation is described in the chapter? A. x/P and x-P B. log(x) and sqrt(x) only C. sin(2πx/P) and cos(2πx/P) D. x multiplied by the target Correct Answer: C. sin(2πx/P) and cos(2πx/P)
|