Exercise 01
Exercise 01
Introduction
For computational efficiency of typical operations in machine learning applications, it is very beneficial to use
NumPy arrays together with vectorized commands, instead of explicit for loops. The vectorized commands are
better optimized, and bring the performance of Python code (and similarly e.g. for Matlab) closer to lower level
languages like C. In this exercise, you are asked to write efficient implementations for three small problems that
are typical for the field of machine learning.
Getting Started
Follow the Python setup tutorial provided on our github repository here:
After you are set up, clone or download the repository, and start by filling in the template notebooks in the folder
/labs/ex01, for each of the 3 tasks below.
To get more familiar with vector and matrix operations using NumPy arrays, it is also recommended to go through
the npprimer.ipynb notebook in the same folder.
Note: The following three exercises could be solved by for-loops. While that’s ok to get started, the goal of
this exercise sheet is to use the more efficient vectorized commands instead:
Useful Commands
We give a short overview over some commands that prove useful for writing vectorized code. You can read the
full documentation and examples by issuing help(func).
At the beginning: import numpy as np
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Figure 1: Two sets of points in the plane. The circles are a subset of the dots and have been perturbed randomly.
|Σ| is the determinant of Σ and Σ−1 its inverse. Your function must return the ’most likely’ assignment
an ∈ {1, 2} for each input point n, where an = 1 means that xn has been assigned to model 1. In other words
in the case that an = 1, it holds that p(xn | µ1 , Σ1 ) > p(xn | µ2 , Σ2 ).
2
Theory Questions
In addition to the practical exercises you do in the labs, as for example above, we will in future labs also provide
you some theory oriented questions, to prepare you for the final exam. As the rest of the exercises, it is not
mandatory to solve them, but we would recommend that you at least look at - and try - some of them during the
semester. From last year’s experience, many students where surprised by the heavy theoretical focus of the final
exam after having worked on the two very practical projects. Do not fall for this trap! Passing the course require
acquiring both a practical and a theoretical understanding of the material, and those exercises should help you
with the latter.
However, please note that the difficulty of these exercises might not be of the same level as the exam and are
not enough by themselves; you should read the additional material given at the end of the lectures and do the
exercises in the recommended books - see The course info sheet.
Note that we will try to, but might not, provide solutions.
This week, as we just started the course, there are no exercises. You should refresh your mind of the prerequisites,
especially on the following topics.
• If it has been long since your last calculus class, make sure you know how to handle Gradients. You can
find a quick summary and useful identities in The Matrix Cookbook.
• For probability and statistics, you should at least know about
If you need a refresh, check Chapter 2 in Pattern Recognition and Machine Learning by Christopher Bishop,
available at the EPFL Library.