← Back to AI & Machine Learning track
Reference

AI & Machine Learning Cheat Sheet

The AI and machine learning track on one page — landscape, classical models, evaluation, a little deep learning, language and generative ideas, and MLOps. Sketches are pure Python; a library call is marked as a sketch and is not required.

Landscape
Four words
AI      a system that does work we call intelligent
ML      patterns learned from examples, not only hand-written rules
DL      ML with multi-layer neural networks
GenAI   models that generate text, images, audio, or more
How the track is grouped
math you reuse
data, features, and splits
classical models and how to score them
a little deep learning
language, vision, and generative models
MLOps and the cloud platforms named later
Roles, roughly
modelling     choose the model and the metric
data          examples worth learning from
production    train, deploy, watch, retrain
Cloud names in the track: Azure ML, SageMaker, Vertex AI.
Math you reuse
Dot and cosine
def dot(a, b):
    return sum(x * y for x, y in zip(a, b))

def cosine(a, b):
    na = sum(x * x for x in a) ** 0.5
    nb = sum(y * y for y in b) ** 0.5
    if na == 0 or nb == 0:
        return 0.0
    return dot(a, b) / (na * nb)
Slope and chain rule
def numeric_slope(f, x):
    h = 0.00001
    return (f(x + h) - f(x - h)) / (2 * h)

# gradient: the slope in every weight at once
# chain rule: multiply local slopes across layers
Bayes and entropy
import math

# P(A|B) = P(B|A) * P(A) / P(B)

def entropy(probs):
    return -sum(p * math.log2(p) for p in probs if p > 0)
Shapes
vector     a list of numbers
matrix     a table; multiplying transforms a vector
tensor     the same idea in more axes
eigenvector  a direction a transform only stretches
Data before the model
Three splits
def train_val_test(rows, train=0.6, val=0.2):
    n = len(rows)
    i = int(n * train)
    j = int(n * (train + val))
    return rows[:i], rows[i:j], rows[j:]
# shuffle first when the file is sorted
Scale
def mean(xs):
    return sum(xs) / len(xs)

def standardise(xs):
    mu = mean(xs)
    var = sum((x - mu) ** 2 for x in xs) / len(xs)
    sd = var ** 0.5 or 1.0
    return [(x - mu) / sd for x in xs]

def minmax(xs):
    lo, hi = min(xs), max(xs)
    span = (hi - lo) or 1.0
    return [(x - lo) / span for x in xs]
Categories and gaps
def one_hot(value, levels):
    return [1 if value == level else 0 for level in levels]

# missing: drop or fill, and write down which
# outlier: far from the rest — error, or the point
Classical machine learning
Linear regression
def predict_linear(x, w, b):
    return sum(wi * xi for wi, xi in zip(w, x)) + b
# learn w and b so predictions sit near y
Logistic regression
def sigmoid(z):
    if z >= 0:
        return 1 / (1 + 2.718281828 ** (-z))
    ez = 2.718281828 ** z
    return ez / (1 + ez)
# probability = sigmoid(w·x + b)
# a threshold (often 0.5) makes it a class
Neighbours
def knn(query, rows, k=3):
    # each row is (features, label)
    def dist(feats):
        return sum((a - b) ** 2 for a, b in zip(query, feats))
    return sorted(rows, key=lambda row: dist(row[0]))[:k]
Other classical names
decision tree   a chain of if / else on features
naive Bayes     features treated as independent given the class
SVM             a boundary with the widest margin
# sketch — library not required to read this page
# LogisticRegression().fit(X, y)
Ensembles and structure
Many models
random forest      many trees, bootstrap rows, random features
gradient boosting  each new tree fits the remaining error
XGBoost, LightGBM  fast boosting — names in the track
# sketch only; nothing here imports them
Unsupervised
k-means
  pick k centres
  assign each point to the nearest
  move each centre to its members
  repeat
PCA
  new axes that keep the most variance
  fewer columns, most of the spread
Evaluation
Counts that matter
def confusion(pairs):
    # pairs of (truth, pred) in {0, 1}
    tp = sum(t == 1 and p == 1 for t, p in pairs)
    fp = sum(t == 0 and p == 1 for t, p in pairs)
    fn = sum(t == 1 and p == 0 for t, p in pairs)
    tn = sum(t == 0 and p == 0 for t, p in pairs)
    return tp, fp, fn, tn

def precision_recall(tp, fp, fn):
    prec = tp / (tp + fp) if tp + fp else 0
    rec = tp / (tp + fn) if tp + fn else 0
    return prec, rec
