Topics
Feature engineering is often described as the process of creating variables that help a machine-learning model predict more accurately. That description is correct but incomplete. A feature is not merely another column added to a dataset; it is a representation of information available to the model, and the way that representation is constructed changes the class of relationships the model can express. A logarithm encodes multiplicative structure, an interaction term encodes effect modification, a lag encodes temporal dependence, a rolling statistic summarizes recent history, and an embedding replaces an original object with coordinates in a learned representation space. Feature engineering is therefore part of statistical model specification rather than a preliminary data-cleaning stage.
This perspective also clarifies why feature engineering can improve a simple model more than replacing it with a more flexible algorithm. If the raw variables do not expose the structure relevant to the prediction problem, a highly nonlinear model must discover that structure indirectly from finite data. By expressing scientifically or operationally plausible transformations explicitly, one can reduce the burden on the learning algorithm, improve interpretability, and sometimes improve extrapolation. The opposite is equally true: mechanically generating thousands of transformations can inflate the search space, create leakage, increase variance, and produce apparently strong validation performance that disappears in deployment.
The central problem is therefore not how many features can be generated. It is how to construct a representation that is available at prediction time, scientifically meaningful where possible, statistically stable, and evaluated without allowing information from the validation data or future outcomes to contaminate training.
Information availability comes before transformation
For a prediction made at time $t$, every feature must be computable using information that would actually have been available by $t$. This requirement, usually described as point-in-time correctness, is one of the most important constraints in applied machine learning because it separates a valid predictor from a retrospectively constructed explanation.
Suppose a customer-churn model is intended to make a prediction at the end of each month. A feature such as the number of support calls in the previous 30 days is legitimate if the calls occurred before the prediction timestamp. The same feature becomes invalid if its aggregation window includes calls that occurred after the prediction was supposedly made. The resulting leakage can be subtle because the feature may look entirely reasonable in a static table. Its invalidity is visible only when the temporal semantics of the data are reconstructed.
The same problem appears in medicine, finance, reliability engineering, and fraud detection. A laboratory test ordered after clinical deterioration cannot be used to predict that deterioration at an earlier time. A recovery amount recorded after a loan defaults cannot be used as a predictor of default at origination. A maintenance action performed after a machine fault cannot be used to predict that fault. In each case, the dataset may contain the variable, but the deployment system could not have known it at the required decision time.
This distinction can be formalized by introducing an information set $\mathcal F_t$ containing everything observable at time $t$. A valid feature for a prediction made at time $t$ should be measurable with respect to $\mathcal F_t$. In practical terms, feature pipelines should preserve event timestamps, observation timestamps, and prediction timestamps rather than collapsing every variable into one timeless row.
Temporal leakage is only one form of information leakage. If preprocessing parameters such as means, variances, category frequencies, vocabulary, principal components, or selected variables are estimated using the full dataset before cross-validation, then information from the held-out observations enters the training procedure. The same principle applies: a transformation is valid only if it could have been estimated without seeing the validation observations.
Transformations change the statistical model
Feature transformations are often presented as data preparation, but mathematically they modify the hypothesis space available to the learner. Consider a linear regression model
This model assumes a linear conditional mean in the original coordinate $X$. If we augment the representation with $X^2$, then
which is linear in the coefficients but nonlinear in the original predictor. Adding a spline basis produces a richer smooth function; adding an interaction $XZ$ allows the effect of $X$ to depend on $Z$; using $\log X$ changes the scale on which linearity is assumed.
These transformations are not interchangeable conveniences. Each encodes a different structural assumption. A log transformation is natural when proportional changes are more meaningful than absolute changes, when the distribution is strongly right-skewed, or when the mechanism is multiplicative. A ratio such as
can be useful when the scale itself has substantive meaning, but ratios can also be unstable when the denominator approaches zero and can induce spurious relationships if numerator and denominator share measurement error. A validation gain is not sufficient justification for a derived feature if the transformation has no stable interpretation and is sensitive to small perturbations.
Interactions are particularly important because many scientific relationships are conditional. In
the partial effect of $X$ is
The interaction coefficient therefore represents effect modification, not merely an arbitrary extra term. Encoding this structure explicitly can make a simple regression model substantially more expressive while retaining a transparent interpretation.
The same reasoning applies to periodic variables. If hour of day is encoded as the integer $0,\ldots,23$, an ordinary distance treats hour 23 as far from hour 0 even though they are adjacent on the clock. A cyclic representation,
embeds the periodic coordinate on the unit circle and removes the artificial boundary. Again, the feature is useful because it represents known geometry.
Scaling, encoding, and learned preprocessing
Some transformations are required because the learning algorithm itself is sensitive to representation. Standardization,
places predictors on comparable numerical scales. This matters for Euclidean-distance methods, principal components, gradient-based optimization, and penalized models in which the size of a coefficient is directly penalized. If one predictor is measured in euros and another in proportions, a ridge or lasso penalty applied without scaling does not treat them comparably.
Scaling is not universally necessary. Tree-based partitioning methods are generally invariant to strictly monotone rescaling of individual predictors because the ordering of observations is preserved. Applying standardization merely because it is common practice can therefore add complexity without changing the fitted tree. Feature engineering should be conditional on the mathematical properties of the estimator, not a ritual applied identically to every model family.
Categorical variables create a different problem. One-hot encoding represents each category with an indicator, which is transparent but potentially expensive for high-cardinality variables. Ordinal encoding imposes an ordering that may be unjustified unless the categories are genuinely ordered. Learned embeddings can compress large categorical spaces, but the resulting geometry depends on the training objective and may be unstable when categories are sparse.
Target encoding deserves special care because it uses the response to construct a predictor. For category $c$, a naive target encoding might be
If observation $i$ contributes to the mean used to encode itself, the feature contains information from its own target. This is especially damaging for rare categories, where a category appearing once can reproduce its target exactly. Valid target encoding therefore requires an out-of-fold, leave-one-out, or otherwise leakage-controlled construction. Smoothing toward the global mean is also important because raw means for rare categories have high variance.
All learned preprocessing must obey the same resampling boundaries. Imputation parameters, category maps, scalers, vocabulary, PCA loadings, embeddings, and target encoders should be fitted on the training fold and then applied to the held-out fold. If these steps are estimated before splitting the data, cross-validation no longer estimates the performance of the actual training procedure.
Aggregation and the semantics of history
Many production models do not operate on one observation per event. They predict at the level of customers, machines, patients, loans, or accounts while the underlying data consist of many time-stamped records. Feature engineering then becomes a problem of aggregating history without violating the prediction cutoff.
For an entity $i$ and prediction time $t$, a rolling mean over a look-back window of length $w$ can be written as
where $N_i(t;w)$ is the number of observations in the interval. Similar features include counts, maxima, quantiles, volatility, time since last event, number of failures, cumulative usage, or trends estimated over recent observations.
The window definition is part of the feature. A 7-day rolling mean and a 180-day rolling mean represent different assumptions about how quickly the relevant process changes. A cumulative count since account creation encodes long-term exposure; a count over the last week encodes recency. Choosing windows by exhaustive search can produce severe multiple-comparison effects, particularly when many overlapping windows are tested against the same validation set.
Recency, frequency, and duration features can also be confounded with exposure time. A customer observed for five years has had more opportunity to accumulate events than one observed for two months. Counts may therefore need normalization by time at risk, and the definition of “history” should be aligned with the scientific process rather than treated as a purely computational aggregation.
The treatment of missingness can itself be informative. In many operational systems, a missing value does not mean that the underlying quantity is absent; it means that a measurement was not made. Measurement intensity may depend on risk. Adding an indicator for missingness can improve prediction, but it may also cause the model to learn workflow behavior rather than the phenomenon of interest. Whether that is acceptable depends on the deployment objective and whether the workflow is expected to remain stable.
Feature selection is part of model fitting
Feature engineering and feature selection are related but distinct. Engineering constructs a representation; selection decides which components of that representation to retain. The distinction matters because selection is itself a data-dependent learning step and must be validated accordingly.
Filter methods rank variables using quantities such as correlation, mutual information, univariate tests, or variance. Embedded methods such as lasso, elastic net, and tree-based procedures incorporate some form of variable selection into model fitting. Wrapper methods repeatedly fit models to alternative subsets. Each approach has different statistical properties and computational costs, but all can overfit if selection is performed on the full dataset before evaluation.
Suppose $p$ candidate features are screened against the target and only the strongest are passed to a classifier. If the screening uses all observations, then the validation fold has influenced which variables were selected. The resulting validation score is optimistic even if the classifier itself never sees the held-out labels directly. The correct procedure repeats feature selection separately inside every training fold.
Selection also becomes unstable when predictors are strongly correlated. Two variables may carry almost identical information, so small perturbations in the sample can determine which one is retained. Interpreting the selected variable as uniquely important can then be misleading. Stability analysis, bootstrap inclusion frequencies, grouped penalties, or domain-based constraints can help distinguish predictive redundancy from scientific importance.
Dimensionality reduction belongs to another category. Principal component analysis constructs orthogonal linear combinations that explain variance in $X$ without reference to $Y$. It can reduce dimension and collinearity, but the principal directions need not be predictive. Supervised dimensionality-reduction methods use the target and therefore require the same leakage controls as other learned transformations. Methods such as t-SNE and UMAP are primarily designed for nonlinear representation and visualization; using their low-dimensional coordinates as generic production features requires considerably more justification than simply observing visually separated clusters.
Automated feature generation changes the search problem
Automated feature engineering can be useful when the data have clear relational or temporal structure. Systems can generate aggregations across linked tables, polynomial combinations, transformations, or candidate interaction terms faster than a human analyst could enumerate them manually. The statistical danger is that automation dramatically enlarges the hypothesis space.
If thousands of candidate features are generated and repeatedly evaluated on the same validation set, some will appear useful by chance. The resulting process is a form of adaptive multiple testing. Cross-validation helps, but repeated human or algorithmic tuning against the same folds can still overfit the validation procedure itself. A final untouched test set, nested cross-validation, temporal holdout, or external validation may therefore be necessary when feature search is extensive.
Semantic validation remains essential. An automated system cannot infer from column names alone that a variable was recorded after the outcome, that a status code is created only when an investigation begins, or that a relational join duplicates future events. The more automatically generated the feature space becomes, the more important lineage and timestamp semantics become.
This is why “automated feature engineering” should not be interpreted as replacing domain knowledge. Its useful role is to search a constrained set of transformations whose semantics and availability have already been defined. Automation can explore; it cannot decide which information would legitimately exist at prediction time.
Training-serving consistency and feature governance
A feature that performs well offline is useless if it cannot be reproduced reliably during inference. Training-serving skew occurs when the feature computed during model development differs from the feature computed in production. Differences in SQL logic, timezone handling, missing-value rules, category definitions, unit conversion, or aggregation cutoffs can all create silent discrepancies.
A feature store or shared feature-definition layer can reduce this risk by reusing the same transformation logic for historical training data and online or batch inference. The architectural benefit is reproducibility and lineage, not statistical correctness by itself. A perfectly consistent feature store can consistently compute a leaking or scientifically meaningless feature.
Useful feature metadata therefore include the source tables, owner, definition, transformation version, timestamp semantics, expected range, update frequency, allowed latency, missingness behavior, and the prediction contexts in which the feature is valid. For temporal aggregations, the cutoff rule should be explicit. For externally supplied variables, the publication delay should also be recorded because a macroeconomic figure observed today may describe last month while not having been available at the historical prediction timestamp.
Feature governance becomes particularly important when many models reuse the same variables. A change in one upstream definition can affect dozens of deployed systems simultaneously. Versioning and backward-compatible recomputation are then part of model risk management.
Predictive features are not automatically causal variables
Feature engineering is usually optimized for prediction, but predictive usefulness and causal interpretability should not be conflated. A variable can be an excellent predictor because it is a proxy for an unobserved cause, a consequence of the outcome process, or a collider created by selection. Such a variable may improve forecasting while being inappropriate in a causal adjustment set.
Suppose the objective is to estimate the effect of treatment $A$ on outcome $Y$. A variable measured after treatment may predict $Y$ strongly, but adjusting for it can block part of the causal effect or introduce bias. Likewise, automated selection can remove a weakly predictive confounder even though that variable is essential for causal identification.
For predictive modeling, the central question is whether the feature is available, stable, and useful under the deployment distribution. For causal modeling, the central question is whether conditioning on the variable preserves identification of the estimand. The same engineered dataset should not automatically be reused for both purposes.
Validation should reproduce the intended deployment process
The final quality of a feature set cannot be assessed independently of the validation design. Random cross-validation assumes that training and validation observations are exchangeable. That assumption fails when multiple rows come from the same patient or customer, when observations overlap through rolling windows, when the model will be deployed on future calendar periods, or when sites differ systematically.
If the deployment task is forecasting future behavior, temporal validation is often more realistic:
If predictions will be made for unseen customers, splits should be grouped by customer. If the same machine contributes many time windows, those windows should not be divided randomly between training and validation. Otherwise, entity-specific patterns can leak across folds and exaggerate generalization.
Every data-dependent feature transformation belongs inside this validation design. The complete procedure is
and the entire sequence must be repeated separately inside each training fold. Validation data should pass only through transformations already fitted on the corresponding training subset.
This is the reason pipeline abstractions are valuable. Their main benefit is not software elegance but statistical discipline: they make it harder to accidentally fit preprocessing on data that are supposed to remain unseen.
Conclusion
Feature engineering is best understood as the design of a representation under information, statistical, and deployment constraints. A useful feature exposes structure that the model can exploit, but it does so without relying on information unavailable at prediction time and without contaminating validation. The quality of a feature therefore depends not only on its association with the target, but also on its temporal semantics, stability, reproducibility, and relationship to the intended deployment process.
This viewpoint changes the usual workflow. Instead of beginning with a catalogue of possible transformations, one begins with the prediction time, the available information set, and the structure of the data-generating process. Transformations are then introduced because they express plausible geometry, periodicity, nonlinearity, interactions, or history. Selection and learned preprocessing are treated as part of model fitting, aggregations are tied to explicit cutoffs, and validation reproduces the grouping and temporal structure expected after deployment.
The strongest feature-engineering practice is therefore not the one that creates the largest feature matrix. It is the one that makes every variable defensible. For each feature, an analyst should be able to answer where it came from, when it became available, what transformation produced it, which assumptions it encodes, how it is recomputed in production, and whether its apparent predictive value survives a validation design that mirrors real use. Once those questions are answered carefully, feature engineering becomes what it should be: a principled form of statistical representation design rather than an automated search for correlations.
References
- Kuhn, M., & Johnson, K. (2019). Feature Engineering and Selection: A Practical Approach for Predictive Models. CRC Press.
- Hastie, T., Tibshirani, R., & Friedman, J. (2009). The Elements of Statistical Learning (2nd ed.). Springer.
- Zheng, A., & Casari, A. (2018). Feature Engineering for Machine Learning. O'Reilly Media.
Embed interactive plots, widgets, and demos using <figure>, <iframe>, or <div class="interactive-embed"> containers. Ensure each embed includes descriptive captions for accessibility.
How to cite
Use the quick export buttons to save citations for reference managers or copy the formatted text directly.
Diogo Ribeiro (2024). Feature Engineering: Representation, Leakage, and Validation. Faculty of Media Arts and Design, Technical University of Porto. https://diogoribeiro7.github.io/mathematics/Feature_Engineering/.


