COMP5318 — Applied Machine Learning
Week 1: Python, NumPy & Pandas
Refresh core Python, vectorised NumPy, and pandas DataFrames — aligned with w1.ipynb / w1.pdf and the ML1 lecture (data, CRISP-DM, attribute types, learning settings).
Course materials (this week)
Open these files from your local clone (same folder as this site). PDFs are for reading; notebooks are meant to be run in Jupyter.
| File | Role |
|---|---|
| w1.ipynb | Python refresher — lists, dicts, loops, NumPy, pandas |
| w1.pdf | Week summary / slides (if applicable) |
| ml1.pdf | Lecture notes (ML1) |
pip install package or your course environment instructions.
External references (from w1.ipynb): Google’s Python class (developers.google.com/edu/python), Aurélien Géron Hands-On Machine Learning…, and Müller & Guido Introduction to Machine Learning with Python (both via O’Reilly / library). For extra NumPy drills, see Géron’s companion notebook tools_numpy.ipynb.
Machine learning, data & the data-mining process
Aligned with ML1 (week 1): data is everywhere; each example is described by attributes; data mining extracts patterns from large datasets using ML, statistics, and databases.
Data, attributes & labels
- Dataset = collection of examples (instances, records, observations).
- Each example is described by attributes (features, variables) — e.g. refund status, marital status, taxable income in a classification example.
- Nominal (categorical) attributes take values from a finite set of categories; numeric (continuous) attributes are real-valued (e.g. Iris sepal/petal measurements + iris type label).
Data mining
Data mining is the process of discovering patterns in large data — methods sit at the intersection of machine learning, statistics, and database systems (Witten / Tan viewpoint as in the slides).
CRISP-DM (six phases)
The cross-industry standard process breaks work into: Business understanding → Data understanding → Data preparation → Modelling → Evaluation → Deployment. The order is not strict; you often iterate back and forth.
Data preparation (high level)
- Cleaning: missing values, noise/outliers, inconsistent naming.
- Transformation: common formats, scaling, feature selection, dimensionality reduction.
Evaluation & deployment
- Ask whether performance (e.g. accuracy) is good and patterns are meaningful, not spurious.
- Poor results → revisit earlier phases; good results → integrate into software systems (possibly re-implemented).
Three modelling settings (as in ML1)
Each training example has labels (targets) — e.g. fraud vs not fraud. Tasks include classification and regression. The course covers many supervised algorithms.
No labels required — e.g. clustering groups similar customers or transactions. The slides note the gap between having only unlabeled structure and wanting a label like “fraud”: if you need to predict fraud, you typically need labeled fraud/non-fraud data for supervised learning.
Mentioned in the lecture roadmap; the unit focuses primarily on supervised and unsupervised methods.
.ipynb) implement data understanding → preparation → modelling steps in code; Python + NumPy + pandas are the tools for loading and shaping data before sklearn models.
Python essentials
Mirrors w1.ipynb / w1.pdf: structures, builtins, loops, and functions.
Data structures
- list — ordered, mutable;
append,remove,count. - dict — key–value map; ideal for named fields.
- tuple — immutable sequence.
- set — unordered unique elements.
Note: Avoid shadowing builtins — e.g. do not name a variable list if you then mean the type list (the notebook’s first print uses the name list before reassigning to a real list; use my_list = [1,2,3] instead).
Useful builtins (from the lab)
len(seq)— length.str(n)— convert to string for concatenation with+.help(fn)— documentation in the interpreter / Jupyter.
Control flow
- Indentation defines blocks.
for i in range(n):andwhile cond:.if/elif/elsefor branching.- List comprehensions:
[x for x in xs if cond].
Functions
Define reusable blocks with def name(args):, optional return. Parameters are passed by assignment; keep functions small and test them in notebook cells.
def greet(name):
print("Hello " + name)
return True
Why it matters: scikit-learn and pandas are Python-first; fluent basics reduce time spent on syntax errors in assignments.
xs and threshold t. Which is the most idiomatic way to build a new list of all values strictly greater than t?NumPy arrays
Homogeneous ndarray; vectorised ops; index from 0. Install with conda/pip if needed (see Canvas Jupyter guide).
np.zeros((rows, cols)),np.ones(...), scalar math:2 * np.ones([3,4]).- Inspect
.shape,.size,.ndim. np.arange(start, stop)andnp.arange(start, stop, step)for evenly spaced values.
a[0] first element; a[1:2] slice (half-open interval). For 2D arrays, arr[row, col]; arr[:, 1:4] selects columns 1–3 for all rows (the : means “all along this axis”).
arr.reshape(r, c) changes layout; total element count must match. Use this to prepare matrices for linear algebra and sklearn (which expects 2D feature matrices).
import numpy as np
a = np.zeros([3, 4])
b = 2 * np.ones([3, 4])
c = np.arange(5, 10, 0.5)
d = b.reshape(2, 6)
sub = d[:, 1:4]
More examples: tools_numpy.ipynb (Géron).
a = np.zeros([3, 4]). What is a.size?pandas DataFrames
Two-dimensional, column-oriented tables — data, row index, and columns (possibly heterogeneous types).
Build from a dict of column name → list: pd.DataFrame(data). Select columns with a list: df[["Name", "Age"]].
iloc vs lociloc uses integer positions (e.g. df.iloc[[1]] for the second row). loc uses labels — set a meaningful index with set_index("Name"), then df.loc[["Peter"]].
In Jupyter, display(df) pretty-prints tables. For CSVs later in the course: pd.read_csv(...).
import pandas as pd
data = {"Name": ["John", "Anna"], "Age": [24, 13]}
df = pd.DataFrame(data)
df2 = df.set_index("Name")
# df.loc[["Anna"]]
"Age" column as a pandas Series. Which expression is correct?Lab workflow
- Activate the course Python environment (conda/venv as on Canvas).
- Install Jupyter per the Canvas document; open
w1.ipynblocally (recommended) so you can install packages. - Use Kernel → Restart & Run All to verify the notebook from a clean state.
- Read tracebacks: the last line is usually the error type; the lines above show the call stack.
- If
numpy/pandasis missing:pip installor conda install into the same interpreter Jupyter uses (see notebook comment forsys.executable+ pip). - Later weeks: theoretical exercises (paper-style) may mirror exam style — complete them even if not all covered in the 1-hour tutorial.
Chapter quizzes
Self-test and math questions for this week are in the Quiz Hub (practice or exam mode).