Introduction to AI and ML

Chapter 2
Machine Learning Fundamentals

Diploma in Artificial Intelligence and Machine Learning
Introduction to AI and ML   •   Diploma

Machine Learning Fundamentals

Introduction

Machine learning (ML) is a central area of artificial intelligence in which computer systems improve their performance by learning patterns from data. Instead of writing an explicit instruction for every possible situation, a developer supplies data, a learning objective, and an appropriate algorithm. The algorithm produces a model that can make predictions or decisions for previously unseen cases.

Machine learning is not magic and it is not identical to simply storing data. A useful ML system identifies relationships that are sufficiently stable to support generalization. Its quality depends on the data, the representation of the problem, the learning algorithm, the evaluation method, and the operational environment.

This chapter introduces the foundations required for later study of regression, classification, clustering, neural networks, and intelligent agents.

Learning Objectives

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

  • distinguish conventional programming from machine learning;
  • explain features, labels, instances, models, parameters, training, and inference;
  • distinguish parameters from hyperparameters;
  • explain generalization, overfitting, underfitting, and data leakage;
  • describe supervised, unsupervised, semi-supervised, and reinforcement learning;
  • select a suitable learning paradigm for a practical problem; and
  • identify important professional, ethical, and governance considerations.

Conventional Programming and Machine Learning

In conventional programming, a programmer specifies rules that transform inputs into outputs:

Input data + Explicit program rules → Output

For example, a tax program may contain legally defined rates and conditions. If the rules change, the program must be changed.

In machine learning, examples of inputs and, in many cases, desired outputs are used to construct the rules implicitly:

Input data + Desired outcomes + Learning algorithm → Trained model

The trained model can then be used as follows:

New input + Trained model → Prediction or decision

Consider email filtering. A conventional system might use manually written rules such as “if the subject contains a particular word, mark the message as spam.” A machine-learning system learns statistical patterns from labelled examples of spam and legitimate messages. It may use word frequencies, sender information, message structure, and other signals.

The distinction is not absolute. Production ML systems normally contain conventional software for data collection, validation, security, user interfaces, and decision thresholds. ML supplies a learned component within a larger engineered system.

When machine learning is appropriate

ML is particularly useful when:

  • the rules are difficult to state explicitly;
  • the problem involves large or changing data sets;
  • patterns can be learned from historical examples; or
  • predictions are needed for cases not observed during development.

It is less suitable when the task requires an exact, transparent rule that is already known, when reliable training data are unavailable, or when errors have unacceptable consequences without human review.

The Data Vocabulary of Machine Learning

A dataset is a structured collection of observations. Each observation is commonly called an instance, example, record, or sample. The terms are often used interchangeably, although particular fields may give them more specialised meanings.

Features

A feature is an input variable used by a model. In a house-price problem, features might include floor area, number of bedrooms, location, and building age. Features can be numerical, categorical, textual, visual, audio-based, or derived from other data.

A feature vector represents the input values for one instance. It is often written as x = (x1, x2, …, xd), where d is the number of features and xj is the value of feature j. A feature must be available at the time a prediction is required; otherwise, it cannot be used operationally.

Labels and targets

A label is the desired output associated with an instance. In supervised learning, a dataset may be represented as pairs (xi, yi), where xi is an input vector and yi is its label. The word target is often used similarly.

Examples include:

  • “fraud” or “not fraud” for a classification task;
  • a numerical sale price for a regression task; and
  • the next word in a sequence for a prediction task.

Some datasets have no labels. They can still be useful for unsupervised learning.

Instances and datasets

Suppose a loan dataset has 10,000 applications and 18 input columns. Each application is an instance, and each column may be a feature. If repayment outcome is included, it is usually the label rather than an input feature. Confusing the label with a feature can produce leakage and unrealistically high evaluation results.

Models, Parameters, and Hyperparameters

A model is a learned representation of a relationship between inputs and outputs. A model may predict a numerical value, assign a class, estimate a probability, group similar instances, or choose an action.

Parameters are internal values learned from training data. In a linear model, for example, the weights and intercept are parameters:

ŷ = w₀ + w₁x₁ + w₂x₂ + … + wdxd

