1. Home
  2. Explore
  3. Computer Science
  4. Introduction to Machine Learning

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

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:

  1. A feature vector describing the example.

  2. A label giving the correct class for that example.

Suppose there are nnn observed examples. The iiith example has feature vector x(i)x^{(i)}x(i) and label y(i)y^{(i)}y(i). The full training dataset is

Dn={(x(i),y(i))}i=1n.D_n = \left\{ \left(x^{(i)}, y^{(i)}\right) \right\}_{i=1}^{n}.Dn​={(x(i),y(i))}i=1n​.

The feature vector has ddd components:

x(i)=[x1(i)x2(i)⋮xd(i)]∈Rd.x^{(i)} = \begin{bmatrix} x_1^{(i)} \\ x_2^{(i)} \\ \vdots \\ x_d^{(i)} \end{bmatrix} \in \mathbb{R}^d.x(i)=​x1(i)​x2(i)​⋮xd(i)​​​∈Rd.

Here, ddd 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 555555 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:

  • x1x_1x1​: an illustrative measure of oxygen intake.

  • x2x_2x2​: an illustrative measure of movement.

The label is

y∈{−1,+1},y \in \{-1, +1\},y∈{−1,+1},

where −1-1−1 means “did not have a seizure” and +1+1+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:

h:Rd→{−1,+1}.h : \mathbb{R}^d \rightarrow \{-1, +1\}.h:Rd→{−1,+1}.

For an input xxx, the value h(x)h(x)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,

h(x)=+1for every xh(x) = +1 \quad \text{for every } xh(x)=+1for every x

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\mathcal{H}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 ddd 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 xxx as a column vector in Rd\mathbb{R}^dRd. Choose another column vector θ∈Rd\theta \in \mathbb{R}^dθ∈Rd. The dot product of θ\thetaθ and xxx is

θ⊤x.\theta^\top x.θ⊤x.

Because θ⊤\theta^\topθ⊤ has shape 1×d1 \times d1×d and xxx has shape d×1d \times 1d×1, their product is a scalar.

The dot product is related to the projection of xxx onto the direction of θ\thetaθ:

projection of x onto θ=θ⊤x∥θ∥.\text{projection of } x \text{ onto } \theta = \frac{\theta^\top x}{\|\theta\|}.projection of x onto θ=∥θ∥θ⊤x​.

This scalar describes how far xxx extends in the direction of θ\thetaθ. If xxx is perpendicular to θ\thetaθ, 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

θ⊤x+θ0=0,\theta^\top x + \theta_0 = 0,θ⊤x+θ0​=0,

where θ0\theta_0θ0​ is a scalar. Together, θ\thetaθ and θ0\theta_0θ0​ specify the boundary:

  • θ\thetaθ determines the boundary’s orientation.

  • θ0\theta_0θ0​ shifts the boundary’s position.

For example, if the projection of xxx onto θ\thetaθ equals −b-b−b, then

θ⊤x∥θ∥=−b.\frac{\theta^\top x}{\|\theta\|} = -b.∥θ∥θ⊤x​=−b.

Multiplying by ∥θ∥\|\theta\|∥θ∥ gives

θ⊤x+b∥θ∥=0.\theta^\top x + b\|\theta\| = 0.θ⊤x+b∥θ∥=0.

Thus, setting θ0=b∥θ∥\theta_0 = b\|\theta\|θ0​=b∥θ∥ yields the form θ⊤x+θ0=0\theta^\top x + \theta_0 = 0θ⊤x+θ0​=0. The scalar θ0\theta_0θ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\theta^\top x + \theta_0θ⊤x+θ0​ indicates which side of the hyperplane contains xxx. A linear classifier is defined by

