Feature Engineering, Model Evaluation and ExplainabilityIntroductionA predictive model is only as useful as the data representation, evaluation design and interpretation that support it. In practical data science, model development is not simply a matter of selecting an algorithm and minimizing an error function. Analysts must decide which information should be represented, how observations should be divided, which performance measure reflects the real decision, whether the evaluation is contaminated by information leakage, and how results can be explained to technical and non-technical stakeholders. Feature engineering converts raw observations into variables that algorithms can use effectively. Model evaluation estimates how well a model will perform on previously unseen data. Explainability methods help users understand why a model produced a prediction and whether its reasoning is sensible, stable and fair. These activities are closely connected: a poorly engineered feature can create leakage, an inappropriate metric can reward harmful behaviour, and an apparently accurate model may rely on unjustifiable proxies. This chapter develops a practical framework for feature engineering, model selection, evaluation and explainability, with emphasis on reproducible professional practice. Learning ObjectivesBy the end of this chapter, students should be able to: - Describe the purpose and risks of feature engineering.
- Construct numerical, categorical, temporal, text and interaction features appropriately.
- Select train-validation-test and cross-validation strategies for different data structures.
- Calculate and interpret major regression and classification metrics.
- Diagnose overfitting, underfitting, leakage and class imbalance.
- Select suitable explainability techniques and distinguish global from local explanations.
- Design an evaluation workflow that is technically valid, ethically responsible and reproducible.
1. Feature Engineering as Representation DesignA feature is a measurable input variable used by a model. In a dataset, a row usually represents an observation and a column represents a feature, although this arrangement is not conceptually required. Feature engineering is the process of transforming raw data into representations that preserve useful signal while reducing noise, ambiguity and computational difficulty. A useful feature should be: - relevant to the prediction target;
- available at the time a prediction is made;
- measured consistently;
- sufficiently complete and reliable;
- legally and ethically permissible to use; and
- represented in a form compatible with the chosen algorithm.
Feature engineering is not the same as indiscriminately creating many variables. Additional features can increase dimensionality, amplify noise and make a model more difficult to govern. The central question is whether a feature improves out-of-sample decision quality without violating the information boundary of the prediction task. 1.1 Numerical FeaturesNumerical variables may be continuous, such as temperature, or discrete, such as number of transactions. Common transformations include: - Scaling: Standardization transforms a value using z = (x - μ) / σ, where x is the observed value, μ is the training-set mean and σ is the training-set standard deviation. It produces a dimensionless variable with mean approximately zero and standard deviation approximately one when σ > 0. The mean and standard deviation must be calculated on the training data only.
- Min–max transformation: x' = (x - xmin) / (xmax - xmin). This usually maps training values to [0, 1], provided xmax ≠ xmin. It is sensitive to extreme values.
- Logarithmic transformation: x' = log(1 + x) is useful for non-negative, right-skewed quantities such as income or transaction counts. The addition of 1 permits zero values; negative values require a different transformation or careful domain treatment.
- Binning: A continuous variable is divided into intervals. Binning can improve interpretability but discards within-bin variation and may create artificial discontinuities.
- Ratios and rates: For example, claims per policy or revenue per customer can express intensity. The denominator must be meaningful and non-zero, and the time period of numerator and denominator must be aligned.
Scaling is particularly important for distance-based methods, nearest-neighbour models, support vector machines and regularized linear models. Tree-based models generally do not require scaling because their splits depend on order thresholds rather than Euclidean distance. 1.2 Categorical FeaturesCategorical variables contain labels such as region, product type or payment method. - One-hot encoding creates one binary feature per category. With an intercept in a linear model, one category is commonly omitted as a reference category to avoid perfect linear dependence. For an unseen category at deployment, an explicit unknown-category policy is required.
- Ordinal encoding maps categories to integers. It is appropriate only when the ordering is meaningful, such as low, medium and high. Encoding colours as 1, 2 and 3 would falsely imply order and equal spacing.
- Frequency encoding replaces a category with its observed frequency. It is compact but may lose category identity and must be computed without using information from the evaluation data.
- Target encoding replaces a category with an estimate of its target mean. It can be powerful for high-cardinality variables but is highly vulnerable to leakage. The estimate must be calculated within training folds, commonly with smoothing and out-of-fold procedures.
Rare categories may be grouped into an “other” category, but the threshold should be established using training data and documented because it can affect minority groups disproportionately. 1.3 Dates, Time and SequencesDates should rarely be used as raw strings. Useful representations include year, month, day of week, holiday indicator, elapsed time since an event and time since the previous transaction. Cyclical variables such as hour of day can be represented using sine and cosine: xsin = sin(2πt / P), xcos = cos(2πt / P) Here, t is the time position and P is the period, such as 24 hours or 7 days. The two variables preserve the fact that the end and beginning of a cycle are close. For forecasting, lag and rolling-window features are useful. A lag feature uses only values known before the prediction time. A rolling mean over the previous k observations can be written as: mt = (1/k) ∑i=1k yt-i where yt-i is the observed value at an earlier time and k is the window length. Including yt in its own predictor would constitute leakage. 1.4 Text and High-Dimensional DataText can be represented using term-frequency vectors, n-grams, hashing, word embeddings or document embeddings. A common weighting is term frequency–inverse document frequency (TF–IDF): tfidf(t,d) = tf(t,d) × log(N / df(t)) Here, tf(t,d) measures the frequency of term t in document d, N is the number of documents and df(t) is the number of documents containing the term. Variants use smoothing and normalization. The vocabulary and weighting statistics must be fitted on training data only. 1.5 Missing Values and OutliersMissingness may be caused by random failure, a measurement process or the behaviour being studied. Common approaches include deletion, constant-value imputation, mean or median imputation, model-based imputation and missingness indicators. Imputation should be performed inside the training pipeline. Otherwise, the validation set can influence the imputed values. An indicator can be defined as [[MATH(m = 1)]] when a value is missing and [[MATH(m = 0)]] otherwise. This can preserve information when missingness itself is predictive, although it may also encode administrative or social processes that create unfairness. Outliers should not automatically be removed. They may represent errors, rare but important events or a valid heavy-tailed population. Robust transformations, winsorization, domain-based validation and algorithms resistant to extreme values are alternatives. Any removal rule must be based on information available during training and applied consistently during deployment. 2. Feature Selection and the Information BoundaryFeature engineering creates candidate variables; feature selection determines which candidates should enter the model. Selection can be based on domain knowledge, statistical association, model-based importance, regularization or computational constraints. Filter methods rank variables independently of the final model, for example by correlation or mutual information. Wrapper methods evaluate subsets using model performance but can be computationally expensive. Embedded methods perform selection during training, as with L1 regularization or tree-based splitting. Correlation does not establish predictive usefulness, and a feature with weak marginal association may be valuable through interaction with another feature. Conversely, highly predictive variables may be unacceptable if they are proxies for protected characteristics, unavailable at decision time or consequences of the target. LeakageLeakage occurs when information unavailable at prediction time enters model training or evaluation. Examples include: - using a customer’s eventual default status to predict whether the customer will default;
- calculating a medical feature using measurements taken after treatment;
- randomly splitting time-ordered data when future patterns influence the past;
- fitting an imputer, scaler or target encoder before cross-validation;
- selecting features using the full dataset before creating a test set.
Leakage often produces impressive validation scores and disappointing deployment performance. The remedy is to define the prediction time, draw an explicit information boundary and place all learned transformations inside a reproducible pipeline. 3. Experimental Design for Model Evaluation3.1 Train, Validation and Test SetsThe training set estimates model parameters. The validation set supports choices such as hyperparameters, feature sets and model families. The test set is reserved for a final, approximately unbiased estimate of generalization performance. A common workflow is: - Define the target, prediction time and evaluation metric.
- Split data according to its structure.
- Fit preprocessing and candidate models on training data.
- Use validation data or cross-validation to select the approach.
- Lock the final specification.
- Evaluate once on the untouched test set.
- Report uncertainty, subgroup performance and operational implications.
Repeatedly checking the test set turns it into an informal validation set. If the dataset is small, nested cross-validation can separate hyperparameter selection from performance estimation. 3.2 Choosing the SplitFor independent observations, a random split may be suitable. Stratification preserves approximately similar class proportions across splits, but it does not solve temporal or group dependence. For time-dependent data, training observations should precede validation and test observations. For example, a model trained on January–September may be validated on October and tested on November. For repeated observations from the same person, household or machine, group-based splitting prevents the same entity appearing in both training and evaluation sets. 3.3 Cross-ValidationIn k-fold cross-validation, the training data are divided into k folds. Each fold is used once for validation while the remaining k - 1 folds are used for fitting. For a loss function L, the estimated cross-validation error is: CVk = (1/k) ∑j=1k Lj where Lj is the loss measured on fold j. This estimate is useful under assumptions that folds represent the deployment population and observations are sufficiently independent given the split design. Stratified folds are commonly used for classification. Grouped and blocked folds are used when observations are clustered or ordered. Cross-validation does not eliminate leakage: preprocessing and feature selection must be repeated within each fold. 4. Performance MetricsA metric should reflect the cost of errors and the decision being supported. No single metric is universally best. 4.1 Regression MetricsFor actual values yi, predictions [[MATH(ŷ_i)]], and n observations, the mean absolute error is: MAE = (1/n) ∑i=1n |yi - ŷi| MAE has the same units as the target and gives linear weight to errors. The root mean squared error is: RMSE = √[(1/n) ∑i=1n (yi - ŷi)2] RMSE also has target units but penalizes large errors more strongly. The coefficient of determination is: R² = 1 - [∑(yi - ŷi)² / ∑(yi - ȳ)²] where [[MATH(ȳ)]] is the mean of observed target values. Relative to predicting the sample mean, R² can be negative on test data; it is not a percentage accuracy measure. Mean absolute percentage error, MAPE = (100/n)∑| (yi - ŷi) / yi |, is undefined when yi = 0 and unstable near zero. It should therefore be used only when the target is strictly positive and percentage error is substantively meaningful. 4.2 Classification MetricsFor binary classification, the confusion matrix contains true positives (TP), true negatives (TN), false positives (FP) and false negatives (FN). Accuracy = (TP + TN) / (TP + TN + FP + FN) Accuracy can be misleading under class imbalance. Precision and recall are: Precision = TP / (TP + FP)
Recall = TP / (TP + FN) Precision measures the proportion of predicted positives that are correct; recall measures the proportion of actual positives detected. The F1 score is the harmonic mean: F1 = 2 × (Precision × Recall) / (Precision + Recall) It is defined when precision plus recall is non-zero. Specificity is TN/(TN + FP). The false-positive and false-negative costs should determine the classification threshold rather than an automatic choice of 0.5. ROC–AUC measures ranking performance across thresholds, while precision–recall curves are often more informative when the positive class is rare. A well-ranked model may still produce poorly calibrated probabilities. Calibration asks whether predictions near 0.7 correspond approximately to events occurring 70% of the time in comparable groups. Worked Example: Imbalanced ClassificationSuppose a fraud detector produces TP = 72, TN = 850, FP = 28 and FN = 50. There are 1,000 cases. Accuracy is (72 + 850)/1000 = 0.922, or 92.2%. Precision is 72/(72 + 28) = 0.72, or 72%. Recall is 72/(72 + 50) ≈ 0.590, or approximately 59.0%. The F1 score is approximately 2×(0.72×0.590)/(0.72+0.590) ≈ 0.649. The 92.2% accuracy alone hides that approximately 41% of fraud cases were missed. A production decision might prioritize recall, precision, expected financial cost or a constrained combination of these measures. 5. Diagnosing Model BehaviourOverfittingOverfitting occurs when a model captures idiosyncratic noise in training data and performs substantially worse on new data. Symptoms include low training error, higher validation error and unstable performance across folds. Remedies include simpler models, regularization, more data, stronger feature controls, early stopping and leakage checks. UnderfittingUnderfitting occurs when the model is too simple to capture important structure. Both training and validation performance are poor. Remedies include more informative features, a more flexible model, reduced regularization or improved optimization. Increasing complexity is not justified if the underlying features are weak or the target is noisy. Learning CurvesLearning curves plot training and validation performance against training-set size or model complexity. A persistent high error on both curves suggests underfitting or irreducible noise. A large gap suggests overfitting. These are diagnostic patterns, not mechanical proofs; distribution shift and measurement error can create similar observations. Imbalance and ResamplingClass imbalance should first be treated as a measurement and decision problem, not merely a sampling problem. Options include class-weighted losses, threshold adjustment, over-sampling, under-sampling and synthetic methods such as SMOTE. Resampling must occur inside training folds. The untouched validation and test sets should retain the deployment distribution unless a clearly justified alternative evaluation is required. Reported results should include per-class metrics, confusion matrices, calibration and subgroup performance. A model can have acceptable aggregate recall while failing a small but important population. 6. Explainability and InterpretabilityInterpretability concerns how understandable a model or its reasoning is. Explainability methods provide evidence about model behaviour, but an explanation is not automatically a causal account. A feature that contributes to a prediction is not necessarily a cause of the outcome. Global ExplanationsGlobal methods describe overall behaviour. Examples include: - coefficients in a linear model, interpreted with attention to scaling, interactions and regularization;
- feature importance from tree ensembles;
- permutation importance, which measures performance change after shuffling a feature;
- partial dependence plots, which show average predicted response as a feature varies;
- accumulated local effects, which are often more robust when features are correlated.
Permutation importance can be misleading when correlated variables contain overlapping information: shuffling one may have little effect because another substitutes for it. Partial dependence can also evaluate unrealistic combinations of feature values. Local ExplanationsLocal methods explain an individual prediction. LIME fits a simple local approximation around a selected instance. SHAP methods allocate a prediction difference among features using Shapley-value ideas. For an additive explanation, a prediction can be expressed conceptually as: f(x) = φ₀ + ∑j=1p φj where f(x) is the model prediction, φ₀ is a baseline value and φj is the contribution attributed to feature j for that instance. The interpretation depends on the chosen background distribution, model assumptions and link scale. Contributions are not necessarily causal effects. Counterfactual explanations describe a nearby change that would alter an outcome, such as “if income were higher and debt lower, the classification would change.” Such explanations must be actionable, feasible and ethically acceptable. It is inappropriate to recommend changing immutable characteristics or to imply that an individual is responsible for structural disadvantage. Explanation QualityA useful explanation should be: - faithful: it reflects the actual model rather than a persuasive approximation;
- stable: small irrelevant changes do not produce arbitrary explanations;
- understandable: its language and complexity suit the audience;
- actionable: it identifies factors that a user can legitimately address;
- complete enough: it communicates uncertainty and important limitations; and
- governed: it is logged, versioned and reviewable.
Explainability should be combined with error analysis, subgroup testing, calibration analysis and domain review. A plausible explanation does not prove that a model is fair or correct. 7. Applied Case: Predicting Customer ChurnThe following is an illustrative case. Background and ProblemA subscription company wants to identify customers at high risk of cancelling within the next 30 days so that retention teams can offer appropriate support. The dataset contains customer identifiers, subscription history, usage events, service contacts, billing records and cancellation outcomes. AnalysisThe team first defines the prediction timestamp and labels a customer as positive if cancellation occurs within the following 30 days. It discovers that a “closure reason” field is recorded after cancellation. Although highly predictive, it is unavailable at decision time and is removed. Random splitting is rejected because multiple monthly records belong to the same customer and future records could enter training. A time-based split is used, with customer-group controls where necessary. Features include usage decline over the previous four weeks, days since the last login, number of unresolved support cases, subscription age and payment failures. Rolling features use only events before the prediction timestamp. Missing usage is represented using an imputation pipeline and, where operationally meaningful, a missingness indicator. Approach and SolutionThe team compares a regularized logistic regression with a gradient-boosted tree model. Preprocessing is fitted within each training fold. Stratified evaluation is used within the training period, while the final test period is chronologically later. Because only 6% of customers churn, accuracy is not selected as the primary metric. The team reports recall at a fixed contact capacity, precision, PR–AUC, calibration and performance by customer segment. SHAP-based local explanations are used by analysts to inspect individual scores, while permutation importance and grouped error analysis are used for global review. The team finds that support delays and declining usage are important, but also discovers that a regional service-code feature is acting as a proxy for a historical operational process. That feature is removed and the model is retrained. OutcomeThe final model achieves lower aggregate ranking performance than the initial leaky model but performs more reliably in the future test period. Retention staff receive risk scores with concise evidence and are instructed to offer assistance rather than apply automatic penalties. Monitoring tracks data drift, calibration, recall, contact outcomes and subgroup disparities. Learning PointsThe case demonstrates that a lower but trustworthy test score is preferable to a high score produced by leakage. It also shows that feature availability, temporal structure, capacity constraints, subgroup analysis and explanation governance are part of model quality, not optional additions. 8. Professional Practice, Ethics and GovernanceA production evaluation plan should record the data version, feature definitions, split logic, preprocessing parameters, random seeds, model version, metrics, confidence intervals where feasible and known limitations. Performance should be monitored after deployment because population behaviour, measurement systems and policies change. Fairness analysis should examine whether errors, selection rates, calibration or benefits differ across legally protected or operationally important groups. Fairness criteria can conflict mathematically and operationally, particularly when base rates differ. Therefore, the appropriate objective must be determined with domain experts, affected communities, legal advisers and accountable decision-makers. Privacy principles include data minimization, purpose limitation, access controls, retention limits and protection against re-identification. Explanations must not disclose sensitive information unnecessarily. Automated decisions with significant effects may require human review, contestability, documentation and compliance with applicable data-protection and sector-specific regulation. 9. Best-Practice WorkflowA robust workflow is: - Define the decision, target, prediction time and error costs.
- Audit data provenance, missingness, quality and permitted use.
- Split data according to time, group and deployment structure.
- Build preprocessing and feature generation as a leakage-resistant pipeline.
- Establish a simple baseline model.
- Select metrics that reflect operational and social consequences.
- Use appropriate cross-validation and tune only on development data.
- Compare training, validation and test behaviour for overfitting.
- Inspect subgroup performance, calibration and error examples.
- Use global and local explanations, documenting their assumptions.
- Lock the model and test it on untouched data.
- Monitor drift, performance, fairness, explanations and user feedback after deployment.
Chapter SummaryFeature engineering is the disciplined design of model inputs from raw data. Effective features are relevant, available at prediction time, reliable, ethically permissible and suitable for the algorithm. Numerical transformations, categorical encoding, temporal features, text representations, missing-value treatment and interaction features must be fitted without allowing evaluation data to influence training. Model evaluation is an experimental-design problem. Train, validation and test roles must remain distinct, while cross-validation must respect time, groups and class structure. MAE, RMSE, R², precision, recall, F1, ROC–AUC, PR–AUC and calibration answer different questions. Accuracy alone is often inadequate for imbalanced classification. Overfitting, underfitting, leakage and imbalance require diagnostic analysis rather than automatic remedies. Explainability methods such as coefficients, permutation importance, partial dependence, LIME, SHAP and counterfactuals offer different global and local perspectives. They should be interpreted as descriptions of model behaviour, not unquestionable causal explanations. Professional systems combine technical performance with fairness, privacy, accountability, reproducibility and post-deployment monitoring. Key TermsFeature engineering: Transforming raw data into useful model inputs. Feature selection: Choosing a subset of candidate features for modelling. Standardization: Rescaling a variable using its training mean and standard deviation. One-hot encoding: Representing categories with binary indicator variables. Target encoding: Replacing a category with an estimate based on target outcomes. Imputation: Replacing or modelling missing values. Data leakage: Use of information unavailable at prediction time. Overfitting: Learning noise or idiosyncrasies that reduce generalization. Underfitting: Failing to represent important structure in the data. Cross-validation: Repeated fitting and validation across data folds. Stratification: Preserving approximate class proportions across splits. Group split: Separating related observations to prevent entity leakage. Calibration: Agreement between predicted probabilities and observed frequencies. Precision: The proportion of predicted positives that are true positives. Recall: The proportion of actual positives that are detected. Class imbalance: Unequal prevalence of outcome classes. Global explanation: Description of model behaviour across a population. Local explanation: Explanation of a particular prediction. Counterfactual explanation: A description of changes that could alter a prediction. Concept drift: Change in the relationship between inputs and outcomes over time. Review QuestionsShort Answer Questions- Why must preprocessing parameters such as a mean or standard deviation be fitted on training data only?
- Distinguish precision from recall.
- Give two examples of data leakage.
- Why can accuracy be misleading for an imbalanced classification problem?
- What is the difference between a global and a local explanation?
Long Answer Questions- Explain how numerical, categorical and temporal features can be engineered while preserving the information boundary of a prediction task.
- Compare train-validation-test splitting with cross-validation, including circumstances requiring temporal or group-based splits.
- Discuss MAE, RMSE, R², precision, recall, F1 and calibration, and explain when each may be appropriate.
- Explain the causes and remedies of overfitting, underfitting, leakage and class imbalance.
- Compare permutation importance, partial dependence, SHAP and counterfactual explanations, including their limitations.
Analytical / Application Questions- A bank has 2% loan defaults. Its classifier reports 98% accuracy but detects only 20% of defaults. Design a more suitable evaluation and threshold-selection plan.
- A forecasting model uses a rolling average that accidentally includes the value being forecast. Identify the error, explain its effect on evaluation and propose a corrected feature definition.
- A churn model performs well in cross-validation but poorly in the next quarter, particularly for one region. Propose a structured investigation covering drift, leakage, feature quality, calibration, fairness and monitoring.
Multiple Choice Questions with AnswersQuestion 1Which action best prevents preprocessing leakage during cross-validation? A. Fit the scaler on the complete dataset before splitting B. Fit the scaler separately within each training fold C. Fit the scaler only on the final test set D. Avoid scaling all numerical variables Correct Answer: B. Fit the scaler separately within each training fold Question 2Which metric is generally more informative than accuracy when the positive class is rare and ranking quality is important? A. Precision–recall area under the curve B. Mean absolute error C. R-squared D. Training loss only Correct Answer: A. Precision–recall area under the curve Question 3A model has low training error and much higher validation error. Which diagnosis is most likely? A. Underfitting B. Overfitting C. Perfect calibration D. No class imbalance Correct Answer: B. Overfitting Question 4Which split is most appropriate for predicting future demand from historical observations? A. A random split that mixes future and past observations B. A split in which the test period precedes the training period C. A chronological split that trains on earlier periods D. A split based only on alphabetical order of customers Correct Answer: C. A chronological split that trains on earlier periods Question 5What 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 average squared prediction error Correct Answer: B. The proportion of actual positives that are detected Question 6Why is ordinal encoding unsuitable for nominal categories such as colours? A. It always creates missing values B. It requires a time-ordered dataset C. It falsely implies numerical order and spacing D. It can be used only for regression targets Correct Answer: C. It falsely implies numerical order and spacing Question 7Which statement about RMSE is correct? A. It has no units B. It penalizes large errors more strongly than MAE C. It is defined only for classification D. It always lies between zero and one Correct Answer: B. It penalizes large errors more strongly than MAE Question 8What is a local explanation intended to describe? A. The average behaviour of the whole population only B. The legal status of a dataset C. The reasons associated with one particular prediction D. The number of rows in the training set Correct Answer: C. The reasons associated with one particular prediction Question 9A feature records a customer’s cancellation reason after cancellation occurs. Using it to predict cancellation is primarily an example of: A. Data leakage B. Under-sampling C. Calibration D. Standardization Correct Answer: A. Data leakage Question 10Which statement best describes a counterfactual explanation? A. It proves that a feature caused the outcome B. It describes a feasible change that could alter a prediction C. It reports only the model’s training accuracy D. It replaces the need for a test set Correct Answer: B. It describes a feasible change that could alter a prediction
|