Sorting fruit: the challenge that introduces machine learning
Pick one of the 3 fields on the left.
The headline
Data Science “uses Data Mining to discover hidden patterns, Machine Learning to build models, AI to make intelligent decisions”. Formally, mining describes a dataset D, learning chooses a function f ∈ 𝓗 from D, and AI turns f into an action a = π(s).
Timeline of the ideas on this page · click a chip
Walk one fruit through Data Mining, Machine Learning and AI
The ideas that make it work: features, labels, patterns and experience
1 Features and labels
2 Patterns (data mining)
3 Experience E improves P
Mitchell (1997). “A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E.” Card 3 is that sentence as a curve: T = sort fruit, P = test accuracy on 120 held-out fruit, E = labelled examples.
The defining rule: learn from data, not from explicit programs
learned: μ_c = (1/n_c) Σ_{yᵢ=c} xᵢ, θ* = argmin_θ (1/n) Σ L(f_θ(xᵢ), yᵢ)
if round < .50: banana elif tex > .45: orange elif color < .15: apple else: unknown
Classify, regress or cluster: decide it by hand and by eye
Worked example by hand: classify one fruit with k = 3 nearest neighbours
For each labelled fruit: subtract feature by feature, square, add, take the square root. Then sort the distances and let the k = 3 nearest vote.
Where the rewards add up: State, Action, Reward and the cumulative reward
The (S, A, R, S') tuple and its discounted arithmetic appear here.
Return G₀
Shares of |contribution|
Worked example from the lecture: a child learning to ride a bicycle, in 6 steps
The 5 components are S, A, R, S' and π; the sixth step is how π improves.
One Q-learning update
The family: supervised, unsupervised, reinforcement and transfer learning
Click a row above.
Transfer learning up close · click a block to freeze or fine-tune
The cost and the limits: the curse of dimensionality
P(cell empty) = (1 − r⁻ᵈ)ⁿ
edge for 10 % of data = 0.1^(1/d)
k-NN cost per query = O(n·d)
Apply it yourself: choose the right learning type and set up Anaconda
The decider
Procedure for a new problem · tick as you go
- Write T, P and E (Mitchell): the task, the measure, the data that counts as experience.
- Check the data: is there a label column? Discrete (classes) or continuous (how much / how many)?
- Count features d against examples n; if rᵈ ≫ n, select or reduce features first (section 08).
- Standardise numeric features so no unit (cm vs 0–1) dominates the distance.
- Hold out a test set before any fitting; report P only on it.
- Start with a simple baseline (k-NN, linear regression, k-means), then consider a pre-trained model.
Tutorial preparation: install Anaconda
Download the installer for your system from docs.anaconda.com/anaconda/install/, install it, then open Anaconda Prompt (Windows) or a terminal (macOS / Linux):
# one environment for the course conda create -n cds6354 python=3.11 -y conda activate cds6354 conda install scikit-learn pandas matplotlib jupyter -y jupyter notebook # opens in the browser
Reference implementation (scikit-learn)
import numpy as np from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor from sklearn.cluster import KMeans from sklearn.preprocessing import StandardScaler # classification: links, "free" words -> Spam (1), Not Spam (0) X = np.array([[8,7],[6,9],[9,5],[1,0],[2,1],[0,2]]) y = np.array([1, 1, 1, 0, 0, 0]) clf = KNeighborsClassifier(n_neighbors=3).fit(X, y) print(clf.predict([[5, 4]])) # -> [1] # regression: air-con hours, people -> monthly bill (how much) Xr = np.array([[2,2],[4,3],[6,4],[8,4],[3,1]]) yr = np.array([80, 130, 190, 240, 70]) reg = KNeighborsRegressor(n_neighbors=3).fit(Xr, yr) print(reg.predict([[6, 3]])) # mean of 3 neighbours # clustering: no labels; humans name the clusters later F = StandardScaler().fit_transform( np.array([[8,1],[7,2],[8,7],[9,8],[19,2],[20,1]])) km = KMeans(n_clusters=3, n_init="auto", random_state=0).fit(F) print(km.labels_) # cluster ids, not names