This tutorial will introduce the basic elements for writing Python programs, and using Jupyter notebooks.
Due to the wide variety of backgrounds that students may have, it is worth recalling some mathematics and statistics that we build upon in this course.
IMPORTANT: When using mathematical formulas, provide the precise name for each component.
$\newcommand{\RR}{\mathbb{R}}$
Write down the definitions of the following entities, and provide a simple example to illustrate.
--- replace this with your solution, add and remove code and markdown cells as appropriate ---
For discrete random variables $X$ and $Y$, define the following, and show an example of how it applies to the example below.
$p(\mathbf{X},\mathbf{Y})$ | X=a | X=b | X=c | X=d | X=e |
---|---|---|---|---|---|
Y = red | 0.2 | 0.1 | 0.1 | 0.01 | 0.04 |
Y = green | 0.08 | 0.07 | 0.01 | 0.05 | 0.05 |
Y = blue | 0.01 | 0.01 | 0.07 | 0.05 | 0.15 |
--- replace this with your solution, add and remove code and markdown cells as appropriate ---
Compute the gradient of the following function $f:\RR\to\RR$ $$ f(x) = \frac{1}{1 + \exp(x^2)} $$ What would the the gradient if $x$ was two dimensional (that is $f:\RR^2\to\RR$)? Generalise the scalar function above appropriately.
--- replace this with your solution, add and remove code and markdown cells as appropriate ---
If you already know Python and Jupyter notebooks well, please work on Tutorial 1b "Matrix decomposition"
The introduction will focus on the concepts necessary for writing small programs in Python for the purpose of Machine Learning. That means, we expect a user of the code will be a reasonable knowledgeable person. Therefore, we can skip most of the code a robust system would have to contain in order to check the input types, verify the input parameter ranges, and make sure that really nothing can go wrong when somebody else is using the code. Having said this, you are nevertheless encouraged to include some sanity tests into your code to avoid making simple errors which can cost you a lot of time to find. Some of the Python concepts discussed in the tutorial will be
We will be using Python3 in this course.
Some resources:
The easiest way to get a working Python environment is using one of the following collections:
It is also not too difficult to install python using your favourite package manager and then use conda or pip to manage python packages.
To work on a worksheet or assignment, download the notebook and edit it locally.
Jupyter notebooks provide a convenient browser based environment for data analysis in a literate programming environment. The descriptive parts of the notebook implements an enhanced version of markdown, which allows the use of LaTeX for rendering equations.
To use a notebook locally:
jupyter notebook name_of_file.ipynb
In addition to lists and links which are already shown above, tables are also nice and easy
Title | Middle | Left aligned | Right aligned |
---|---|---|---|
Monday | 10:00 | Sunny | 30 |
Thursday | 12:32 | Rain | 22.3 |
It is also easy to typeset good looking equations inline, such as $f(x) = x^2$, or on a line by itself. \begin{equation} g(x) = \sum_{i=1}^n \frac{\prod_{j=1}^d y_j \sqrt{3x_i^4}}{f(x_i)} \end{equation} If you use a symbol often, you can define it at the top of a document as follows (look at source), and use it in equations.
$\newcommand{\amazing}{\sqrt{3x_i^4}}$
\begin{equation} h(x) = \sum_{i=1}^n \amazing \end{equation}Setting up python environment (do not use pylab)
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp
%matplotlib inline
Some resources:
Write a function gen_data
that generates data from two Gaussians with unit variance, centered at $\mathbf{1}$ and $-\mathbf{1}$ respectively. $\mathbf{1}$ is the vector of all ones.
Hint: use np.ones
and np.random.randn
Use the function to generate 100 samples from each Gaussian, with a 5 dimensional feature space.
# replace this with your solution, add and remove code and markdown cells as appropriate
Use gen_data
to generate 30 samples from each Gaussian, with a 2 dimensional feature space. Plot this data.
Discuss:
# replace this with your solution, add and remove code and markdown cells as appropriate
Write a file containing the data to a csv file. Confirm that you can read this data using python and also manually inspect the file with a text editor.
# replace this with your solution, add and remove code and markdown cells as appropriate