Here, ŷ is the predicted output, xj is feature j, wj is its learned weight, w0 is the intercept, and d is the number of features. For a numerical prediction, the output and features must have compatible meanings and units; the weights provide the required unit conversion.

Hyperparameters are settings selected before or outside the ordinary parameter-learning process. Examples include learning rate, tree depth, number of neighbours, batch size, and regularization strength. Hyperparameters are usually selected using validation data or cross-validation.

A model is therefore more than a formula. It includes the learned parameters, the feature-processing steps, the output interpretation, and often a decision threshold.

Training, Validation, Testing, and Inference

Training is the process of using data to determine model parameters. A learning algorithm searches for parameters that optimize an objective, commonly called a loss function. For supervised learning, a generic empirical-risk objective is:

J(θ) = (1/n) ∑ᵢ₌₁ⁿ L(f_θ(xi), yi)

Here, θ denotes the model parameters, f_θ is the model, xi is the input, yi is the known label, L is a loss function, and n is the number of training instances. The objective is applicable when the loss is defined for each example and the average loss is a meaningful training criterion. Lower loss is preferred, although lower training loss does not necessarily mean better real-world performance.

For squared-error regression, one possible loss is:

L(ŷ, y) = (ŷ − y)²

where ŷ is the prediction and y is the observed numerical target. Both must use compatible units, so the squared loss has squared target units.

A dataset is commonly divided into:

  • Training set: used to learn parameters.
  • Validation set: used to select models, features, and hyperparameters.
  • Test set: reserved for final, relatively unbiased performance estimation.

The test set should not repeatedly guide development. If developers inspect test results and alter the system, the test set gradually becomes part of training decisions.

Inference is the use of a trained model to produce an output for new input. Training may be computationally expensive, whereas inference is often required to be fast, reliable, and cost-effective. Inference can occur in a data centre, on a mobile device, in an embedded system, or at the edge of a network.

Generalization and Model Quality

Generalization is the ability of a model to perform well on relevant unseen data drawn from the same intended operational process. It is the central reason for using ML rather than memorizing a training dataset.

Overfitting and underfitting

Overfitting occurs when a model fits training data too closely, including noise or accidental patterns, and performs poorly on new data. A highly complex model, a small dataset, noisy labels, or excessive training can increase this risk.

Underfitting occurs when the model is too simple or insufficiently trained to capture important structure. It performs poorly even on training data.

Regularization, early stopping, simpler models, more data, data augmentation, and careful feature selection can reduce overfitting. Better features, a more suitable model, or longer training can address underfitting. These remedies must be evaluated rather than assumed to work.

Distribution shift

Generalization assumptions can fail when deployment data differ from training data. This is called distribution shift. For example, a model trained on daytime road images may perform poorly at night. Changes can affect feature distributions, label relationships, user behaviour, or the meaning of labels.

Data leakage

Data leakage occurs when information unavailable at prediction time enters training or evaluation. Examples include using a post-treatment medical variable to predict treatment outcome, using a future transaction in a fraud model, or scaling all data before splitting them. Leakage produces misleadingly strong results and is a major professional error.

Main Learning Paradigms

Supervised learning

Supervised learning uses labelled examples to learn a mapping from inputs to outputs. Its main tasks are classification and regression.

Classification predicts a discrete category, such as defective or acceptable. Binary classification has two classes; multiclass classification has more than two. Regression predicts a numerical quantity, such as energy demand or delivery time.

A supervised workflow includes defining the target, collecting representative labelled data, splitting data appropriately, preprocessing inputs, training a model, selecting hyperparameters, evaluating with suitable metrics, and monitoring after deployment.

Common metrics include accuracy, precision, recall, and mean absolute error. Accuracy is:

Accuracy = (number of correct predictions) ÷ (total number of predictions)

For a binary classifier, precision is:

Precision = TP ÷ (TP + FP)

and recall is:

Recall = TP ÷ (TP + FN)

Here, TP is true positives, FP is false positives, and FN is false negatives. These metrics are meaningful only when the positive class and decision procedure are clearly defined. Accuracy can be unsuitable for highly imbalanced classes.

Unsupervised learning

Unsupervised learning uses data without supplied labels. The system seeks structure such as groups, low-dimensional representations, or unusual observations.

