Machine Learning Basics

Chapter 11
Applied Machine-Learning Workflows and Tools

Bachelors Program in AI & ML
Machine Learning Basics   •   Bachelor

Applied Machine-Learning Workflows and Tools

Introduction and Context

Machine learning becomes valuable when a model is integrated into a reliable process for acquiring data, preparing inputs, training algorithms, evaluating results, deploying predictions, and monitoring performance. A notebook that produces an impressive accuracy score is not, by itself, a dependable machine-learning system. Professional practice requires reproducibility, traceability, appropriate evaluation, and an explicit connection between technical outputs and operational decisions.

This chapter presents a practical workflow using Python, NumPy, pandas, scikit-learn, notebooks, and version-control tools. It also examines how the workflow changes across forecasting, recommendation, text classification, image analysis, and anomaly detection. The emphasis is not on memorizing library commands, but on understanding why each stage exists, how errors arise, and how an experiment can be made credible and repeatable.

Learning Objectives

By the end of this chapter, students should be able to:

1. Describe the stages of a reproducible machine-learning workflow.

2. Use NumPy and pandas for numerical and tabular data preparation.

3. Build training and evaluation pipelines with scikit-learn.

4. Prevent data leakage and select suitable validation strategies.

5. Track experiments using version control and documented configurations.

6. Adapt machine-learning workflows to forecasting, recommendation, text, image, and anomaly-detection problems.

7. Evaluate technical, ethical, operational, and governance risks.

1. The Machine-Learning Workflow as a System

A typical workflow contains the following linked stages:

1. Problem formulation: define the decision, prediction target, users, constraints, and success criteria.

2. Data acquisition: collect data and document its source, permissions, time period, and structure.

3. Data understanding: inspect distributions, missing values, duplicates, outliers, labels, and possible biases.

4. Data splitting: separate training, validation, and test information using a method appropriate to the problem.

5. Preprocessing and feature engineering: transform raw observations into model-ready representations.

6. Model training: fit one or more candidate algorithms using only training information.

7. Evaluation: measure performance with metrics aligned with the application.

8. Error analysis: examine failures by class, time period, population, or operating condition.

9. Deployment: expose the model through an application, batch job, or decision-support system.

10. Monitoring and maintenance: detect changes in data, performance, fairness, and system reliability.

These stages are iterative rather than strictly linear. Error analysis may reveal that labels are ambiguous; monitoring may show that new categories require feature redesign. A professional workflow therefore treats a model as one component of a larger sociotechnical system.

Exploratory and Confirmatory Work

Exploratory data analysis is used to discover structure and generate hypotheses. It may include plots, summary statistics, correlations, and group comparisons. Confirmatory evaluation tests a previously specified procedure on data that were not used to make modeling decisions. If the test set is repeatedly inspected to choose features or algorithms, it ceases to be an unbiased final test set. This distinction is central to credible evaluation.

2. Python Tools for Data Work

NumPy

NumPy provides multidimensional arrays and vectorized numerical operations. An array has a shape, data type, and often a memory layout. Vectorization applies operations to entire arrays rather than writing explicit Python loops, improving clarity and frequently improving speed.

For an array of observations, `axis=0` generally refers to operations down rows, producing one result per column; `axis=1` refers to operations across columns, producing one result per row. Confusing these axes can silently produce incorrect features.

Important practices include:

  • inspect `shape` and `dtype` before computation;
  • distinguish integer, floating-point, Boolean, and object data;
  • avoid unintended broadcasting;
  • use a fixed random generator when random sampling is required;
  • treat missing values explicitly rather than allowing them to propagate unnoticed.

A random seed makes a particular pseudorandom sequence repeatable, but it does not guarantee identical results across all hardware, library versions, or parallel execution settings.

pandas

pandas represents tabular data with a `DataFrame` and labeled one-dimensional data with a `Series`. It is useful for reading files, joining tables, grouping observations, reshaping data, parsing dates, and inspecting quality.

A robust initial inspection commonly includes:

