Paper 2 AI and ML Drills

These are original topic-local Paper 2-style drills, not a complete four-question Paper 2. For this 2027 module, question style is inferred from general Computing assessment patterns rather than from past AI/ML papers. They use small datasets so that outputs can be checked exactly.

Some early questions ask you to hand-code small helper steps so that the algorithm idea is visible. Later questions use scikit-learn, which better matches library-based practical ML work.

Detailed answers are in Paper 2 AI and ML Answers.

Revise the topic hub first:

Questions

Question 1: Absolute Distance

Write a function distance(a, b) that returns the absolute distance between two numeric values.

Test:

print(distance(10, 3))
print(distance(4, 9))

Expected output:

7
5

[3]

Question 2: Nearest Training Example

Write a function nearest_example(training, value).

training is a list of tuples in this format:

[(feature_value, label), ...]

The function should return the label of the training example nearest to value.

Assume training contains at least one tuple and the nearest example is unique for the test data.

Test:

training = [(20, "not spam"), (25, "not spam"), (180, "spam")]
print(nearest_example(training, 30))
print(nearest_example(training, 170))

Expected output:

not spam
spam

[6]

Question 3: k-NN Classification

Write a function classify_knn(training, value, k) that:

  1. calculates the distance from value to each training example;
  2. sorts examples by distance;
  3. uses the first k examples;
  4. returns the majority label.

Assume training is non-empty, 1 <= k <= len(training), and the test data will not produce an unresolved majority-label tie. If equal distances occur at the boundary, the original list order may be used.

Test:

training = [(20, "not spam"), (25, "not spam"), (180, "spam"), (200, "spam")]
print(classify_knn(training, 30, 3))
print(classify_knn(training, 190, 3))

Expected output:

not spam
spam

[10]

Question 4: Accuracy

Write a function accuracy(predicted, actual) that returns the proportion of predictions that are correct.

Assume predicted and actual have the same positive length.

Test:

print(accuracy(["spam", "not spam", "spam"], ["spam", "spam", "spam"]))

Expected output:

0.6666666666666666

[5]

Question 5: Find Nearest Centre

Write a function nearest_centre(value, centres) that returns the index of the nearest centre.

Assume centres contains at least one numeric value. If two centres are equally near, return the lower index.

Test:

print(nearest_centre(3, [2, 10]))
print(nearest_centre(9, [2, 10]))

Expected output:

0
1

[5]

Question 6: k-Means Assignment Step

Write a function assign_clusters(points, centres) that returns a list of nearest-centre indexes for all points.

You may call your nearest_centre function from Question 5.

Test:

print(assign_clusters([2, 3, 10, 11], [2, 10]))

Expected output:

[0, 0, 1, 1]

[6]

Question 7: Mean of a Cluster

Write a function mean(values) that returns the average of a non-empty list.

Test:

print(mean([2, 3]))
print(mean([10, 11]))

Expected output:

2.5
10.5

[4]

Question 8: Update k-Means Centres

Write a function update_centres(points, assignments, k) that returns a list of new centre values.

Assume:

  • points and assignments have the same non-zero length;
  • every cluster index from 0 to k - 1 has at least one assigned point;
  • all assignment values are valid indexes in that range.

Test:

points = [2, 3, 10, 11]
assignments = [0, 0, 1, 1]
print(update_centres(points, assignments, 2))

Expected output:

[2.5, 10.5]

[8]

Question 9: k-NN Parameter Tuning

Use scikit-learn to compare two values of n_neighbors for a k-NN classifier.

Use this training set:

X_train = [[1], [2], [3], [8], [9], [10]]
y_train = ["low risk", "low risk", "high risk", "high risk", "high risk", "high risk"]

Use this validation set:

X_valid = [[3.2], [7.8]]
y_valid = ["low risk", "high risk"]

Write code that:

  1. creates and fits one KNeighborsClassifier with n_neighbors=1;
  2. creates and fits one KNeighborsClassifier with n_neighbors=3;
  3. prints the validation accuracy for each model;
  4. selects the better value of k;
  5. states that this comparison is parameter tuning.

Expected output:

k = 1 accuracy: 0.5
k = 3 accuracy: 1.0
better k: 3

[8]

Question 10: scikit-learn Integration

Use scikit-learn to complete the following tasks. In each part, X contains feature rows. For supervised learning, y contains the labels.

Part A: k-NN prediction [4]

Create a KNeighborsClassifier with n_neighbors=3, train it on:

X = [[20], [25], [180], [200]]
y = ["not spam", "not spam", "spam", "spam"]

Print predictions for [ [30] ] and [ [190] ].

Part B: k-means centres [3]

Create a KMeans model with n_clusters=2, random_state=0, and n_init=10. Fit it on:

X = [[2], [3], [10], [11]]

Print the sorted cluster centres rounded to one decimal place. Numeric cluster labels may be assigned in either order, so do not check raw cluster labels.

Part C: train/test split and cross-validation [5]

Use train_test_split and cross_validate with:

X = [[1], [2], [3], [10], [11], [12]]
y = ["A", "A", "A", "B", "B", "B"]

Use test_size=0.33, random_state=0, stratify=y, KNeighborsClassifier(n_neighbors=1), and cv=3.

Print the test accuracy and the rounded cross-validation test scores.

Briefly state what .score(...) and results["test_score"] represent.

Expected output:

not spam
spam
[2.5, 10.5]
1.0
[1.0, 1.0, 1.0]

[12]

Review Checklist

After attempting these tasks, check whether you can:

  • calculate simple distances;
  • use labelled training examples;
  • implement k-NN majority voting;
  • calculate accuracy;
  • assign points to nearest k-means centres;
  • update centres using the mean;
  • compare simple k-NN parameter values using validation accuracy;
  • use scikit-learn for basic k-NN, k-means, train/test split, and cross-validation tasks;
  • run small tests and compare expected output.