h(x;θ,θ0)={+1if θ⊤x+θ0>0,−1if θ⊤x+θ0≤0.h(x;\theta,\theta_0) = \begin{cases} +1 & \text{if } \theta^\top x + \theta_0 > 0, \\ -1 & \text{if } \theta^\top x + \theta_0 \leq 0. \end{cases}h(x;θ,θ0​)={+1−1​if θ⊤x+θ0​>0,if θ⊤x+θ0​≤0.​

The boundary itself consists of points satisfying θ⊤x+θ0=0\theta^\top x + \theta_0 = 0θ⊤x+θ0​=0. This definition assigns those boundary points the label −1-1−1; that convention is a choice for handling ties.

The notation h(x;θ,θ0)h(x;\theta,\theta_0)h(x;θ,θ0​) separates the input xxx from the parameters θ\thetaθ and θ0\theta_0θ0​. The input is the example being classified. The parameters specify which particular classifier is being used.

The class of linear classifiers, H\mathcal{H}H, contains the classifiers obtained by choosing the possible values of θ\thetaθ and θ0\theta_0θ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)L(g,a)L(g,a) takes two arguments:

  • ggg: the predicted value, or guess.

  • aaa: 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:

L(g,a)={0if g=a,1if g≠a.L(g,a) = \begin{cases} 0 & \text{if } g = a, \\ 1 & \text{if } g \neq a. \end{cases}L(g,a)={01​if g=a,if g=a.​

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′n'n′ new labeled examples arrive after training, their average loss can be written as

Etest(h)=1n′∑i=n+1n+n′L(h(x(i)),y(i)).E_{\text{test}}(h) = \frac{1}{n'} \sum_{i=n+1}^{n+n'} L\left(h(x^{(i)}), y^{(i)}\right).Etest​(h)=n′1​∑i=n+1n+n′​L(h(x(i)),y(i)).

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:

En(h)=1n∑i=1nL(h(x(i)),y(i)).E_n(h) = \frac{1}{n} \sum_{i=1}^{n} L\left(h(x^{(i)}), y^{(i)}\right).En​(h)=n1​∑i=1n​L(h(x(i)),y(i)).

This quantity is the training error. It measures how well the classifier fits the examples in DnD_nDn​.

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 hhh and h~\tilde{h}h~, one can prefer hhh according to training error when

En(h)<En(h~).E_n(h) < E_n(\tilde{h}).En​(h)<En​(h~).

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:

x⟼h(x).x \longmapsto h(x).x⟼h(x).

A learning algorithm takes an entire labeled dataset and returns a classifier:

Dn⟼h.D_n \longmapsto h.Dn​⟼h.

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,

h(1),h(2),…,h^{(1)}, h^{(2)}, \ldots,h(1),h(2),…,

and suppose that a learning algorithm is allowed to consider the first kkk candidates. It calculates each candidate’s training error and chooses the one with the smallest value:

j∗∈arg min⁡1≤j≤kEn(h(j)),j^* \in \operatorname*{arg\,min}_{1 \leq j \leq k} E_n\left(h^{(j)}\right),j∗∈argmin1≤j≤k​En​(h(j)),

then returns h(j∗)h^{(j^*)}h(j∗). If several candidates tie for the minimum, the notation allows any of the minimizers to be selected.

The quantity kkk 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 θ\thetaθ and θ0\theta_0θ0​ specify an individual hypothesis.

If k=1k=1k=1, the algorithm has only one candidate and must return h(1)h^{(1)}h(1). If k=2k=2k=2, it chooses whichever of h(1)h^{(1)}h(1) and h(2)h^{(2)}h(2) has lower training error.

As kkk increases, the candidate set includes all the earlier candidates plus additional ones. Therefore, the smallest training error among the candidates cannot increase:

min⁡1≤j≤k+1En(h(j))≤min⁡1≤j≤kEn(h(j)).\min_{1 \leq j \leq k+1} E_n\left(h^{(j)}\right) \leq \min_{1 \leq j \leq k} E_n\left(h^{(j)}\right).min1≤j≤k+1​En​(h(j))≤min1≤j≤k​En​(h(j)).

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:

  1. Represent examples: Convert each observed case into a feature vector x∈Rdx \in \mathbb{R}^dx∈Rd.

  2. Provide labels: Pair each training example with its known label yyy.

  3. Choose a hypothesis class: Specify the kinds of prediction functions the method may use, such as linear classifiers.

  4. Define quality: Use a loss function to express the cost of different predictions and errors.

  5. Learn from data: Apply a learning algorithm to the training set to select a classifier.

  6. 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.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent