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).

🐍Python
📊NumPy
📄pandas
📚Labs
📐 Math Foundations → 🗺 Mind Map →

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.

FileRole
w1.ipynbPython refresher — lists, dicts, loops, NumPy, pandas
w1.pdfWeek summary / slides (if applicable)
ml1.pdfLecture notes (ML1)
Tip: Run cells top-to-bottom. If a package is missing, use 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

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 understandingData understandingData preparationModellingEvaluationDeployment. 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)

Supervised learning

Each training example has labels (targets) — e.g. fraud vs not fraud. Tasks include classification and regression. The course covers many supervised algorithms.

Unsupervised learning

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.

Reinforcement learning

Mentioned in the lecture roadmap; the unit focuses primarily on supervised and unsupervised methods.

Connect to labs: Jupyter notebooks (.ipynb) implement data understanding → preparation → modelling steps in code; Python + NumPy + pandas are the tools for loading and shaping data before sklearn models.
Quick check — CRISP-DM
Which phase comes first in the CRISP-DM lifecycle?
Business understanding
Data preparation
Deployment
Quick check — Learning settings
Which setting assumes that training examples include known labels (targets) for supervision?
Supervised learning
Unsupervised clustering only
Data understanding only

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): and while cond:.
  • if / elif / else for 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.

Quick check — Python
You have a list xs and threshold t. Which is the most idiomatic way to build a new list of all values strictly greater than t?
[x for x in xs if x > t]
xs.filter(t)
for x in xs: append (without building a new list in one expression)

NumPy arrays

Homogeneous ndarray; vectorised ops; index from 0. Install with conda/pip if needed (see Canvas Jupyter guide).

Creation & properties
  • np.zeros((rows, cols)), np.ones(...), scalar math: 2 * np.ones([3,4]).
  • Inspect .shape, .size, .ndim.
  • np.arange(start, stop) and np.arange(start, stop, step) for evenly spaced values.
Indexing & slicing

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”).

reshape

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).

Quick check — NumPy
Let a = np.zeros([3, 4]). What is a.size?
7
12
4

pandas DataFrames

Two-dimensional, column-oriented tables — data, row index, and columns (possibly heterogeneous types).

Construction & column selection

Build from a dict of column name → list: pd.DataFrame(data). Select columns with a list: df[["Name", "Age"]].

Row selection: iloc vs loc

iloc 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"]]
Quick check — pandas
You want the "Age" column as a pandas Series. Which expression is correct?
df["Age"]
df.Age.iloc (without further indexing)
df.Series("Age")

Lab workflow

  1. Activate the course Python environment (conda/venv as on Canvas).
  2. Install Jupyter per the Canvas document; open w1.ipynb locally (recommended) so you can install packages.
  3. Use Kernel → Restart & Run All to verify the notebook from a clean state.
  4. Read tracebacks: the last line is usually the error type; the lines above show the call stack.
  5. If numpy / pandas is missing: pip install or conda install into the same interpreter Jupyter uses (see notebook comment for sys.executable + pip).
  6. 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).

Open Quiz Hub Chapter flashcards