```python

import pandas as pd

df = pd.read_csv("transactions.csv")

print(df.shape)

print(df.dtypes)

print(df.isna().sum())

print(df.duplicated().sum())

print(df.describe(include="all"))

```

A join must be checked carefully. Joining customer records to transactions on a non-unique customer identifier can multiply rows and change the target distribution. Analysts should verify key uniqueness, row counts, and the intended join type.

Date and categorical variables deserve special attention. Dates should be parsed with an explicit interpretation of time zones where relevant. Categories should be checked for inconsistent spelling, hidden whitespace, and previously unseen values. Aggregations must also respect the prediction time: information created after the prediction point must not be used as a feature.

Notebooks and Scripts

Notebooks support interactive exploration, visualization, and explanation. They are excellent for investigation but can become non-reproducible when cells are executed out of order, state remains hidden, or data are loaded from undocumented locations. A professional project commonly uses notebooks for exploration and modules or scripts for reusable data preparation, training, and evaluation.

A good notebook should:

  • state its purpose and data version;
  • execute from top to bottom in a clean kernel;
  • avoid hard-coded personal paths and secrets;
  • record software versions and configuration;
  • separate exploration from final evaluation;
  • save outputs with descriptive names and metadata.

3. Reproducible Preprocessing and Modeling

Data Splitting

For independent and identically distributed observations, a random train-test split may be suitable. If a dataset contains repeated observations from the same person, household, machine, or document, related observations should usually remain in the same partition. Otherwise, the model may memorize entity-specific information.

For time-dependent data, random splitting can allow future information to influence training. A chronological split is generally more defensible: earlier observations are used for training and later observations for validation or testing.

A test set should be held back until model selection and major design decisions are complete. When data are limited, cross-validation estimates performance by repeatedly training on subsets and validating on the remaining subset. In k-fold cross-validation, the data are divided into k folds; each fold serves once as validation while the other k−1 folds serve as training. The reported score is commonly the mean across folds, accompanied by variation such as the standard deviation. Cross-validation does not remove bias caused by leakage or an unrepresentative dataset.

Leakage

Data leakage occurs when information unavailable at prediction time enters the modeling process. Examples include:

  • scaling the complete dataset before splitting;
  • imputing values using statistics calculated from test data;
  • using a post-outcome field as a feature;
  • allowing duplicate individuals in both training and test partitions;
  • selecting features after repeatedly examining the test score.

Leakage often produces unusually strong results that fail in deployment. The remedy is to fit every learned transformation only on the training portion and apply it unchanged to validation or test data.

scikit-learn Pipelines

A scikit-learn `Pipeline` chains transformations and an estimator so that cross-validation fits transformations within each training fold. For mixed data, `ColumnTransformer` can apply different operations to numeric and categorical columns.

```python

from sklearn.compose import ColumnTransformer

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import StandardScaler, OneHotEncoder

from sklearn.impute import SimpleImputer

from sklearn.linear_model import LogisticRegression

numeric = ["age", "income"]

categorical = ["region", "device"]

num_pipe = Pipeline([

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

("scale", StandardScaler())

])

cat_pipe = Pipeline([

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

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

])

preprocess = ColumnTransformer([

("num", num_pipe, numeric),

("cat", cat_pipe, categorical)

])

model = Pipeline([

("preprocess", preprocess),

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

])

```

Standardization transforms a numeric variable using

`z = (x − μ) / σ`,

where `x` is an observed value, `μ` is the training mean, and `σ` is the training standard deviation. The transformation is appropriate when a model is sensitive to feature scale, provided that `σ` is nonzero. The mean and standard deviation must be estimated from training data only. Tree-based models generally do not require scaling, although preprocessing may still be needed for missing or categorical values.

One-hot encoding represents a categorical variable using indicator columns. With `handle_unknown="ignore"`, an unseen category at prediction time does not cause a failure; however, it receives no learned category-specific contribution. This behavior should be understood and monitored.

Hyperparameter Search

A hyperparameter is selected before or around training, such as a tree depth or regularization strength. A parameter is learned from training data, such as a regression coefficient. Search procedures such as `GridSearchCV` or randomized search compare candidate settings using cross-validation. The search itself must be placed inside a sound validation design. For high-stakes or highly tuned systems, nested cross-validation can separate hyperparameter selection from performance estimation.

