Python for Machine Learning I
Colab, NumPy, Matplotlib — the tooling layer under every notebook.
Low exam-yield on its own, but every coding question is written in this dialect. Skim it, then use it as a reference when a notebook cell does something you do not recognise.
Colab and the notebook contract
A notebook is a list of cells: text and executable code/output. Submission is the .ipynb plus a PDF. Markdown cells hold the prose and LaTeX equations; code cells hold everything else.

NumPy
The fundamental array package. The properties you will actually use in the notebooks:
arr = np.array([1, 2, 3, 4])
arr.shape # (4,)
arr.ndim # 1
arr.size # 4
arr.dtype # int64

Key claims from the slide worth remembering: elements are all the same data type, arrays can be 1-D, 2-D or higher, storage is more compact than a Python list, and operations run over the whole array through vectorization (no Python loop).
The in-class exercise list is a decent self-test:
- Create an array of 5 numbers
- Check size, shape, dimension, dtype
- Reverse it
- Swap the first and third element
- Check whether a specified number is present
- Get the unique numbers
- Find the numbers greater than 1
- Sum, mean, max, min, std
- Replace zeros with a non-zero number
- 100 random integers and their histogram (
np.histogram)
Matplotlib
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4]
y = [3, 2, 1, 5, 0]
plt.plot(x, y) # straight line through the points
plt.show() # render at any point in the code
Formatting calls used constantly in the course notebooks:
| Call | Effect |
|---|---|
plt.axis('off') | turn axes off |
plt.grid() | grid on |
plt.xlabel() / plt.ylabel() | axis labels |
plt.title() | title |
plt.legend() | legend from label= arguments |
plt.savefig('f.png', bbox_inches='tight') | save |

Every notebook in this course plots plt.hist(labels) before modelling. That is the label distribution check, and it is graded in Assignment 1. Doing it on X instead of Y was an actual deduction — the histogram must be of the target, not a feature.
Packaging
pip install numpy,conda install numpyimport numpy,import numpy as np,from numpy import random,from numpy.random import normal- A module is a single
.pyfile; a package is a directory of modules with__init__.py.
conda create -n cs460g python
conda env export > cs460g.yml # at source
conda env create -f cs460g.yml # at target
That export/import pair is the reproducibility answer if you are asked how to make a project reproducible across machines.





























