Introduction to Machine Learning
Introduction to Machine Learning
What Machine Learning Does
Machine learning uses data and computational methods to produce predictions or decisions. A typical problem provides examples of some phenomenon, then asks a method to make decisions about new cases.
For instance, a hospital might have records of newborns who did or did not experience seizures. A machine-learning method could use those records to predict whether a future newborn is likely to have a seizure. Similar problems arise in many fields:
Astronomy: Telescope data can be used to identify which candidate signals indicate exoplanets.
Medicine: EEG data can help detect possible seizures in newborns and support decisions about medical care.
Text analysis: The text of legal petitions can be analyzed to identify topics, or workplace interviews can be assigned codes for occupations and events.
Cybersecurity: Email text can be classified as phishing or not phishing.
Finance: Information such as satellite images and weather data can help estimate whether a farm is likely to repay a loan.
Facial recognition: Images can be used to identify people, although mistakes can have serious consequences.
These examples share a structure: data are used to make a decision about an instance. The data may be images, signals, text, or other measurements, and the decision may be a category, a prediction, or an action.
Why Study Machine Learning?
Machine learning can automate decisions and may save time, labor, and resources. It can also be applied to new areas where manual analysis would be difficult or costly.
Understanding machine learning matters for additional reasons:
Evaluating claims: A system described as “machine learning” may not work as claimed. Its effectiveness and limitations need to be assessed.
Understanding consequences: Machine-learning decisions can affect medical care, access to loans, security, and interactions with government.
Improving systems: Even a system that performs adequately may need changes to work more reliably or appropriately.
Assessing risks: An error can have unequal consequences. For example, a mistaken identification in facial recognition can harm the person who is misidentified.
Machine learning is a tool, not a universal solution. It can perform poorly when the data or problem do not support reliable predictions, so it is important to understand when a method is likely to work and how its performance should be evaluated.
Machine learning is also fundamentally based on mathematics. Linear algebra, geometry, calculus, and probability are used to describe data, define models, and reason about their behavior.
Data and Supervised Classification
Representing Examples
In a supervised classification problem, each observed example consists of:
A feature vector describing the example.
A label giving the correct class for that example.
Suppose there are n observed examples. The ith example has feature vector x(i) and label y(i). The full training dataset is
The feature vector has d components:
Here, d is the dimension of the feature space. Although examples are often drawn as points in two dimensions for visualization, real applications may use many more features. A newborn-seizure example discussed in the lecture used a representation with 55 features.
Features are measurements or derived quantities used to represent an example. Constructing them can be a substantial task: complex raw data, such as EEG recordings, may need to be processed into a numerical feature vector before a classifier can use them.
Example: Newborn Seizure Classification
In a simplified example, each newborn is represented by two features:
x1: an illustrative measure of oxygen intake.
x2: an illustrative measure of movement.
The label is
where −1 means “did not have a seizure” and +1 means “had a seizure.” The features in this example are only a cartoon representation; real medical data and feature construction are more involved.
The labels in a training dataset are assumed to have been supplied—for example, by an expert determining what happened in each recorded case. The aim is to use these labeled examples to make predictions for future cases.
Hypotheses and Hypothesis Classes
A hypothesis is a function that maps a feature vector to a predicted label:
For an input x, the value h(x) is the classifier’s prediction.
Any function with this input and output type is a hypothesis, even if it is useless. For example,
is a valid hypothesis: it labels every example as positive. But if the task is to distinguish cases with seizures from cases without them, this constant prediction does not make the desired distinction.
A hypothesis class, commonly written H, is a collection of hypotheses from which a method may choose. A useful class contains hypotheses capable of expressing relevant patterns in the data.
Linear Classifiers
A linear classifier separates the feature space using a line in two dimensions or, more generally, a hyperplane in d dimensions. It assigns one label to one side of the separating boundary and the other label to the opposite side.
Describing a Hyperplane
Treat each feature vector x as a column vector in Rd. Choose another column vector θ∈Rd. The dot product of θ and x is
Because θ⊤ has shape 1×d and x has shape d×1, their product is a scalar.
The dot product is related to the projection of x onto the direction of θ:
This scalar describes how far x extends in the direction of θ. If x is perpendicular to θ, the projection is zero. All points whose projection has the same value form a hyperplane perpendicular to $\theta`.
A hyperplane can be described by the equation
where θ0 is a scalar. Together, θ and θ0 specify the boundary:
θ determines the boundary’s orientation.
θ0 shifts the boundary’s position.
For example, if the projection of x onto θ equals −b, then
Multiplying by ∥θ∥ gives
Thus, setting θ0=b∥θ∥ yields the form θ⊤x+θ0=0. The scalar θ0 can be understood directly as part of the boundary’s equation; it does not need to be interpreted through a projection.
Defining the Classifier
The value θ⊤x+θ0 indicates which side of the hyperplane contains x. A linear classifier is defined by
The boundary itself consists of points satisfying θ⊤x+θ0=0. This definition assigns those boundary points the label −1; that convention is a choice for handling ties.
The notation h(x;θ,θ0) separates the input x from the parameters θ and θ0. The input is the example being classified. The parameters specify which particular classifier is being used.
The class of linear classifiers, H, contains the classifiers obtained by choosing the possible values of θ and θ0. Defining a classifier does not require data: its parameters specify a rule over the feature space. A learning method must use data to select a suitable classifier from a class.
Measuring Classification Quality with Loss
A classifier is intended to predict labels for future examples. To define what “predict well” means, a problem needs a loss function.
A loss function L(g,a) takes two arguments:
g: the predicted value, or guess.
a: the actual value.
A loss assigns a numerical cost to a prediction. Lower loss is better. In settings where utility is used instead, loss can be viewed as negative utility: a larger loss is bad, whereas a larger utility is good.
Zero-One Loss
The basic classification loss is zero-one loss:
A correct prediction incurs no loss; an incorrect prediction incurs a loss of one. This loss treats all mistakes equally.
Asymmetric Loss
In many applications, different mistakes have different consequences. A loss can be asymmetric, assigning different costs to different kinds of errors.
In the seizure example:
Predicting a seizure when the newborn does not have one may lead to unnecessary use of staff time or other resources.
Predicting no seizure when the newborn does have one may prevent timely medical attention and lead to a serious adverse outcome.
These errors are not equally costly. A loss function can represent that difference by making a false negative more costly than a false positive. Choosing the appropriate relative costs is an important application-specific decision; it depends on the consequences of the possible predictions.
Training Error and Test Error
Test Error: The Desired Evaluation
Ultimately, a classifier should perform well on future data—examples that were not used to choose it. If n′ new labeled examples arrive after training, their average loss can be written as
This is the test error on those new examples. It compares the classifier’s predictions with the actual labels and averages the losses.
Test performance is the relevant goal because a classifier is intended to make useful predictions about cases it has not already seen. However, the labels for genuinely future examples are unavailable when the classifier is being selected. Without those labels, future error cannot be calculated in advance.
Training Error: An Available Proxy
The labeled training examples are available, so their average loss can be calculated:
This quantity is the training error. It measures how well the classifier fits the examples in Dn.
Training error is useful because it can be computed from the available data, but it is not the same as future performance. A low training error alone does not establish that a classifier will predict well on new examples. Test error is what one would ultimately like to know; training error is an accessible quantity that can help compare classifiers, but its limitations must be kept in mind.
Given two classifiers h and h~, one can prefer h according to training error when
This provides a rule for comparing two candidates, but selecting the best classifier from a large or complex class may itself be computationally difficult.
Learning Algorithms
A classifier takes one feature vector and returns a predicted label:
A learning algorithm takes an entire labeled dataset and returns a classifier:
The algorithm’s output depends on the dataset it receives. If it receives a different dataset, it may return a different classifier. Thus, a classifier and a learning algorithm are distinct objects: the classifier predicts on individual examples, while the learning algorithm uses a dataset to construct or select a classifier.
Selecting Among Candidate Classifiers
Consider a list of candidate classifiers,
and suppose that a learning algorithm is allowed to consider the first k candidates. It calculates each candidate’s training error and chooses the one with the smallest value:
then returns h(j∗). If several candidates tie for the minimum, the notation allows any of the minimizers to be selected.
The quantity k changes the behavior of the learning algorithm but is not part of its dataset input. Such a setting is a hyperparameter. By contrast, parameters such as θ and θ0 specify an individual hypothesis.
If k=1, the algorithm has only one candidate and must return h(1). If k=2, it chooses whichever of h(1) and h(2) has lower training error.
As k increases, the candidate set includes all the earlier candidates plus additional ones. Therefore, the smallest training error among the candidates cannot increase:
The minimum can stay the same or decrease. This guarantee concerns training error only; it does not establish that a larger candidate set will improve performance on future data.
Overall Learning Setup
The introductory classification problem can be summarized as follows:
Represent examples: Convert each observed case into a feature vector x∈Rd.
Provide labels: Pair each training example with its known label y.
Choose a hypothesis class: Specify the kinds of prediction functions the method may use, such as linear classifiers.
Define quality: Use a loss function to express the cost of different predictions and errors.
Learn from data: Apply a learning algorithm to the training set to select a classifier.
Evaluate appropriately: Aim for low loss on future data, while recognizing that future labels are unavailable during training and that training error is only an imperfect proxy.
The central challenge is not merely to define a classifier, but to use available data to select one that makes useful predictions on cases that have not yet been observed.

Ask anything
We've read this whole study set, so questions about it get the best answers.