Programming for Data Science (Python/R)

Chapter 12
Programming Machine Learning and AI Workflows

Post Graduate Diploma in Data Science
Programming for Data Science (Python/R)   •   Post Graduate

12. Programming Machine Learning and AI Workflows

Introduction and Context

Machine learning projects rarely fail because a particular algorithm is unavailable. They fail more often because data are prepared inconsistently, evaluation is poorly designed, information leaks from the future into the past, or a model is optimized for a metric that does not represent the real decision. Programming for machine learning therefore involves much more than calling a training function. It requires the construction of a reproducible workflow that connects data acquisition, validation, preprocessing, model fitting, tuning, evaluation, interpretation, deployment, and monitoring.

For a data scientist working in Python or R, this workflow is implemented through coordinated abstractions. In Python, scikit-learn provides estimators, transformers, pipelines, model-selection utilities, and metrics. The broader Python ecosystem adds tools such as pandas, NumPy, XGBoost, PyTorch, MLflow, and SHAP. In R, tidymodels provides a unified framework through packages such as recipes, parsnip, workflows, rsample, tune, yardstick, and dials. Although syntax differs, both ecosystems express similar principles: separate roles clearly, fit transformations only on training data, resample honestly, tune systematically, and preserve the full procedure for later use.

This chapter develops these principles for supervised and unsupervised learning. It emphasizes the reasoning behind workflow design rather than isolated code fragments. The central question is not merely whether a model predicts well on a dataset, but whether the reported performance is credible, reproducible, interpretable, operationally useful, and ethically defensible.

Learning Objectives

After studying this chapter, students should be able to:

1. Design end-to-end supervised and unsupervised learning workflows in Python and R.

2. Distinguish training, validation, test, and production data roles.

3. Apply preprocessing through leakage-resistant pipelines and recipes.

4. Select resampling strategies appropriate to temporal, grouped, imbalanced, and ordinary data.

5. Tune hyperparameters and compare models using suitable metrics.

6. Diagnose overfitting, underfitting, data leakage, and distribution shift.

7. Interpret model outputs using global and local explanation methods.

8. Track experiments, artifacts, assumptions, and model versions reproducibly.

9. Identify ethical, governance, privacy, and operational risks in AI workflows.

From a Dataset to a Learning System

A machine learning workflow is a sequence of transformations and decisions. A typical supervised workflow begins with a problem definition, followed by data auditing, splitting, preprocessing, model specification, resampling, tuning, final assessment, interpretation, and deployment. These stages are not strictly linear: exploratory findings may alter the target definition, and deployment monitoring may reveal the need for a new training design. Nevertheless, explicit stage boundaries reduce accidental reuse of information and make decisions auditable.

The target variable must be defined before model selection. Classification predicts categories, regression predicts numerical quantities, and ranking or survival problems require specialized targets and evaluation procedures. A target should represent the decision that the organization actually needs to make. For example, predicting whether a customer will respond to an offer is different from predicting whether the customer will eventually purchase. Confusing these outcomes can produce a technically accurate but operationally irrelevant model.

A dataset is normally divided into a training set and a final test set. The training set supports preprocessing, model fitting, feature selection, and hyperparameter tuning. The test set is held back until the workflow has been finalized. If the test set is repeatedly consulted, it becomes part of the development process and no longer provides an unbiased estimate of future performance. In some settings, a separate validation set is used; in resampling-based workflows, cross-validation often performs the validation role more efficiently.

Estimators, Transformers, and Workflows

In scikit-learn, an estimator implements `fit`, while a predictor commonly implements `predict` or `predict_proba`. A transformer implements `fit` and `transform`, as with `StandardScaler`, `OneHotEncoder`, and `SimpleImputer`. A `Pipeline` combines transformations and a final estimator so that the same operations are applied consistently during training, validation, testing, and deployment.

A conceptual Python workflow may be represented as follows:

```python

from sklearn.compose import ColumnTransformer

from sklearn.pipeline import Pipeline

from sklearn.impute import SimpleImputer

from sklearn.preprocessing import OneHotEncoder, StandardScaler

from sklearn.linear_model import LogisticRegression

numeric_pipe = Pipeline([

("impute", SimpleImputer(strategy="median")),

("scale", StandardScaler())

])

categorical_pipe = Pipeline([

("impute", SimpleImputer(strategy="most_frequent")),

("encode", OneHotEncoder(handle_unknown="ignore"))

])

preprocess = ColumnTransformer([

("num", numeric_pipe, numeric_columns),

("cat", categorical_pipe, categorical_columns)

])

workflow = Pipeline([

("preprocess", preprocess),

("model", LogisticRegression(max_iter=1000))

])

```

The important feature is not the particular classifier. It is that imputation, scaling, encoding, and model fitting form one object. During cross-validation, each fold fits its preprocessing only on that fold's training portion. This prevents statistics such as means, medians, category frequencies, or selected features from being calculated using validation observations.

In tidymodels, the analogous design uses a `recipe` for preprocessing, a `parsnip` model specification, and a `workflow` to combine them. A simplified pattern is:

```r

rec <- recipe(outcome ~ ., data = training_data) |>

step_impute_median(all_numeric_predictors()) |>

step_normalize(all_numeric_predictors()) |>

step_dummy(all_nominal_predictors())

spec <- logistic_reg() |>

set_engine("glm") |>

set_mode("classification")

wf <- workflow() |>

add_recipe(rec) |>

add_model(spec)

```

A recipe has a preparation phase and a baking phase. Preparation estimates required quantities from the analysis data, while baking applies the learned transformations to new data. The workflow ensures that these operations occur within resampling rather than before it.

Preprocessing and the Problem of Leakage

Data leakage occurs when information unavailable at prediction time influences model fitting or evaluation. Leakage can be direct, such as including a variable recorded after the target event, or indirect, such as scaling the entire dataset before cross-validation. Leakage produces overly optimistic estimates and may remain hidden until deployment.

Common forms include:

  • Temporal leakage: using future measurements to predict an earlier event.
  • Target leakage: using a variable created from, or after, the outcome.
  • Preprocessing leakage: estimating imputation, scaling, feature selection, or dimensionality reduction on all observations before splitting.
  • Group leakage: placing records from the same person, household, patient, or machine in both training and validation sets when the deployment task concerns unseen groups.
  • Duplicate leakage: allowing identical or near-identical records to occur across partitions.

Leakage prevention begins with understanding how data are generated. A random split is inappropriate when observations have time order, repeated entities, or spatial dependence. In a credit-risk model, for example, a random split may mix multiple applications from the same applicant. The resulting estimate answers the question “How well does the model predict another record from known applicants?” rather than “How well does it predict a new applicant?”

Feature engineering must also respect the prediction timestamp. Aggregate features such as a customer's number of transactions should be calculated only from transactions available before the prediction moment. In production, feature definitions should be versioned and tested against historical snapshots.

Resampling as a Design Decision

Resampling estimates how a fitted procedure may perform on unseen data. In k-fold cross-validation, the training data are divided into k folds. Each fold is held out once, while the remaining folds are used for fitting. The mean and variability of the evaluation metric summarize performance across splits. Repeated cross-validation reduces dependence on one partition but increases computation.

Resampling is not merely a technical convenience. It defines the population to which the result generalizes. Appropriate choices include:

  • Stratified cross-validation for classification with unequal class proportions.
  • Group cross-validation when observations from the same entity must remain together.
  • Time-series or rolling-origin validation when future observations must not influence past predictions.
  • Leave-one-group-out validation when the number of meaningful groups is small and generalization to new groups is central.
  • Bootstrap resampling for uncertainty estimation and certain small-sample settings.

For highly imbalanced outcomes, stratification preserves approximate class proportions, but it does not solve the substantive problem by itself. The analyst must also choose threshold-dependent or threshold-independent metrics appropriate to the cost of errors.

Nested cross-validation is useful when an unbiased estimate is required after extensive hyperparameter tuning. An inner loop selects hyperparameters, while an outer loop estimates generalization. Without nesting, repeated optimization against the same resampling results can make the reported score optimistic.

Supervised Learning: Fitting, Tuning, and Evaluation