Clustering assigns instances to groups according to a similarity criterion. Customer segmentation is a common application, but a cluster is not automatically a meaningful social or business category. Human interpretation and external validation are required.

Dimensionality reduction represents data using fewer variables while attempting to preserve selected information, such as variance or neighbourhood relationships. It can support visualization, compression, and preprocessing.

Anomaly detection identifies observations that differ from the expected pattern. In safety or cybersecurity, an anomaly is not necessarily an attack; it is a signal requiring investigation.

Semi-supervised learning

Semi-supervised learning combines a small labelled dataset with a larger unlabelled dataset. It is useful when obtaining labels requires expert time or expense, such as medical image annotation. Its assumptions must be checked: unlabelled examples should be relevant, and the model's use of them should not reinforce systematic labelling errors.

A related approach is self-supervised learning, in which artificial prediction tasks are generated from unlabelled data. For example, parts of a sequence may be hidden and predicted. The resulting representation can later be adapted to a labelled task. Self-supervised learning is often discussed separately because its supervision signal is constructed rather than supplied by a human annotator.

Reinforcement learning

Reinforcement learning (RL) concerns an agent interacting with an environment. At time t, the agent observes a state, chooses an action, receives a reward, and moves to a subsequent state. The objective is to learn a policy that selects actions to maximize expected cumulative reward.

A simplified discounted return is:

Gt = Rt+1 + γRt+2 + γ²Rt+3 + …

Here, Rt+k is the reward received at a future step and γ is the discount factor, where 0 ≤ γ < 1 in the usual infinite-horizon formulation. A smaller γ places greater emphasis on immediate rewards; a value near one gives greater weight to delayed rewards. The formulation assumes rewards are defined over successive time steps and that the discounted return is well behaved.

RL differs from supervised learning because the correct action is not normally supplied for every state. Feedback may be delayed, and the agent must balance exploration of alternatives with exploitation of known good actions. Applications include robotics, resource allocation, recommendation, and game playing. Poorly designed reward functions can produce unwanted behaviour, a problem known as reward misspecification.

Comparison of paradigms

ParadigmTraining signalTypical taskExample
SupervisedHuman- or system-provided labelsPredictionDetect fraudulent transactions
UnsupervisedNo supplied labelsDiscover structureGroup similar customers
Semi-supervisedSmall labelled and large unlabelled setsPrediction with limited labelsClassify medical images
ReinforcementRewards from interactionSequential decision-makingControl a warehouse robot

A Practical Machine-Learning Workflow

A competent project begins with the problem, not with a fashionable algorithm.

  1. Define the decision: Specify who will use the output, what action follows, and what constitutes success.
  2. Identify the data-generating process: Determine how data are collected, labelled, updated, and governed.
  3. Select features available at decision time: Document sources, units, missing values, and permissible use.
  4. Choose the learning paradigm: Decide whether labels, unlabelled data, or interactive feedback are available.
  5. Create valid data splits: Respect time order, groups, users, and other dependencies. Random splitting is not always appropriate.
  6. Establish a baseline: Compare with a simple rule or existing process.
  7. Train and tune: Fit parameters on training data and select hyperparameters using validation evidence.
  8. Evaluate: Use task-appropriate metrics, subgroup analysis, calibration checks, and robustness tests.
  9. Deploy responsibly: Integrate the model with monitoring, human oversight, security, and rollback procedures.
  10. Monitor and improve: Track performance, drift, data quality, fairness, and changing business or social conditions.

Applied Case: Predictive Maintenance in a Manufacturing Plant

This is an illustrative case.

Background and problem

A plant operates industrial pumps. Unexpected failures stop production, while replacing components too early wastes money and materials. The plant wants to predict whether a pump is likely to fail within the next seven days.

Analysis

Historical records contain vibration, temperature, pressure, operating hours, maintenance events, and failure dates. The target is binary: failure within seven days or no failure within seven days. A critical issue is time: measurements recorded after a fault has begun, or maintenance records created after a failure, must not be used as if they were available before the prediction.

The data should be split chronologically. Earlier months can form the training set and later months the test set. A random split might place nearly identical readings from the same failure event in both sets and inflate performance.

Approach and solution