4. Metrics and Error Analysis

Metric choice expresses what counts as a useful prediction.

For binary classification, let TP, TN, FP, and FN denote true positives, true negatives, false positives, and false negatives. Accuracy is

`(TP + TN) / (TP + TN + FP + FN)`.

Precision is `TP / (TP + FP)`, when the denominator is nonzero, and recall is `TP / (TP + FN)`, when the denominator is nonzero. 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 of precision and recall:

`F1 = 2PR / (P + R)`,

where P is precision and R is recall, assuming `P + R > 0`.

Accuracy can be misleading with class imbalance. For example, a system that identifies a rare disease in 1% of patients could obtain 99% accuracy by predicting “healthy” for everyone while detecting no disease. Precision-recall analysis, class-specific recall, cost-sensitive evaluation, and calibration may be more relevant.

For regression, mean absolute error is

`MAE = (1/n) Σ |yᵢ − ŷᵢ|`,

and root mean squared error is

`RMSE = sqrt[(1/n) Σ (yᵢ − ŷᵢ)²]`,

where `yᵢ` is the observed value, `ŷᵢ` is the prediction, and n is the number of observations. Both have the units of the target. RMSE penalizes large errors more strongly than MAE because the errors are squared before averaging.

Metrics should be supplemented with residual plots, confusion matrices, threshold analysis, subgroup performance, and examples of incorrect predictions. A single aggregate score rarely explains whether a model is safe or useful.

5. Experimentation, Version Control, and MLOps Foundations

Reproducibility means that another person can reconstruct the data, code, environment, configuration, and evaluation procedure sufficiently to obtain the same or appropriately comparable result. It is stronger than simply setting a random seed.

A practical project may contain:

```text

project/

data/ # often references or small reproducible samples

notebooks/

src/

tests/

configs/

reports/

requirements.txt

README.md

```

Git records changes to code and configuration. Commits should describe meaningful changes, such as “add chronological validation,” rather than vague messages such as “update.” Large raw datasets and secrets should not be committed to a public repository. Data versioning or a catalog should record data snapshots, schemas, licenses, and transformations.

An experiment record should include:

  • dataset identifier and time range;
  • feature definition and target definition;
  • train-validation-test method;
  • code commit or release identifier;
  • library and hardware environment;
  • random seeds and parallelization settings;
  • model and hyperparameters;
  • metrics, confidence or variation estimates, and prediction threshold;
  • known limitations and error-analysis results.

Environment files such as `requirements.txt`, lock files, or container specifications reduce dependency drift. Tests should check data schemas, expected columns, transformation behavior, and prediction interfaces. Continuous integration can run these tests whenever code changes.

Deployment may be batch-based or real-time. A deployed model needs input validation, logging, access control, rollback procedures, and monitoring. Monitoring should distinguish data drift, concept drift, infrastructure failure, and changes in label availability. Retraining should be triggered by evidence and governance rules rather than by an automatic schedule alone.

6. Domain-Specific Workflows

Forecasting

Forecasting predicts future values from information available before the forecast origin. A time series may contain trend, seasonality, calendar effects, promotions, and external variables. Features can include lagged observations, rolling summaries, and calendar indicators, but each must be computed using historical information only.

Validation should preserve temporal order. Rolling-origin evaluation trains on an initial period, predicts a later period, then expands or moves the training window and repeats. A naive baseline, such as the previous observation or seasonal previous observation, is essential. A complex model is valuable only if it consistently improves on a defensible baseline and does so at an operationally meaningful horizon.

Forecast intervals communicate uncertainty. A point prediction alone may cause decision-makers to overcommit inventory or staffing. Forecast evaluation should be reported separately by horizon and season where appropriate.

Recommendation

Recommendation systems estimate which items a user may prefer. Collaborative filtering uses interactions among users and items; content-based methods use item or user attributes; hybrid systems combine both. Common problems include sparse interaction matrices, new users or items (the cold-start problem), popularity bias, feedback loops, and exposure bias.