A model has parameters learned from data and hyperparameters selected by the analyst or tuning procedure. In regularized regression, coefficients are parameters, while the penalty strength is a hyperparameter. Tree depth, learning rate, number of trees, and minimum leaf size are other examples. Hyperparameters control the flexibility and computational behavior of the learner.

Tuning should be guided by a meaningful search space. Grid search is transparent but can be inefficient when many dimensions exist. Random search often explores high-value regions more effectively. Successive-halving and Bayesian optimization can allocate resources adaptively, although they require careful implementation and validation. The search procedure itself should be reproducible through fixed seeds, documented ranges, and recorded software versions.

Metric selection must follow the decision context. For regression, mean absolute error treats errors linearly and is relatively interpretable; mean squared error penalizes large errors more strongly; root mean squared error returns to the target's units; and the coefficient of determination describes variance relative to a baseline but is not a direct measure of operational error. For classification, accuracy can be misleading under class imbalance. Precision measures the proportion of predicted positives that are correct, while recall measures the proportion of actual positives detected. Specificity measures the proportion of negatives correctly rejected. F1 summarizes precision and recall through their harmonic mean. ROC-AUC evaluates ranking over thresholds, whereas precision-recall curves are often more informative when positive cases are rare.

A probability model should be evaluated for calibration as well as discrimination. A calibrated model assigning probabilities near 0.8 should be correct approximately 80 percent of the time among comparable predictions. Calibration plots, Brier score, and recalibration methods help assess this property. A model can have strong ranking performance but poor probability accuracy.

The decision threshold should not automatically be set to 0.5. It should reflect the relative costs of false positives and false negatives, operational capacity, and regulatory requirements. Threshold selection must be performed using training or validation data, not optimized on the final test set.

Unsupervised Workflows

Unsupervised learning does not use a known target. It seeks structure such as groups, lower-dimensional representations, or anomalies. This changes the meaning of validation. There may be no single correct answer, so evaluation combines internal criteria, stability, domain interpretation, and downstream usefulness.

Clustering methods differ in their assumptions. K-means seeks compact, approximately spherical clusters and is sensitive to scale and initialization. Hierarchical clustering produces a tree of nested partitions and supports different linkage choices. Density-based methods such as DBSCAN identify dense regions and can label isolated observations as noise, but their results depend on distance and density parameters. Before clustering, variables may need transformation, scaling, or careful treatment of missingness. Including a high-variance variable without standardization can cause it to dominate distance calculations.

The silhouette coefficient compares within-cluster cohesion with separation from neighboring clusters. It is useful but not definitive. A high score may favor an artificial partition that is not useful to the organization. Cluster stability can be assessed by resampling the data and comparing assignments. Analysts should also examine whether clusters are driven by data artifacts, protected characteristics, or measurement differences rather than meaningful behavioral patterns.

Principal component analysis transforms correlated variables into orthogonal components ordered by explained variance. It is useful for compression, visualization, and noise reduction, but components are linear combinations whose interpretation may be difficult. PCA must be fitted within the training portion when it is used before a predictive model. In an unsupervised exploratory analysis, the analyst should still document whether the transformation used information from all available records and what population the result describes.

Interpretability and Explanation

Interpretability is not one property. It may refer to a model that is intrinsically understandable, a post hoc explanation of a complex model, or evidence that predictions are sensible and stable. Linear models and shallow decision trees offer relatively direct global interpretation. Ensemble and neural models may require additional tools.

Global interpretation asks how the model behaves overall. Feature importance, permutation importance, partial dependence, and accumulated local effects can reveal associations between predictors and predictions. Local interpretation explains one prediction using methods such as SHAP or local surrogate models. These explanations are conditional on the data and model; they do not automatically establish causation.

Permutation importance can be misleading when predictors are strongly correlated, because one variable may appear unimportant when another provides substitute information. Partial dependence can also be unrealistic when it evaluates combinations of feature values that rarely occur together. Explanation methods should therefore be accompanied by data-density checks, sensitivity analysis, and domain review.

For high-stakes decisions, explanation is only one part of accountability. The organization must also provide contestability, human oversight, documentation of intended use, and procedures for correcting inaccurate or discriminatory outcomes.

Experiment Tracking and Reproducibility

A credible experiment is more than a score in a notebook. It should record the dataset version, feature definitions, split strategy, preprocessing steps, model class, hyperparameters, random seeds, software environment, evaluation metrics, and artifacts such as fitted models and diagnostic plots. Tools such as MLflow, Weights & Biases, and cloud-native experiment platforms support this practice. In R, projects may combine tidymodels with version control, renv, targets, and model registries.

Reproducibility has several levels. A script may reproduce a result in one environment; a container may reproduce it across machines; a fully governed pipeline may reproduce the data, code, environment, and approval history. Deterministic seeds are helpful but do not guarantee identical results across hardware, library versions, parallel algorithms, or nondeterministic operations.

A model registry should distinguish candidate, approved, deployed, and retired versions. Deployment should include validation of input schema, acceptable ranges, missingness, and category values. Monitoring should cover data drift, prediction drift, performance when labels become available, calibration, latency, and fairness indicators.

Illustrative Case Study: Predicting Equipment Failure

Background

Consider an illustrative manufacturing company operating industrial pumps. The company wants to predict whether a pump will experience a critical failure within the next seven days so that maintenance can be scheduled in advance.

Situation and Problem

The available data include vibration, temperature, pressure, operating hours, maintenance records, and failure logs. Several sensor readings are collected every hour. The initial team randomly splits all rows into training and test sets and reports an accuracy of 97 percent. Management is impressed, but maintenance engineers question the result because failures are rare and many rows come from the same pump.

Analysis

The random row-level split creates group leakage: readings from the same pump appear in both partitions. It also creates temporal leakage because records collected after a maintenance intervention are used to predict events that occurred earlier in the operational sequence. Accuracy is inflated because the majority of rows represent non-failure periods. The operational question concerns future failures on pumps that may have different usage histories, not classification of arbitrary sensor rows.

Approach and Solution

The team first defines a prediction timestamp and constructs labels using only events occurring in the subsequent seven days. Features are generated from historical windows ending at that timestamp. Maintenance notes entered after a failure are excluded. Pumps are divided by identity so that a pump appears in only one partition, and a time-based holdout is retained for final testing. Within the training data, grouped and time-aware resampling is used.

A preprocessing recipe imputes missing sensor values, creates lagged summaries, and standardizes numerical variables within each resampling split. Several models are compared, including regularized logistic regression, a random forest, and gradient boosting. The primary metric is precision-recall AUC because failures are uncommon; recall at a maintenance team's fixed weekly capacity is also reported. Calibration is assessed because maintenance planners need meaningful probabilities.

The best candidate is selected using validation results, then refitted on the complete development data. The untouched temporal test set is used once for final assessment. Feature contributions are reviewed with engineers, and the model is monitored after deployment for sensor drift, changes in maintenance policy, and performance by pump type.

Outcome

The revised evaluation produces a lower score than the original 97 percent accuracy, but the result is more credible and operationally useful. The maintenance team receives a ranked list of pumps and can investigate the highest-risk cases within its capacity. The organization also obtains a documented data-generation process, a reproducible training pipeline, and clear criteria for model review.

Learning Points

The case demonstrates that a lower but valid estimate is preferable to an impressive leaked estimate. It also shows that metric selection, grouping, temporal design, calibration, and operational capacity must be considered together. A model is part of a decision system, not an isolated mathematical object.

Challenges, Risks, and Professional Responsibilities

Overfitting occurs when a workflow captures idiosyncrasies of the development data rather than generalizable relationships. It can result from excessive model complexity, weak sample size, repeated tuning, high-dimensional features, or noisy labels. Regularization, early stopping, simpler models, stronger validation, feature reduction, and additional data may help, but no method substitutes for a sound problem definition.

Data quality problems include missing-not-at-random values, inconsistent units, label noise, duplicates, and changing measurement procedures. Removing outliers mechanically may discard important rare events. Imputation can introduce bias if the missingness mechanism is ignored. Analysts should document assumptions and assess sensitivity to plausible alternatives.

Fairness requires more than a single group comparison. Performance, calibration, error rates, access to intervention, and the consequences of decisions may differ across groups. Some fairness criteria cannot all be satisfied simultaneously when base rates differ. The appropriate choice depends on law, context, stakeholder values, and the harm associated with errors. Protected attributes should not be removed automatically: they may be needed to audit disparities, although their use in prediction requires careful governance.

