7. Machine Learning and AI FrameworksIntroduction and ContextMachine learning and artificial intelligence frameworks provide the software foundations through which data scientists design, train, evaluate, explain, and deploy models. A framework is more than a collection of algorithms. It also includes interfaces for data preparation, numerical computation, optimization, model persistence, visualization, distributed execution, hardware acceleration, and production integration. For postgraduate data science practice, the central question is not simply which library can train a model. The more important question is how a complete, reliable, and responsible workflow can be constructed. A high-performing model may still be unsuitable if its evaluation is affected by data leakage, if it cannot be explained to stakeholders, if its predictions are unfair, or if it cannot be maintained after deployment. This chapter examines contemporary frameworks used for supervised learning, unsupervised learning, and deep learning. It emphasizes workflow design: separating training and testing data, constructing reproducible pipelines, applying cross-validation, tuning hyperparameters, interpreting predictions, and governing models throughout their life cycle. The discussion connects technical choices with organizational and ethical responsibilities. Learning ObjectivesAfter completing this chapter, the reader should be able to: 1. distinguish major machine learning and deep-learning frameworks and select them appropriately; 2. design supervised and unsupervised learning workflows using reusable pipelines; 3. explain the role of train–validation–test splits, cross-validation, and hyperparameter tuning; 4. compare CPU-oriented, GPU-oriented, and distributed approaches to model development; 5. evaluate models using suitable performance measures rather than accuracy alone; 6. apply explainability, fairness, reproducibility, and governance practices; 7. diagnose common implementation errors and propose improvements; 8. assess emerging developments in machine learning tooling and deployment. The Framework LandscapeA modern data science environment usually contains several complementary layers. A numerical layer, such as NumPy, provides arrays and mathematical operations. A tabular data layer, commonly represented by pandas or Polars, supports data manipulation. Visualization libraries assist exploratory analysis and model diagnostics. Machine learning libraries provide estimators, preprocessing components, metrics, and model-selection utilities. Deep-learning frameworks provide tensor computation, automatic differentiation, neural-network abstractions, and accelerator support. Finally, deployment and governance tools package models, monitor predictions, and manage versions. No single framework is optimal for every task. Scikit-learn is particularly effective for classical algorithms, structured data, rapid experimentation, and transparent pipelines. PyTorch and TensorFlow support neural networks and large-scale numerical computation. Keras offers a comparatively high-level interface for constructing neural networks and may operate with different computational backends. XGBoost, LightGBM, and CatBoost are specialized gradient-boosting libraries that are often effective for tabular prediction. Spark MLlib is relevant when data or computation must be distributed across a cluster. Framework selection should therefore consider the data modality, scale, algorithmic requirements, available hardware, deployment environment, team expertise, interpretability needs, and maintenance burden. A sophisticated framework is not automatically a better choice. For a medium-sized credit-risk dataset, a well-engineered gradient-boosting model may be more appropriate than a deep neural network because it is faster to develop, easier to audit, and often competitive in predictive performance. A Unified Learning WorkflowA machine learning project begins with a problem definition, not with a library call. The analyst must specify the prediction target, the unit of analysis, the decision that the prediction will support, the time at which information is available, and the consequences of different errors. These decisions determine the data design and evaluation criteria. Supervised learningIn supervised learning, a model learns a relationship between input variables, represented by a feature matrix X, and a target variable y. Classification predicts categories such as fraud or non-fraud; regression predicts numerical quantities such as demand or revenue. Common estimators include linear and logistic regression, decision trees, random forests, support vector machines, nearest-neighbour methods, gradient boosting, and neural networks. The workflow generally includes data validation, exploratory analysis, preprocessing, feature construction, model fitting, validation, tuning, final testing, interpretation, and deployment. Preprocessing may involve imputation, scaling, encoding categorical variables, text vectorization, or image normalization. The transformation must be learned only from the training data. For example, calculating a mean for imputation using the complete dataset allows information from the test set to influence training and produces an optimistic estimate of generalization. Unsupervised learningUnsupervised learning operates without a labelled target. Clustering methods such as k-means, hierarchical clustering, and density-based methods seek structure among observations. Dimensionality-reduction techniques such as principal component analysis (PCA) create lower-dimensional representations while retaining selected information. Anomaly detection identifies observations that differ substantially from an expected pattern. Unsupervised results require substantive interpretation. A cluster is not automatically a meaningful customer segment, and a two-dimensional projection is not proof that the original data have only two important dimensions. Analysts should examine stability, sensitivity to preprocessing, domain plausibility, and the consequences of acting on the discovered structure. Deep learningDeep learning uses multilayer neural networks to learn representations as well as predictive functions. A typical network contains an input layer, hidden layers, activation functions, and an output layer. Training adjusts parameters by minimizing a loss function through gradient-based optimization and backpropagation. Frameworks such as PyTorch and TensorFlow provide tensors, automatic differentiation, optimizers, neural-network modules, data loaders, checkpointing, and accelerator support. Convolutional architectures are suited to spatial data such as images; recurrent and sequence-oriented architectures can process ordered data; transformer architectures use attention mechanisms and are widely applied to language, vision, and multimodal tasks. Transfer learning allows a model trained on a large dataset to be adapted to a related task, often reducing data and computational requirements. Deep learning introduces additional design choices: network depth, width, activation functions, batch size, learning rate, regularization, initialization, and training duration. These choices are hyperparameters rather than learned parameters. A validation set is required to select them without contaminating the final test evaluation. Pipelines, Validation, and ReproducibilityA pipeline represents a sequence of transformations and an estimator as one coherent object. In scikit-learn, a pipeline can combine an imputer, a scaler, an encoder, and a classifier. During cross-validation, each transformation is fitted separately within each training fold. This prevents leakage and ensures that the evaluation reflects the process used on unseen data. A pipeline also improves maintainability. It makes the order of operations explicit, allows the complete workflow to be saved, and reduces the risk that production code applies preprocessing differently from training code. Column-specific transformations can be managed through a column transformer, which applies numerical and categorical operations to their appropriate fields. A basic data partition consists of training and test sets. The training data fit the model; the test data provide a final estimate of performance. When model choices are made repeatedly using the test set, the test set ceases to be genuinely independent. Cross-validation addresses the need for a more reliable development estimate by dividing training data into folds, fitting on some folds, and validating on the remaining fold in rotation. In k-fold cross-validation, the average validation score is often reported together with its variation. Stratified cross-validation maintains approximate class proportions for classification. Time-series validation must respect chronological order, because randomly mixing future observations into training would create unrealistic information flow. Grouped cross-validation prevents records belonging to the same person, household, patient, or organization from appearing in both training and validation folds. Nested cross-validation is useful when unbiased performance estimation is especially important. An inner loop selects hyperparameters, while an outer loop estimates performance of that selection process. It is computationally more expensive but reduces the bias caused by evaluating a model on data used to tune it. Reproducibility depends on more than setting a random seed. It requires recording data versions, feature definitions, software dependencies, hardware assumptions, configuration parameters, model artifacts, and evaluation code. Random seeds improve repeatability but do not guarantee identical results across all libraries, devices, or parallel execution settings. Model Selection and Hyperparameter TuningParameters are learned from data; hyperparameters are specified before or around training. Examples include the regularization strength in logistic regression, tree depth in a random forest, the number of clusters in k-means, and the learning rate or batch size in a neural network. Grid search evaluates a predefined Cartesian product of hyperparameter values. It is straightforward but can be wasteful when many parameters have little influence. Random search samples configurations and can explore influential dimensions more efficiently. Bayesian optimization uses previous trials to choose promising configurations. Successive-halving methods allocate greater computational resources to promising candidates while stopping weak candidates early. Tuning must use a metric aligned with the decision. Accuracy may be misleading for highly imbalanced data. Precision measures the proportion of predicted positives that are correct, whereas recall measures the proportion of actual positives detected. The F1 score combines precision and recall through their harmonic mean. Receiver operating characteristic area under the curve (ROC-AUC) assesses ranking across thresholds, while precision–recall curves are often more informative when positive cases are rare. Regression measures include mean absolute error, root mean squared error, and coefficient of determination. Each has a different interpretation and sensitivity to errors. Threshold selection is distinct from model training. A classifier may produce probabilities, after which a threshold is selected according to operational costs. In a medical screening context, a lower threshold may be preferred to reduce missed cases, while a costly intervention may require greater precision. Probabilities should also be checked for calibration when they are used as risk estimates. Explainability and Model InterpretationExplainability helps stakeholders understand how a model behaves, but it does not transform a model into a fair or correct system. Global interpretation examines overall patterns, such as feature importance or partial dependence. Local interpretation examines the reason for an individual prediction. Permutation importance measures the deterioration in performance after a feature’s values are shuffled. It is model-agnostic but can be misleading when features are strongly correlated. Partial dependence describes the average predicted response as a feature changes, although interactions and unrealistic combinations of features must be considered. SHAP-based methods attribute a prediction to features relative to a reference expectation, while local surrogate methods approximate a complex model near one observation. For inherently interpretable models, coefficients, decision rules, monotonic relationships, and generalized additive structures may provide more direct explanations. Explanations should be presented with their scope and limitations. A feature associated with a prediction is not necessarily a causal factor, and post hoc explanations may be unstable across similar observations. Responsible AI and Model GovernanceResponsible machine learning requires attention to the complete sociotechnical system. Important risks include discriminatory outcomes, privacy violations, insecure data handling, hidden proxies for protected characteristics, automation bias, and inadequate recourse for affected individuals. Fairness assessment should begin by identifying the relevant groups, legal context, decision consequences, and acceptable definitions of fairness. Measures may include demographic parity, equal opportunity, equalized odds, and group-specific calibration. These criteria can conflict, particularly when groups have different base rates. Consequently, fairness cannot be solved by selecting a metric mechanically; it requires institutional and normative judgment. Privacy practices include data minimization, access controls, retention limits, de-identification where appropriate, and careful assessment of re-identification risk. Sensitive data should not be used merely because they improve predictive performance. Documentation such as dataset sheets, model cards, decision logs, and impact assessments supports accountability. Governance also includes monitoring after deployment. Data distributions may drift, relationships between inputs and outcomes may change, and performance may deteriorate for specific subgroups. Monitoring should cover input quality, prediction distributions, calibration, latency, failures, fairness indicators, and eventual outcomes where labels become available. A model should have an owner, an escalation process, a version history, and defined conditions for retraining or withdrawal. An Illustrative Case: Predicting Customer Loan DefaultBackgroundA fictional regional lender wishes to improve early identification of customers at risk of missing a loan payment. The proposed system will support, but not automatically determine, outreach and repayment assistance. Situation and problemThe historical dataset contains repayment outcomes, income bands, loan characteristics, payment history, and application information. The positive class is relatively uncommon. Some records contain multiple monthly observations for the same customer, and the data were collected over several years. A naïve approach randomly splits all rows, scales the full dataset before splitting, and selects the model with the highest accuracy. This design is problematic: observations from the same customer may occur in both training and test data; future information may be included in earlier records; and class imbalance may conceal poor detection of defaults. Analysis and approachThe data science team first defines the prediction time and excludes variables that would only be known after that time. It creates a customer-level, time-aware split, using earlier periods for training and a later period for testing. Numerical missing values, categorical encoding, and scaling are placed inside a pipeline. Grouped and time-respecting validation are used during development. The team compares regularized logistic regression, a gradient-boosting model, and a calibrated model. Evaluation emphasizes recall at a manageable outreach capacity, precision, calibration, subgroup performance, and expected operational cost. Hyperparameters are tuned only within the training process. SHAP-style local explanations and global summaries are reviewed, but the team avoids claiming that influential variables are causes of default. The governance review identifies income and location-related variables as potentially sensitive or proxy variables. Performance is therefore examined across relevant groups, and the lender establishes a human review process, customer notification procedures, and an appeal mechanism. The model is used to prioritize supportive contact rather than deny credit automatically. OutcomeThe resulting workflow produces a more credible estimate of future performance than the initial random-split experiment. It also gives operations staff a documented threshold, a list of excluded features, monitoring indicators, and procedures for investigating drift and unequal error rates. The exact numerical improvement is not assumed; it must be established through the lender’s validated evaluation and subsequent monitoring. Learning pointsThe case demonstrates that a framework cannot compensate for an unclear target, flawed temporal design, or unsuitable metric. Leakage prevention, pipeline construction, subgroup evaluation, interpretability, and human governance are integral parts of the technical solution. Common Failure Modes and Practical SafeguardsA frequent mistake is preprocessing before data splitting. The safeguard is to place learned transformations inside a pipeline. Another is selecting a model using repeated test-set experimentation; a locked final test set or nested validation is preferable. Random cross-validation is inappropriate for time-dependent data or grouped observations. Accuracy-only reporting is inadequate for imbalanced or high-consequence decisions. Overfitting may arise from excessive model complexity, repeated tuning, high-dimensional features, or insufficient data. Regularization, early stopping, dropout, simpler models, feature selection, and stronger validation can help, but no technique eliminates the need for independent evaluation. In deep learning, unstable training may result from unsuitable learning rates, poor normalization, inadequate monitoring, or hardware-related reproducibility issues. Operational failures are also common. A model may depend on a feature unavailable at prediction time, produce outputs without calibrated probabilities, or fail when a new category appears. Schema validation, robust handling of unknown categories, logging, automated tests, and shadow or staged deployment reduce these risks. Emerging DirectionsMachine learning tooling is moving toward unified data and model pipelines, automated experiment tracking, model registries, containerized deployment, and managed accelerator environments. Foundation models and transfer learning increasingly allow organizations to adapt pretrained systems rather than train from the beginning. Retrieval-augmented systems combine model generation with access to external documents, while multimodal frameworks integrate text, image, audio, and structured data. These developments increase capability but also introduce new concerns: data provenance, model licensing, prompt and retrieval security, hallucinated outputs, energy consumption, and evaluation beyond benchmark accuracy. Automated machine learning can accelerate candidate generation, yet human oversight remains necessary for target definition, validation design, fairness analysis, and deployment decisions. The direction of the field is therefore not toward removing professional judgment, but toward making experimentation and deployment more systematic. Best Practices for Professional WorkA robust framework-based workflow should: - define the decision, prediction time, target, and cost of errors before modelling;
- use versioned data, code, dependencies, and model artifacts;
- keep preprocessing and estimation within reproducible pipelines;
- select validation schemes that reflect time, groups, and deployment conditions;
- tune against an appropriate metric and preserve an untouched final test set;
- compare interpretable baselines with more complex alternatives;
- examine calibration, subgroup performance, robustness, and drift;
- document assumptions, exclusions, known limitations, and intended use;
- deploy gradually with monitoring, rollback, and human escalation;
- treat privacy, fairness, security, and accountability as design requirements.
Chapter SummaryMachine learning and AI frameworks support the full path from data preparation to deployment, but framework choice must follow the problem, data, scale, hardware, interpretability needs, and governance context. Classical libraries such as scikit-learn are valuable for structured workflows, while deep-learning frameworks such as PyTorch and TensorFlow support tensor computation, automatic differentiation, and accelerator-based neural-network training. Gradient-boosting and distributed frameworks extend the available options. Reliable practice depends on leakage-resistant pipelines, suitable train–validation–test designs, cross-validation, hyperparameter tuning, and metrics aligned with decisions. Explainability helps interpret model behaviour, but it does not establish causality or fairness. Responsible AI requires privacy protection, subgroup analysis, documentation, monitoring, human oversight, and mechanisms for correction. The professional data scientist therefore evaluates not only whether a model predicts well, but also whether it is valid, maintainable, understandable, secure, and appropriate for the people and organizations affected by it. Key TermsFramework: A software environment providing reusable tools and abstractions for developing computational models. Estimator: An algorithmic object that learns parameters from data and produces predictions or transformations. Pipeline: An ordered, reproducible sequence of preprocessing steps and a model. Supervised learning: Learning from input data paired with known target values. Unsupervised learning: Learning patterns or structure from data without a labelled target. Deep learning: Machine learning based on multilayer neural networks that learn representations through optimization. Feature: An input variable used by a model. Hyperparameter: A configuration value selected outside the direct parameter-learning process. Cross-validation: Repeated partitioning of data into fitting and validation subsets to estimate performance. Data leakage: Unintended use of information unavailable at prediction time or from evaluation data during training. Calibration: The degree to which predicted probabilities correspond to observed frequencies. Regularization: A method that discourages excessive model complexity to reduce overfitting. Transfer learning: Adapting knowledge learned on one task or dataset to another related task. Explainability: The ability to describe or interpret how a model produces outputs. Fairness: The principled assessment and management of unequal model impacts across groups. Model drift: Change in data or relationships that reduces model validity after deployment. Model card: Documentation describing a model’s intended use, evaluation, limitations, and risks. Automatic differentiation: Computational determination of derivatives used to optimize differentiable models. Threshold: A decision boundary applied to a model score or probability. Reproducibility: The ability to repeat an analysis using recorded data, code, configurations, and dependencies. Review QuestionsShort Answer Questions1. Why is a pipeline preferable to performing preprocessing as separate ad hoc code? 2. Distinguish a model parameter from a hyperparameter, giving one example of each. 3. Why can accuracy be unsuitable for an imbalanced classification problem? 4. What is the purpose of a final untouched test set? 5. State two responsibilities involved in responsible AI governance. Long Answer Questions1. Explain how a supervised learning workflow should proceed from problem definition to deployment. 2. Compare scikit-learn, deep-learning frameworks, gradient-boosting libraries, and distributed frameworks in terms of appropriate use. 3. Discuss cross-validation, nested cross-validation, and time-aware validation, including the situations in which each is useful. 4. Explain how model explainability methods can assist professional decision-making and describe their limitations. 5. Develop a responsible AI governance plan for a high-consequence predictive model. Analytical / Application Questions1. A hospital has several records per patient collected over time. Design a validation strategy and explain why ordinary random k-fold cross-validation may be misleading. 2. A fraud model has 99.5% accuracy but detects only 10% of fraudulent transactions. Recommend a more suitable evaluation and threshold-selection approach. 3. A deployed model performs well overall but poorly for one demographic group. Analyse possible causes and propose technical, organizational, and governance responses. 22. Multiple Choice Questions with AnswersQuestion 1Which practice most directly prevents a scaler from using information in the validation fold during model development? A. Applying the scaler to the complete dataset before splitting B. Placing the scaler inside a cross-validated pipeline C. Increasing the number of neural-network layers D. Reporting accuracy instead of recall Correct Answer: B. Placing the scaler inside a cross-validated pipeline Question 2A dataset contains several observations for each patient. Which validation approach best reduces patient-level leakage? A. Randomly splitting individual rows B. Using grouped cross-validation by patient C. Removing the target variable D. Using only training accuracy Correct Answer: B. Using grouped cross-validation by patient Question 3Why is a time-aware split necessary in many forecasting problems? A. It guarantees that the model is linear B. It prevents future information from entering training for earlier predictions C. It removes the need for a validation set D. It always produces higher accuracy Correct Answer: B. It prevents future information from entering training for earlier predictions Question 4Which metric is particularly useful when positive cases are rare and the cost of false positives matters? A. Precision B. Mean squared error C. R-squared alone D. Training loss only Correct Answer: A. Precision Question 5What is the main purpose of hyperparameter tuning? A. To alter the historical labels B. To select configuration values using a validation procedure C. To guarantee causal interpretation D. To eliminate the need for testing Correct Answer: B. To select configuration values using a validation procedure Question 6Which statement best describes transfer learning? A. Training every model from random initialization only B. Using knowledge from a pretrained model for a related task C. Splitting a dataset into more than two classes D. Replacing all features with clusters Correct Answer: B. Using knowledge from a pretrained model for a related task Question 7A model’s predicted probabilities are used to allocate support resources. Which additional property should be assessed besides ranking performance? A. Calibration B. File size only C. Number of programming comments D. Random seed length Correct Answer: A. Calibration Question 8Why should a feature-importance result not automatically be interpreted as a causal explanation? A. Importance methods cannot use numerical features B. Association in predictive data does not establish that changing the feature causes the outcome C. All models are necessarily causal D. Causal analysis never uses data Correct Answer: B. Association in predictive data does not establish that changing the feature causes the outcome Question 9Which action is most appropriate when a deployed model’s performance declines because input distributions have changed? A. Ignore the change if training accuracy was high B. Monitor drift and investigate recalibration, retraining, or withdrawal C. Delete the test set D. Increase the decision threshold without analysis Correct Answer: B. Monitor drift and investigate recalibration, retraining, or withdrawal Question 10In the loan-default case, why was a human review and appeal process included? A. To make validation unnecessary B. To provide oversight and recourse for a consequential decision C. To guarantee that no errors occur D. To replace all model documentation Correct Answer: B. To provide oversight and recourse for a consequential decision
|