01 - Introduction

Pattern Recognition and Machine Learning, Lab 1

Michal Neoral

neoramic@fel.cvut.cz

25 September 2026

What you will learn

Part 1

Decision making

  • Bayesian decision theory
  • Non-Bayesian tasks

Probabilities are given

Part 2

Parameter estimation

  • MLE and MAP estimation
  • Parzen windows, k-NN

Probabilities are estimated

Part 3

Classifiers and learning

  • Logistic regression
  • Perceptron, SVM
  • AdaBoost, neural nets
  • K-means, EM, PCA
  • Decision trees

Many different principles

There is a lot. Do not memorize it, understand how the methods compare and relate.

Organization

Lab overview

  • Weekly assignment: implement a method from the lecture
  • Labs are mostly exercises at the whiteboard
  • Three tests during the semester, bring a calculator!
  • It is a difficult class!
Lab plan 2026/27
25.9. Introduction, Python, NumPy
2.10. Bayesian decision task
9.10. Non-Bayesian tasks: minimax
16.10. Parzen windows test
23.10. MLE, MAP, Bayesian estimation
30.10. Logistic regression
6.11. Problem solving
13.11. Linear classifier: perceptron test
20.11. Dean's day, no lab
27.11. Support Vector Machine
4.12. AdaBoost
11.12. K-means clustering test
18.12. Convolutional neural networks
8.1. Credits, revision, problem solving

Expectations

Time

  • 6 credits ≈ 156 hours in the semester
  • About 1 hour/week on the lecture
  • 11 lab assignments ≈ 6-7 hours/week

Plan enough time for the assignments!

Skills

  • Python: NumPy, Jupyter notebooks
  • Mathematics
    • probability and statistics
    • derivatives, integrals
    • linear algebra, optimization

Credit requirements (zápočet)

All 11 assignments

Each one must pass the BRUTE evaluation

Final deadline: 10 January 2027

All 3 tests

Attend the short written tests at the beginning of the labs on

16 October, 13 November and 11 December

Missing a test for a serious reason? Contact your TA to arrange a make-up test.

Points for an assignment

1.3 pts within 2 weeks

1 pt after 2 weeks, until the final deadline

no credit after 10 January

first 2 weeksuntil 10 January 2027BRUTE closed

The introductory assignment (this week) is worth 1 point, so 1 + 10 × 1.3 = 14 points in total.

Bonus tasks: up to +0.8 pts each. Show them at the lab, no deadline (until the day before the exam).

Final grade: 100 points

Labs 50 pts

Written exam 40 pts

Oral 10

  • Points from the labs go directly into the final evaluation
  • The oral exam is compulsory
  • You can take the exam only with the zápočet

Final grade: 100 points

Assignments14 pts
3 tests × 1236 pts
Written exam40 pts
76 %
Oral10

+ bonus

Bonus points can push the lab score above 50 or compensate for points lost in the tests

76 % of the grade comes from written work: tests + written exam, i.e. "math" and "problem solving"

Grade scale

F < 50

E 50-59

D 60-69

C 70-79

B 80-89

A 90+

  • A minimum number of points from the written exam is required to pass
  • The oral exam can move your grade in both directions
  • Exam questions from previous years and example oral questions are on the course page (Czech, English) and in the exam question archive

Support

CourseWare

  • Lab instructions
  • Git templates
  • BRUTE upload system

Forum

The preferred channel, all TAs answer there.

  • do not share your code
  • do not share derivations
  • search first

E-mail your TA

Only personal matters:

  • missed test, illness
  • no forum access

neoramic@fel.cvut.cz

Never e-mail your code asking "what is wrong?". Ask at the forum, TAs see your code.

Cooperation and plagiarism

OK

  • Study together, use the Exercise book (problems from previous tests)
  • Discuss lectures, exercises and labs
  • Discuss implementation ideas

Not OK

  • Using a piece of implementation or derivation found anywhere
    • except course materials and tutorials
  • Sharing or publishing your solution

Plagiarism = no zápočet

AI tools

  • AI tools are generally allowed, unless stated otherwise
  • You are the author, including the mistakes AI makes
  • Understand every line, we may ask you to explain it
  • Copying longer AI-generated parts counts as plagiarism
  • Be open about how you used AI

Best practice

Use AI to learn the syntax or an idea, then implement the solution again from your head (e.g. after 24 hours).

Our goal

We want you to learn, not to paste the assignment into a chat.

Avoid vibe-coding.

Lab 1: assignment

  • Install your IDE (PyCharm, VS Code, Vim, ...)
  • Verify access to the forum, git templates and BRUTE
  • Download the git template for the first lab
  • Verify that you can upload to BRUTE and get the results back
  • Get familiar with Python and NumPy

Do NOT forget: there is homework already from this first lab!

Recap from probability and statistics

Probability

A1: Non-negativity \(P(A) \ge 0\)

A2: Normalization \(P(\Omega) = 1\)

A3: Additivity \(P(A \cup B) = P(A) + P(B) \;\text{ if }\; A \cap B = \emptyset\)

In general: \(P(A \cup B) = P(A) + P(B) - P(A \cap B)\)

\(P(A \cap B) = \; ?\)

Ω A B

Two pens, pigs and sheep

P(pig) = ?5/10 = ½

P(sheep) = ?5/10 = ½

P([ ]) = ?4/10 = ⅖

P([ ]) = ?6/10 = ⅗