Beyond accuracy
Accuracy hides a rare class.
F1 is the harmonic mean of precision and recall.
ROC: true-positive rate vs false-positive rate
    as the threshold moves.
AUC: area under that curve. 0.5 is chance.
Calibration: if you say 0.8, about 80% should be positive.
Regression scores
def mae(y, yhat):
    return sum(abs(a - b) for a, b in zip(y, yhat)) / len(y)

def rmse(y, yhat):
    err = sum((a - b) ** 2 for a, b in zip(y, yhat)) / len(y)
    return err ** 0.5

def r_squared(y, yhat):
    mu = sum(y) / len(y)
    tot = sum((a - mu) ** 2 for a in y)
    res = sum((a - b) ** 2 for a, b in zip(y, yhat))
    return 1 - res / tot if tot else 0
Trust the number
Cross-validation rotates which fold is held out.
Bias: the model is too rigid for the pattern.
Variance: it memorises this sample.
Hyperparameter search (Optuna is named in the track)
  tunes knobs, not the test set.
SHAP and LIME: which features pushed one prediction.
A little deep learning
One unit
def relu(z):
    return z if z > 0 else 0

def unit(z, w, b):
    total = sum(wi * zi for wi, zi in zip(w, z)) + b
    return relu(total)
# a network stacks these
# backprop is the chain rule on every weight
Loss and steps
squared error     for a number
cross-entropy     for a class
SGD               step against the gradient, in batches
Adam / AdamW      per-weight step sizes
                  AdamW also decays weights
Architectures, named
batch norm    keep a layer's inputs on a stable scale
dropout       ignore random units while training
CNN           local filters, used for images
RNN / LSTM    a state carried along a sequence
attention     weigh other positions, not only the last one
Language and generative ideas
Tokens and vectors
token       a piece of text: word, subword, or bytes
embedding   a vector for that token
similar tokens land nearer in that space
# reuse cosine from the math section to compare them
Readers and writers
BERT-style encoder   reads the whole span both ways
decoder LLM          predicts the next token
LoRA / adapters      small added weights for a fine-tune
prompt               the instruction plus the evidence you pass
Retrieve, then speak
RAG
  retrieve passages
  generate an answer grounded in them
An agent may call a tool, read the result, and continue.
Advanced RAG is a later title, not a second idea.
Generative families
GAN          generator vs discriminator
VAE          encode to a distribution, sample, decode
diffusion    learn to denoise, one step at a time
LLM words    pretraining, RLHF, scaling
multimodal   CLIP, LLaVA — image and text together
MLOps
From rows to a run
pipeline       data, features, train, evaluate, register
feature store  the same definition in train and in serve
tracking       MLflow or Weights & Biases:
               a metric tied to a commit and a dataset
DVC            version the data, not only the code
Drift
def drifted(today, baseline, tol=0.2):
    mu = lambda xs: sum(xs) / len(xs)
    return abs(mu(today) - mu(baseline)) > tol
# inputs or labels moved away from training
# a retrain trigger is a rule you wrote down
Ship it
# sketch — no server is started by this page
# an HTTP handler loads a saved model
# and returns one prediction per request
# cloud CI in the track: Azure ML, SageMaker, Vertex AI
Watch latency, errors, and the drift rule after launch.

Projects to build

The fastest way to make this stick. Build one, then put it in your portfolio.

Beginner

Cosine on three sentences

Turn three short sentences into word-count vectors with the standard library. Rank them against a query using your own dot product and cosine. No NumPy.

vectorsdot productsimilarity
Beginner

Score a guess

Type two short lists, y and yhat. Print MAE, RMSE, and R² from the formulas on this page, and say which error worries you more.

MAERMSER²
Intermediate

k-NN on a dozen rows

Hand-label about twelve rows with two numeric features. Hold out three. Predict them with the pure-Python k-NN sketch and describe one mistake.

distanceholdoutk-NN
Intermediate

Imbalanced evaluation card

Invent a yes/no problem where "no" is common. From made-up truth and predictions, compute precision and recall and write why accuracy would green-light a bad model. Mention calibration in one sentence.

confusion countsprecisionrecall
Advanced

One-page system design

Write the decision, the features, the split, the metric, where drift would show up, and what triggers a retrain. Optional: ten pure-Python steps of gradient descent for y ≈ w·x on a handful of points. Do not import a training framework.

ML system designdriftbaselines