Offline ranking metrics such as precision@k, recall@k, and normalized discounted cumulative gain can be useful, but they do not fully measure user satisfaction or business impact. A recommendation shown to a user affects what the user can click, so observed clicks are not a neutral record of preference. Online experiments must be carefully designed, with safeguards for user welfare and stable business operations.

Text

Text workflows typically include tokenization, normalization, feature representation, model training, and evaluation. A bag-of-words or term-frequency inverse-document-frequency (TF-IDF) representation is a strong educational baseline. TF-IDF gives greater weight to terms frequent in a document but relatively uncommon across the document collection. Word or sentence embeddings can represent semantic relationships, but they may carry biases from their training data.

Text splitting must consider near-duplicate documents, authors, time, and user identity. If related documents appear in both training and test sets, performance may be overstated. Text systems require attention to privacy, harmful content, dialect variation, and explainability.

Images

Image workflows include image decoding, resizing or cropping, normalization, augmentation, model training, and error analysis. A random image split can leak information when multiple images come from the same patient, object, video, or photographer. Group-based splitting is preferable when identity-related similarity is strong.

Augmentation should represent plausible changes and must not alter the label. For example, a horizontal flip may be inappropriate when left-versus-right orientation is clinically meaningful. Evaluation should inspect confusion by class, image quality, demographic group, and acquisition device.

Anomaly Detection

Anomaly detection identifies observations that differ from expected behavior. In many applications, anomalies are rare and labels are incomplete. A threshold is therefore a decision parameter, not an objective truth supplied automatically by an algorithm. Isolation-based, density-based, reconstruction-based, and statistical methods make different assumptions about normality.

Evaluation can use precision among investigated alerts, detection delay, false-alert workload, and missed-event cost. Randomly splitting data may be inappropriate if anomalies occur in episodes or if the future differs from the past. Human review and feedback loops are often essential.

7. Illustrative Case: Predicting Customer Churn

Background and Problem

Consider an illustrative telecommunications company seeking to identify customers at risk of cancelling service within the next 30 days. The company wants to contact selected customers with retention offers. A false positive consumes promotional budget and may annoy a loyal customer; a false negative loses an opportunity to intervene.

Analysis

The data contain customer attributes, monthly charges, service history, support contacts, and a cancellation label. The prediction date is the end of each month. A major risk is leakage: a field recording a cancellation request or a final account status would reveal the outcome and must be excluded. Another risk is repeated monthly records for the same customer. A customer-level split prevents the same customer from appearing in both training and testing.

Because the business operates over time, an illustrative design uses earlier months for training, a later month for validation, and the most recent month as a final test. Numeric variables are median-imputed and scaled; categorical variables are imputed and one-hot encoded inside a pipeline. A regularized logistic-regression model provides an interpretable baseline, while a tree-based model provides a nonlinear comparison.

Approach and Evaluation

The team first compares both models with a no-intervention baseline and reports precision, recall, expected contact volume, and retention outcomes. Suppose the model’s default probability threshold produces high recall but too many contacts. The threshold is therefore selected on the validation period according to the maximum number of offers the call center can make, not on the test set.

The final test is run once after the procedure is fixed. Results are examined by customer tenure, plan type, region, and language. If recall is substantially lower for a group, the team investigates data quality and structural causes rather than simply lowering that group’s threshold without governance review.

Outcome

In this illustrative case, the model is approved only as decision support. Agents receive a risk score and explanation based on recent, permitted features, while customers are not automatically denied service or charged differently. The company monitors data distributions, contact outcomes, complaints, and performance after deployment. Retraining is considered when service plans change or when performance declines.

Learning Points

The case demonstrates that model selection is only one part of the solution. Correct temporal framing, leakage prevention, capacity-aware thresholding, subgroup analysis, human oversight, and post-deployment monitoring determine whether the system creates value responsibly.

8. Limitations, Ethics, and Governance

