Question bank › IB Computer Science › A4 Machine learning

Supervised learning: linear regression, K-NN, decision trees and evaluation metrics (HL)

Answer on paper first, then reveal the mark scheme one mark at a time.

1HardHL only4 marks

A k-nearest neighbours classifier (k = 3, Euclidean distance) is trained on the data below.

PointxyClass
P112A
P223A
P331B
P465B
P556B
P620A

Determine the class that this classifier assigns to the new point (3, 3). Show the distances you calculate. [4]

2StandardHL only4 marks

A machine learning model achieves 99% accuracy on its training data but only 71% accuracy on new, unseen data.

(a) Identify the problem with this model. [1]

(b) Explain why this problem occurs. [1]

(c) Suggest two ways to reduce this problem. [2]

3HardHL only5 marks

A classifier for a disease was tested on 200 patients. 60 patients had the disease: of these, 40 were correctly identified (TP) and 20 were missed (FN). Of the 140 healthy patients, 10 were wrongly flagged (FP) and 130 were correctly cleared (TN).

(a) Calculate accuracy, precision and recall. [3]

(b) A classifier that predicts 'healthy' for everyone achieves 70% accuracy on this sample. Explain why accuracy alone is a poor measure here and which measure is most important for this application. [2]

Stuck on Supervised learning: linear regression, K-NN, decision trees and evaluation metrics (HL)?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4HardHL only5 marks

A school uses linear regression to predict a student's final exam score, y (%), from the number of hours of revision, x. The training data came from students who revised for between 5 and 40 hours. The fitted model is

$$\hat{y} = 1.6x + 28 \qquad r^2 = 0.72$$

(a) Calculate the predicted score for a student who revises for 25 hours. [1]

(b) Explain what the slope and the intercept of this model mean in this context. [2]

(c) Interpret the value r2 = 0.72. [1]

(d) The model predicts a score of 124% for a student who revises for 60 hours. Explain why this prediction is unreliable. [1]

5CoreHL only4 marks

A music-streaming company uses the decision tree shown to predict whether a subscriber will Renew or Cancel their subscription.

SubscriberLogins per weekMonths subscribedReported a problem?
P53Yes
Q118No

(a) State the prediction the tree makes for subscriber P and for subscriber Q. [2]

(b) State what is represented by a leaf node in a decision tree. [1]

(c) Outline one advantage, for the company, of using a decision tree rather than a k-nearest neighbours (K-NN) classifier. [1]

Logins per week ≥ 3?Reported a problem?Months subscribed ≥ 12?CancelRenewRenewCancelYesNoYesNoYesNo
6StandardHL only5 marks

A web-hosting company records the mean response time of its server, y (ms), for different numbers of concurrent users, x. The graph shows the training data and the line of best fit produced by a linear regression model.

(a) Using the graph, determine the slope and the intercept of the regression line, and hence write the equation of the model. [2]

(b) Use your equation to predict the response time when there are 55 concurrent users. [1]

(c) For this model, r2 = 0.90. A second model, which predicts response time from the time of day, has r2 = 0.31 on the same data.

    Explain which model the company should use, with reference to the meaning of r2. [2]

0102030405060708090020406080100120140160180200220240260Number of concurrent users, xResponse time, y (ms)
7CoreHL only4 marks

Supervised learning can be used for regression or for classification.

(a) For each task, state whether it is a regression task or a classification task.

    (i) Predicting the remaining battery life, in hours, of a laptop. [1]

    (ii) Predicting whether a product review is positive, neutral or negative. [1]

(b) Describe how a linear regression algorithm chooses the line of best fit for a set of training data. [2]

8HardHL only6 marks

A music app classifies songs as Workout (W) or Relax (R) using a k-nearest neighbours (K-NN) classifier with Euclidean distance. Each song is described by a tempo score and an energy score, both on a scale of 0 to 10.

SongTempoEnergyClass
A54W
B36R
C66R
D14R
E73W
F87W
G09R

A new song, S, has tempo 4 and energy 4.

(a) State the class assigned to S when k = 1. [1]

(b) Determine the class assigned to S when k = 3. Show your working. [3]

(c) The developers add a third feature, song length in seconds (values range from 120 to 600), but do not rescale any of the features. Explain the effect this would have on the classifier. [2]

9HardHL only6 marks

An email provider tests its spam filter on 500 emails. The results are shown in the confusion matrix, where “positive” means the email is spam.

Actually spamActually not spam
Predicted spam728
Predicted not spam18402

(a) Calculate the precision and the recall of the filter. [2]

(b) Calculate the F1 score of the filter. Give your answer to 3 significant figures. [2]

(c) The provider can change a threshold that increases recall but reduces precision. Explain why the provider is likely to prefer high precision for this application. [2]

10StretchHL only7 marks

A data scientist trains decision trees to predict whether a loan applicant will repay. The hyperparameter maximum tree depth is varied from 1 to 8. The graph shows the accuracy of each tree on the training data and on a separate validation set.

(a) State the maximum depth that should be chosen. Justify your choice. [2]

(b) Explain, with reference to the graph, why the tree with maximum depth 1 is described as underfitting. [2]

(c) Explain, with reference to the graph, what happens to the model as the maximum depth increases from 4 to 8. [2]

(d) After choosing the depth, the data scientist evaluates the final model on a third set of data, the test set. Explain why the validation accuracy should not be reported as the model’s final performance. [1]

123456780.600.650.700.750.800.850.900.951.00Training accuracyValidation accuracyMaximum tree depthAccuracy

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session