P(pig | [ ]) = ?3/4

P(sheep | [ ]) = ?4/6 = ⅔

P([ ] | pig) = ?3/5

P([ ] | sheep) = ?4/5

pig pig sheep sheep sheep sheep
sheep pig pig pig
wolf

The wolf is hungry. He picks a random animal for lunch.

The wolf picks one animal at random. What is the probability that it is a pig?
The wolf picks one animal at random. What is the probability that it is a sheep?
What is the probability that the wolf's lunch comes from the green pen?
What is the probability that the wolf's lunch comes from the brown pen?
The wolf is hunting in the green pen. What is the probability that he catches a pig?
The wolf is hunting in the brown pen. What is the probability that he catches a sheep?
The wolf ate a pig. What is the probability that it came from the green pen?
The wolf ate a sheep. What is the probability that it came from the brown pen?

Total probability

Product rule \[P(A \cap B) = P(A, B) = P(A \mid B)\,P(B) = P(B \mid A)\,P(A)\]

Sum rule, also called marginalization \[P(A) = P(A, B) + P(A, B^c)\] \(B^c\) is the complement of \(B\): all outcomes in which \(B\) does not occur, \(P(B^c) = 1 - P(B)\)

Total probability \[P(A) = \sum_i P(A, B_i) = \sum_i P(A \mid B_i)\,P(B_i)\] \(B_i\) are mutually exclusive and jointly exhaustive (exactly one of them occurs)

More pigs and sheep

P(pig, [ ]) = ?P(pig | [ ]) P([ ]) = ¾ · ⅖ = 0.3

P([ ] | pig) P(pig) = ?⅗ · ½ = 0.3

P(pig, [ ]) = ?P(pig | [ ]) P([ ]) = 2/6 · ⅗ = 0.2

P(pig) = ?P(pig, [ ]) + P(pig, [ ]) = 0.3 + 0.2 = 0.5

wolf
pig pig sheep sheep sheep sheep
sheep pig pig pig
What is the probability that the wolf eats a pig from the green pen?
Do we get the same number the other way round, starting from all the pigs?
What is the probability that the wolf eats a pig from the brown pen?
We know both joint probabilities. What is the probability that the wolf eats a pig at all?

Bayes theorem

\[ \color{#3C7DD9}{P(H \mid E)} = \frac{\color{#E0A800}{P(E \mid H)}\;\color{#9B3C8C}{P(H)}}{\color{#4E8F3A}{P(E)}} \]

\(H\): hypothesis, e.g. it will rain today
\(E\): evidence, e.g. the morning is cloudy

posterior \(P(H \mid E)\) belief in \(H\) after seeing the evidence rain, given a cloudy morning

prior \(P(H)\) belief in \(H\) before any evidence how often it rains

marginal \(P(E)\) total probability of the evidence, often hard to compute how often it is cloudy

Later in RPZ: \(\theta\) = model parameters, \(D\) = data: \(\quad P(\theta \mid D) = P(D \mid \theta)\,P(\theta) \,/\, P(D) \;\propto\; P(D \mid \theta)\,P(\theta)\)

In words: the posterior of the parameters given the data is proportional to the likelihood of the data times the prior of the parameters. \(P(D)\) does not depend on \(\theta\), so we can drop it when comparing parameters.

Example: mammogram

In a study, physicians were asked about a woman with an initial 1 % risk of cancer (a malignant tumor) whose mammogram came back positive.

A mammogram correctly classifies about 80 % of malignant tumors (positive result) and 90 % of benign tumors (negative result).

95 of 100 physicians estimated the probability of cancer at about 75 %.

Do you agree?

Source: M. Merling, Practice questions on Bayes's formula, UPenn Math 107

Example: mammogram, solution

\(M\): malignant, \(B = M^c\): benign
\(+\): positive result

Known: \(P(M) = 0.01\), \(P(+ \mid M) = 0.8\), \(P(- \mid B) = 0.9\)

\(\Rightarrow P(+ \mid B) = 0.1\)

malignant, positive malignant, negative benign, positive benign, negative

\(P(M \mid +) = \dfrac{P(+ \mid M)\,P(M)}{P(+ \mid M)\,P(M) + P(+ \mid B)\,P(B)} = \dfrac{0.8 \cdot 0.01}{0.8 \cdot 0.01 + 0.1 \cdot 0.99} \approx 0.075\), the prior matters!

Example: three cards

We have 3 cards that are identical in form, except for their colors:

RR: both sides red
BB: both sides black
RB: one side red, one black

The cards are mixed in a hat, one is drawn at random and put on the ground.

If the upper side of the chosen card is red, what is the probability that the other side is black?

Source: M. Merling, Practice questions on Bayes's formula, UPenn Math 107

Example: three cards, solution

RR
BB
RB

All 6 sides are equally likely to be on top.

We see red, so only the 3 red sides remain possible.

Only one of them has black on the other side: \(P = \frac{1}{3}\), not \(\frac{1}{2}\)!

Formally, with \(R\) = "the upper side is red": \[ P(RB \mid R) = \frac{P(R \mid RB)\,P(RB)}{P(R \mid RR)\,P(RR) + P(R \mid RB)\,P(RB) + P(R \mid BB)\,P(BB)} = \frac{\frac12 \cdot \frac13}{1 \cdot \frac13 + \frac12 \cdot \frac13 + 0 \cdot \frac13} = \frac13 \]

Extra: videos