Machine-learning outputs can reproduce historical discrimination, encode incomplete measurements, or create unequal error rates. Fairness analysis should consider the decision context, relevant groups, measurement quality, and applicable law. No single fairness metric resolves all conflicts; improving one error-rate relationship may affect another, particularly when base rates differ.

Privacy requires data minimization, lawful collection, access control, retention limits, and secure processing. Sensitive attributes may be needed for auditing even when they are not used as predictive features, but their collection must be justified and protected. Explainability should be matched to the audience and decision: a feature-importance plot is not necessarily a causal explanation.

Governance should identify a system owner, intended use, prohibited use, approval criteria, documentation requirements, incident procedures, and human-override rules. High-impact applications may require formal risk assessments, impact assessments, audit trails, and compliance with sector-specific regulation. Open-source tools reduce development cost but do not transfer accountability to the library authors.

Technical limitations include small or biased samples, unstable labels, distribution shift, missing feedback, computational cost, and model maintenance. A model can be statistically accurate yet operationally useless if predictions arrive too late, cannot be acted upon, or impose excessive review workload.

9. Contemporary Developments and Best Practices

Modern workflows increasingly use experiment-tracking systems, data catalogs, feature stores, automated testing, model registries, containers, and reproducible deployment pipelines. Foundation and generative models have expanded text and image capabilities, but they also introduce risks involving hallucination, copyright, privacy, prompt injection, and high computational cost. Retrieval, fine-tuning, or transfer learning should be selected according to the task, data, latency, and governance requirements rather than novelty.

Recommended practices are:

1. Begin with a clearly defined decision and a simple baseline.

2. Establish the prediction timestamp and permissible information set.

3. Split data according to independence, groups, or time.

4. Put learned preprocessing inside the validation pipeline.

5. Record code, data, environment, configuration, and metrics.

6. Evaluate more than one metric and inspect actual errors.

7. Test performance across relevant populations and operating conditions.

8. Design deployment, monitoring, rollback, and retraining before launch.

9. Protect privacy and document intended and prohibited uses.

10. Prefer the simplest model that meets the operational requirement.

Chapter Summary

An applied machine-learning system is a reproducible workflow rather than an isolated algorithm. Python, NumPy, and pandas support numerical and tabular data work, while notebooks provide interactive investigation and scripts provide maintainable execution. scikit-learn pipelines prevent preprocessing leakage and make transformations part of model selection. Sound evaluation depends on appropriate splits, cross-validation, metrics, baselines, and error analysis. Version control, environment specifications, experiment records, tests, and monitoring make results traceable and maintainable. Forecasting requires temporal validation; recommendation requires attention to exposure and feedback; text and image systems require careful representation and group-aware splitting; anomaly detection requires realistic alert and review measures. Ethical governance, privacy, fairness, human oversight, and operational constraints are integral to professional machine-learning practice.

Key Terms

Anomaly detection: Identification of observations that differ substantially from expected behavior.

Baseline: A simple reference method used to judge whether a more complex model adds value.

Cross-validation: Repeated partitioning of training data to estimate performance and compare models.

Data drift: Change in the distribution of input data over time.

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

Data versioning: Recording and managing specific datasets or data snapshots used in experiments.

Feature engineering: Construction or transformation of input variables for a model.

Hyperparameter: A modeling choice specified outside the parameter-learning process.

Imputation: Replacement of missing values using a defined procedure.

Monitoring: Ongoing observation of data, predictions, performance, and system health.

One-hot encoding: Representation of a categorical variable with indicator columns.

Pipeline: Ordered sequence of preprocessing steps and a predictive estimator.

Precision: Proportion of predicted positive cases that are truly positive.

Recall: Proportion of actual positive cases that are detected.

Reproducibility: Ability to reconstruct an experiment and obtain comparable results.

Temporal validation: Evaluation that preserves the order of time-dependent observations.

Test set: Data held back for final evaluation after model development decisions.

TF-IDF: Text representation weighting terms by document frequency and collection rarity.

Threshold: Decision boundary used to convert a score or probability into an action or class.

Version control: Systematic recording of changes to code and related project artifacts.

Review Questions

Short Answer Questions