The team first establishes a baseline based on operating hours. It then trains a supervised classification model using only readings available at scheduled prediction times. Because failures are rare, accuracy alone is inadequate. Recall is important because missed failures may cause downtime, while precision matters because unnecessary inspections have a cost.

The output probability is converted into an action only after considering inspection capacity and the relative costs of false alarms and missed failures. Engineers review important signals and investigate whether the model behaves differently for pump types or operating environments. The system includes a human confirmation step and records each recommendation for later auditing.

Outcome

In a pilot, the model identifies a useful proportion of upcoming failures earlier than the existing process. It does not eliminate failures and is not treated as an autonomous maintenance authority. Its value comes from prioritizing inspections and supporting engineering judgement. Continued monitoring is required because equipment, operating conditions, and maintenance practices change.

Learning points

The case illustrates that successful ML is not merely selection of an algorithm. It requires a clearly defined target, leakage-free data, time-aware evaluation, cost-sensitive interpretation, domain expertise, human oversight, and post-deployment monitoring.

Professional, Ethical, and Governance Considerations

Machine-learning practitioners are responsible for more than predictive accuracy. They should document data provenance, consent and permissible use, assumptions, known limitations, and model versions. Personal data should be minimized, protected, and retained only as necessary. Access controls and encryption reduce security risk, but they do not remove the need for lawful and ethical governance.

Fairness must be considered when predictions affect people. A model can reproduce historical discrimination even when sensitive attributes are removed, because proxy variables may remain. Fairness assessment requires identifying relevant groups, selecting context-appropriate measures, and considering trade-offs between error rates, access, privacy, and accuracy.

Explainability is especially important in high-impact decisions. An explanation should be understandable to the intended user and should not falsely imply that a complex model is certain or causal. Human review must be meaningful rather than a superficial approval step.

Models can also be attacked or manipulated. Adversaries may poison training data, exploit exposed predictions, or create inputs designed to cause errors. Secure development, testing, incident response, and monitoring are therefore part of ML engineering.

Contemporary Developments and Best Practices

Modern systems increasingly use pretrained models, transfer learning, self-supervised representations, and foundation models. These approaches can reduce the labelled-data requirement but may introduce inherited bias, uncertain provenance, high computational cost, and difficult-to-detect failure modes. A pretrained model must still be evaluated for the specific deployment context.

Best practices include:

  • define the operational objective before choosing a model;
  • maintain a data and model documentation record;
  • separate training, validation, and test information carefully;
  • use interpretable baselines before complex systems;
  • evaluate across relevant subgroups and realistic conditions;
  • test robustness, security, and failure recovery;
  • monitor drift and recalibrate or retrain when justified; and
  • preserve human accountability for consequential decisions.

Chapter Summary

Machine learning constructs models from data rather than relying entirely on manually written rules. Features describe inputs, labels describe desired outputs, and instances are individual examples. Parameters are learned internal values, while hyperparameters control the learning process. Training estimates model parameters; inference applies the trained model to new inputs.

The key objective is generalization: useful performance on relevant unseen cases. Overfitting, underfitting, data leakage, and distribution shift can undermine it. Supervised learning uses labelled examples for classification and regression. Unsupervised learning discovers structure without supplied labels. Semi-supervised learning combines limited labels with unlabelled data, while reinforcement learning learns sequential actions from rewards.

A reliable ML project begins with a well-defined decision and continues through valid data preparation, appropriate evaluation, deployment safeguards, monitoring, and ethical governance. Accuracy is important, but it must be interpreted alongside cost, fairness, privacy, security, explainability, and human responsibility.

Key Terms

Feature: An input variable used by a model.

Label: The known or desired output associated with an instance.

Instance: One observation, example, record, or sample in a dataset.

Dataset: A collection of instances and their associated variables.

Model: A learned representation used to produce predictions, groupings, or decisions.

Parameter: An internal model value learned from data.

Hyperparameter: A learning or model setting selected outside ordinary parameter fitting.

Training: The process of estimating model parameters from data.

Inference: Applying a trained model to new input.

Loss function: A function that measures disagreement between a prediction and a target.

Generalization: Performance on relevant unseen data.

Overfitting: Learning noise or accidental training patterns that do not transfer.

Underfitting: Failing to capture important patterns because the model is inadequate.

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

Distribution shift: A change between data used for development and data encountered in operation.

Classification: Prediction of a discrete category.

Regression: Prediction of a numerical quantity.

Clustering: Grouping instances according to a similarity criterion.

Reinforcement learning: Learning actions through interaction and reward feedback.

Policy: A strategy mapping states or observations to actions in reinforcement learning.

Review Questions

5 Short Answer Questions

  1. How does machine learning differ from conventional programming?
  2. Distinguish a feature from a label using a practical example.
  3. What is the difference between a parameter and a hyperparameter?
  4. Why should a test set be protected from repeated development decisions?
  5. Define data leakage and state one example.

5 Long Answer Questions

  1. Explain the complete lifecycle of a supervised machine-learning project, from problem definition to monitoring.
  2. Discuss overfitting, underfitting, and distribution shift, including methods for reducing their effects.
  3. Compare supervised, unsupervised, semi-supervised, and reinforcement learning.
  4. Explain why accuracy may be inadequate for evaluating a classifier used to detect rare failures.
  5. Discuss ethical, privacy, fairness, security, and accountability issues in deploying ML systems.

3 Analytical / Application Questions

  1. A hospital has 500 labelled scans and 100,000 unlabelled scans. Which learning paradigm could be considered, and what assumptions and risks should be examined?
  2. Design a leakage-resistant evaluation strategy for a model predicting equipment failure from sensor data collected over time.
  3. A fraud detector has high accuracy but misses most fraudulent transactions. Analyse possible causes and recommend more appropriate evaluation and deployment measures.

Multiple Choice Questions with Answers

Question 1

Which statement best describes machine learning?

A. A system that always follows manually written rules

B. A process that learns patterns from data to produce a model

C. A database that stores every possible answer

D. A hardware component used only for graphics

Correct Answer: B. A process that learns patterns from data to produce a model

Question 2

In a supervised dataset, what is a label?

A. An input variable

B. A model hyperparameter

C. The desired output associated with an instance

D. A software license

Correct Answer: C. The desired output associated with an instance

Question 3

Which value is normally learned from training data?

A. A model parameter

B. The test-set definition

C. The legal purpose of the system

D. The deployment location

Correct Answer: A. A model parameter

Question 4

What is inference?

A. Cleaning a dataset before collection

B. Applying a trained model to new input

C. Selecting a programming language

D. Deleting the validation set

Correct Answer: B. Applying a trained model to new input

Question 5

Which situation is an example of data leakage?

A. Using a feature available at prediction time

B. Evaluating on a reserved test set once

C. Using a post-failure maintenance record to predict failure beforehand

D. Comparing a model with a simple baseline

Correct Answer: C. Using a post-failure maintenance record to predict failure beforehand

Question 6

Which paradigm uses data without supplied labels to discover structure?

A. Unsupervised learning

B. Supervised learning

C. Reinforcement learning

D. Manual programming

Correct Answer: A. Unsupervised learning

Question 7

In reinforcement learning, what primarily guides the agent's behaviour?

A. A label for every possible action

B. Rewards received through interaction

C. A fixed table of all future states

D. Only the number of input features

Correct Answer: B. Rewards received through interaction

Question 8

Why can accuracy be misleading for rare-event detection?

A. Accuracy cannot be calculated for classifiers

B. A model can be accurate overall while missing most rare positive cases

C. Accuracy measures only training speed

D. Accuracy is always lower than recall

Correct Answer: B. A model can be accurate overall while missing most rare positive cases

Question 9

Which practice most directly helps prevent overfitting?

A. Evaluating only on training data

B. Increasing leakage from the test set

C. Using validation data and appropriate regularization

D. Removing all evaluation metrics

Correct Answer: C. Using validation data and appropriate regularization

Question 10

What is the main purpose of a chronological test split in predictive maintenance?

A. To ensure future information does not influence assessment of past-trained models

B. To guarantee that every class has equal size

C. To eliminate the need for labels

D. To convert regression into clustering

Correct Answer: A. To ensure future information does not influence assessment of past-trained models

Introduction to AI and ML
Chapter 2 of 14