Introduction to Jupyter notebooks

COMP4670/8600 - Statistical Machine Learning

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.

Basic knowledge

IMPORTANT: When using mathematical formulas, provide the precise name for each component.

$\newcommand{\RR}{\mathbb{R}}$

Random variables

Write down the definitions of the following entities, and provide a simple example to illustrate.

  1. The expectation of a function $f$ with respect to a
    • continuous random variable $X$
    • discrete random variable $X$
  2. The variance of a random variable $X$.
  3. Independence of two random variables $X$ and $Y$

Answer

--- replace this with your solution, add and remove code and markdown cells as appropriate ---

Discrete probabilities

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
  1. The sum rule of probability theory
  2. The product rule of probability theory
  3. Independence of two random variables $X$ and $Y$

Answer

--- replace this with your solution, add and remove code and markdown cells as appropriate ---

Calculus

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.

Answer

--- replace this with your solution, add and remove code and markdown cells as appropriate ---

Python and Programming for Machine Learning

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:

Installation

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.

Jupyter Notebooks

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.

  1. Descriptive notes
    • Markdown
    • LaTeX
  2. Computational code
    • numerical python
      • numpy
      • scipy
    • matplotlib

To use a notebook locally:

jupyter notebook name_of_file.ipynb

Markdown and LaTeX

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}

Computational code

Setting up python environment (do not use pylab)

Some resources:

Toy dataset for debugging

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.

Use gen_data to generate 30 samples from each Gaussian, with a 2 dimensional feature space. Plot this data.

Discuss:

Reading the writing CSV

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.