1. Why should learned preprocessing steps be placed inside a scikit-learn pipeline?

2. Distinguish a validation set from a final test set.

3. What is data leakage? Give one example.

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

5. State two purposes of version control in a machine-learning project.

Long Answer Questions

1. Explain the stages of a reproducible machine-learning workflow from problem formulation through monitoring.

2. Compare random splitting, group-based splitting, and chronological splitting, including when each is appropriate.

3. Explain how NumPy, pandas, notebooks, and scikit-learn contribute different capabilities to an ML project.

4. Discuss the strengths and limitations of precision, recall, F1, MAE, and RMSE.

5. Describe the principal ethical and governance requirements for deploying a predictive model.

Analytical / Application Questions

1. A hospital has five images per patient and wants to classify disease. Explain how you would split the data, preprocess images, evaluate the model, and detect leakage.

2. A retailer forecasts weekly demand but uses a randomly shuffled train-test split. Analyze the risks and propose a better validation and baseline strategy.

3. An anomaly detector produces 1,000 alerts daily, of which only 20 are investigated as useful. Propose technical and operational metrics and explain how you would decide whether to change the threshold or the model.

Multiple Choice Questions with Answers

Question 1

Which practice most directly prevents preprocessing leakage during cross-validation?

A. Scaling the complete dataset before cross-validation

B. Placing preprocessing and the estimator inside a pipeline

C. Increasing the number of model parameters

D. Using the test set to select features

Correct Answer: B. Placing preprocessing and the estimator inside a pipeline

Question 2

Which split is generally most appropriate for evaluating a model that predicts next month's demand?

A. A chronological split that preserves time order

B. A split based only on alphabetical order of product names

C. A random split that mixes future and past observations without restriction

D. A split made after examining the final test score

Correct Answer: A. A chronological split that preserves time order

Question 3

If TP = 30 and FP = 10, what is precision?

A. 0.25

B. 0.50

C. 0.75

D. 0.90

Correct Answer: C. 0.75

Question 4

What is the main purpose of a final test set?

A. To fit the imputer repeatedly during feature engineering

B. To provide a final estimate after model-development decisions are complete

C. To increase the number of training observations

D. To replace the need for a baseline

Correct Answer: B. To provide a final estimate after model-development decisions are complete

Question 5

Which statement about RMSE is correct?

A. It has the squared units of the target

B. It ignores large errors

C. It has the same units as the target and penalizes large errors relatively strongly

D. It is defined only for classification

Correct Answer: C. It has the same units as the target and penalizes large errors relatively strongly

Question 6

Why should multiple records from the same customer often remain in one data partition?

A. To guarantee every partition has the same number of rows

B. To prevent customer-specific information from leaking across partitions

C. To eliminate all missing values

D. To make one-hot encoding unnecessary

Correct Answer: B. To prevent customer-specific information from leaking across partitions

Question 7

Which issue is especially important in recommendation systems?

A. Exposure bias caused by only observing interactions with displayed items

B. The impossibility of using categorical variables

C. The requirement that all users have identical preferences

D. The absence of any cold-start problem

Correct Answer: A. Exposure bias caused by only observing interactions with displayed items

Question 8

What does `handle_unknown="ignore"` in one-hot encoding help address?

A. Unknown categories appearing at prediction time

B. Numerical overflow in matrix multiplication

C. Duplicate rows in the raw dataset

D. Incorrect chronological ordering

Correct Answer: A. Unknown categories appearing at prediction time

Question 9

Which artifact most directly records the exact code version used for an experiment?

A. A model's confusion matrix

B. A Git commit identifier

C. A feature's mean value

D. A prediction threshold alone

Correct Answer: B. A Git commit identifier

Question 10

For anomaly detection, why is alert workload an important evaluation measure?

A. Because anomalies are always labeled perfectly

B. Because a mathematically accurate detector is useful regardless of review capacity

C. Because excessive false alerts can make the system operationally unusable

D. Because workload replaces the need for any validation data

Correct Answer: C. Because excessive false alerts can make the system operationally unusable

Machine Learning Basics
Chapter 11 of 15