Privacy risks arise from direct identifiers, quasi-identifiers, linkage attacks, and model outputs that reveal sensitive information. Data minimization, access control, retention limits, encryption, audit logs, and applicable data-protection requirements should be integrated into the workflow. In regulated or high-impact applications, documentation should cover purpose, data provenance, limitations, human oversight, and mechanisms for appeal.

Current Directions and Best Practice

Modern workflows increasingly combine classical machine learning with deep learning, foundation models, automated feature generation, and retrieval-augmented systems. These technologies expand capability but also increase risks involving computational cost, opaque behavior, prompt or data injection, copyright, privacy, and unstable outputs. Evaluation must therefore include robustness, security, provenance, and human review rather than relying only on average predictive scores.

The following practices provide a reliable baseline:

  • Define the prediction target, decision point, and population before modeling.
  • Split data according to time, group, or deployment conditions.
  • Keep preprocessing, feature selection, and dimensionality reduction inside the workflow.
  • Use resampling that matches the data-generating process.
  • Select metrics according to consequences, prevalence, and operational constraints.
  • Preserve a final test set and avoid repeated test-set inspection.
  • Track code, data, environment, parameters, and artifacts.
  • Assess calibration, subgroup performance, robustness, and explanation stability.
  • Validate input data and monitor post-deployment behavior.
  • Treat model approval, retraining, and retirement as governed organizational processes.

Chapter Summary

Machine learning programming is the disciplined construction of a reproducible decision workflow. Scikit-learn pipelines and tidymodels workflows connect preprocessing with model fitting and ensure that transformations are learned only from appropriate training data. Honest resampling depends on the structure of the problem, including class imbalance, groups, time, and spatial dependence. Hyperparameter tuning must be separated from final assessment, and metrics must reflect operational costs rather than convenience.

Unsupervised workflows require attention to scaling, stability, assumptions, and domain usefulness because no target necessarily identifies a correct answer. Interpretability methods can support diagnosis and accountability but do not establish causation. Experiment tracking, versioning, monitoring, privacy protection, fairness analysis, and human oversight extend the workflow beyond model training. The professional data scientist therefore evaluates not only whether a model predicts, but whether the entire system is valid, useful, governable, and safe.

Key Terms

Data leakage: Use of information during training or evaluation that would not be available at prediction time.

Estimator: A learning object that fits parameters from data, commonly through a `fit` operation.

Transformer: A component that learns a data transformation and applies it to observations.

Pipeline: An ordered object that combines preprocessing and modeling steps into one reproducible procedure.

Recipe: A tidymodels object specifying and learning preprocessing operations.

Training set: Data used to estimate model parameters and learn preprocessing quantities.

Validation set: Data used during development to compare models or tune hyperparameters.

Test set: Held-out data used for final performance assessment.

Cross-validation: A resampling method that repeatedly fits a model on subsets and evaluates it on held-out folds.

Nested cross-validation: Resampling with inner tuning loops and outer performance-estimation loops.

Hyperparameter: A modeling choice selected outside the ordinary parameter-fitting process.

Overfitting: Learning patterns specific to development data that do not generalize.

Calibration: Agreement between predicted probabilities and observed event frequencies.

Precision: The proportion of predicted positive cases that are truly positive.

Recall: The proportion of actual positive cases that are correctly identified.

Group leakage: Information contamination caused by placing related entities in multiple data partitions.

Concept drift: Change over time in the relationship between predictors and the target.

Permutation importance: Change in model performance after randomly rearranging a feature.

Experiment tracking: Systematic recording of data, code, parameters, metrics, and artifacts for experiments.

Model governance: Policies and controls covering approval, monitoring, use, review, and retirement of models.

Review Questions

Short Answer Questions

1. Why should preprocessing be placed inside a scikit-learn pipeline or tidymodels workflow?

2. Distinguish a parameter from a hyperparameter.

3. What is the principal difference between precision and recall?

4. Why can accuracy be misleading for rare-event classification?

5. What does calibration mean in a probabilistic classifier?

Long Answer Questions

1. Explain how leakage can arise through preprocessing, time, groups, and target construction. Provide an example of each.

2. Compare ordinary, stratified, grouped, and time-based cross-validation and identify a suitable use case for each.

3. Discuss the role of metric selection in aligning a machine learning model with an organizational decision.

4. Explain how a tidymodels workflow and a scikit-learn pipeline support reproducibility and deployment consistency.

5. Describe a complete workflow for tuning, evaluating, interpreting, approving, and monitoring a supervised learning model.

Analytical / Application Questions

1. A hospital has repeated records for each patient and wants to predict readmission for new patients. Design the data split, resampling strategy, preprocessing workflow, and evaluation metrics. Justify each decision.

2. A fraud model has a high ROC-AUC but low precision at the organization's investigation capacity. Analyze possible reasons and propose a revised evaluation and threshold-selection process.

3. A clustering analysis produces three groups with strong silhouette scores, but one cluster consists almost entirely of records from a single data-collection site. How would you determine whether the clustering reflects meaningful structure or a measurement artifact?

22. Multiple Choice Questions with Answers

Question 1

A data scientist computes the mean and standard deviation for scaling using the complete dataset before performing cross-validation. What is the primary problem?

A. The model will always underfit because scaling reduces variance

B. Validation folds influence preprocessing, creating information leakage

C. Cross-validation cannot be used with numerical variables

D. Standardization is valid only for classification models

Correct Answer: B. Validation folds influence preprocessing, creating information leakage

Question 2

A prediction system is intended to estimate failures on machines that were not present during training. Which resampling strategy is most appropriate?

A. Random row-level cross-validation

B. Group-based splitting by machine identity

C. Splitting only after model deployment

D. Using the same machine in every fold

Correct Answer: B. Group-based splitting by machine identity

Question 3

For a highly imbalanced fraud problem, which metric is generally more informative than accuracy when assessing positive-case retrieval?

A. Precision-recall AUC

B. Mean squared error only

C. R-squared

D. Explained variance of the predictors

Correct Answer: A. Precision-recall AUC

Question 4

What is the main purpose of a final test set?

A. To select preprocessing methods repeatedly during development

B. To estimate final generalization after the workflow has been finalized

C. To increase the number of hyperparameters available

D. To replace the need for a training set

Correct Answer: B. To estimate final generalization after the workflow has been finalized

Question 5

A classifier ranks cases well but assigns probabilities that are systematically too high. Which property is deficient?

A. Calibration

B. Recall

C. Feature scaling

D. Clustering stability

Correct Answer: A. Calibration

Question 6

Which situation is a clear example of temporal leakage?

A. Using a training-only median to impute missing values

B. Using a maintenance record entered after a machine failure to predict that failure

C. Using stratified folds for an imbalanced target

D. Using a regularization parameter selected by inner cross-validation

Correct Answer: B. Using a maintenance record entered after a machine failure to predict that failure

Question 7

In an unsupervised clustering workflow, why may feature scaling be necessary?

A. To guarantee that the number of clusters is correct

B. To prevent variables with larger numerical units from dominating distance calculations

C. To convert an unsupervised problem into supervised learning

D. To eliminate the need for interpreting clusters

Correct Answer: B. To prevent variables with larger numerical units from dominating distance calculations

Question 8

Which statement about SHAP or permutation-based explanations is most accurate?

A. They automatically prove that a feature causes the outcome

B. They can support interpretation but depend on the model and data structure

C. They remove the need for subgroup fairness analysis

D. They guarantee stable explanations for correlated predictors

Correct Answer: B. They can support interpretation but depend on the model and data structure

Question 9

Why might nested cross-validation be used?

A. To estimate generalization while separating hyperparameter selection from performance estimation

B. To avoid fitting any preprocessing steps

C. To ensure every observation is used only once in the entire analysis

D. To replace the target variable with clusters

Correct Answer: A. To estimate generalization while separating hyperparameter selection from performance estimation

Question 10

A model's test performance declines after deployment because customer behavior has changed. This is best described as:

A. Concept drift or distribution shift

B. Perfect calibration

C. Duplicate removal

D. A reduction in dimensionality

Correct Answer: A. Concept drift or distribution shift

Programming for Data Science (Python/R)
Chapter 12 of 15