First exams 2027 · HL only 2027 年首次开考 · 仅 HL

A4.3.3 · Machine learning approaches (HL only)

A4.3.3 · 机器学习方法(仅 HL)

The Role of Hyperparameter Tuning

超参数调优的作用

A model is never finished the first time it trains. Its performance is measured against a benchmark, and a set of dials fixed before training, the hyperparameters, are turned and re-tested until that performance is as good as it can be.

模型第一次训练完并不算完成。它的表现要对照一个基准来衡量,而一组在训练之前就定好的旋钮,也就是超参数,会被反复调整、反复测试,直到表现达到最好。

Linking to TOK: when a model scores higher on a metric, has it truly learned more, or only become more functional on that one task? A model can score well while still failing badly on cases the training data barely covered.

联系 TOK(知识论): 当一个模型在某个指标上得分更高时,它是真的学到了更多,还是只是在这一项任务上变得更管用了?一个模型完全可能在某个指标上得分很高,却在训练数据几乎没覆盖到的情况上严重失败。

HL only 仅 HL Typically 3–5 marks · AO3 通常 3–5 分 · AO3 Command term: explain 命令词:explain(解释)

Explainer

讲解

Parameters vs hyperparameters

参数与超参数

Df

A parameter (a weight, a bias) is a value the training process works out on its own, by fitting the model to the data. A hyperparameter is a value set BEFORE training begins, that controls how the learning happens. Hyperparameter tuning means trying out different hyperparameter values and comparing how well each one performs, to land on the combination that works best.

参数(权重、偏置)是模型在训练过程中从数据中学到的值。超参数是在训练开始之前设定好的值,它控制着学习是如何进行的。超参数调优是尝试不同的超参数值、比较表现,从而找出效果最好的组合的过程。

AlgorithmA hyperparameter it needs set in advance
K-Nearest NeighboursThe number of neighbours, k
Decision treeThe maximum depth of the tree
K-means clusteringThe number of clusters, k
Neural networkThe learning rate, the number of hidden layers, the activation function
算法它需要事先设定的一个超参数
K 最近邻邻居的数量 k
决策树树的最大深度
K-均值聚类簇的数量 k
神经网络学习率、隐藏层的数量、激活函数

Try it yourself

动手试试

Build a confusion matrix

搭一张混淆矩阵

A factory camera checks bottles for cracks. A positive result means "the model flags this bottle as cracked". Type in counts for a batch of 100 bottles and watch the four evaluation metrics update.

工厂里的一台相机检查瓶子有没有裂纹。阳性结果表示"模型把这个瓶子标记为有裂纹"。为一批 100 个瓶子输入各项计数,看看四个评估指标如何随之变化。

Predicted
cracked
预测
有裂纹
Actually cracked
True positive真阳性
False negative假阴性
Actually fine
False positive假阳性
True negative真阴性
Accuracy准确率
Precision精确率
Recall召回率
F1 scoreF1 分数

A missed crack (false negative) reaches a customer; a good bottle wrongly pulled (false positive) wastes stock. Try the two presets: one keeps accuracy respectable while recall collapses, the other keeps accuracy respectable while precision collapses. Accuracy alone hides both problems.

漏掉的裂纹(假阴性)会流到顾客手中;被误判的好瓶子(假阳性)会浪费库存。试试这两个预设:一个让准确率看起来还行,但召回率崩了;另一个让准确率看起来还行,但精确率崩了。单看准确率会把这两个问题都藏起来。

Explore

探索

Underfit, good fit, overfit

欠拟合、恰好合适、过拟合

Model complexity is itself controlled by hyperparameters (tree depth, number of neighbours, number of hidden layers). Drag along the complexity scale and watch what happens to training accuracy versus accuracy on new, unseen data.

模型的复杂度本身也是由超参数控制的(树的深度、邻居数量、隐藏层数量)。沿着复杂度刻度拖动,看看训练准确率与在新的、未见过的数据上的准确率会发生什么变化。

Simple简单 Complex复杂

Check your understanding

检验你的理解

A screening tool flags patients who may have a rare disease. Both missing a real case and raising a false alarm matter. Which single metric is the best overall summary?

一个筛查工具用来标记可能患有某种罕见疾病的患者。漏掉一个真实病例和发出一次假警报都很要紧。哪一个单一指标最能概括整体表现?

Common mistake

常见错误

Recurring pattern in evaluation answers 评估类答案中反复出现的模式

RecurringAnswers reach for accuracy and stop there, even when the data set is imbalanced (the rare class only makes up a small share of cases). A model that always predicts "no disease" can score 95% accuracy on a data set where disease is rare, while catching zero real cases. Precision and recall exist precisely because they look at the positive class specifically, not the whole data set at once.

反复出现答案只用准确率就止步了,即便数据集是不平衡的(稀有的那一类只占很小一部分案例)。一个永远预测"没有患病"的模型,在患病者稀少的数据集上完全可以得到 95% 的准确率,却一个真实病例都没抓到。精确率和召回率之所以存在,正是因为它们专门看阳性类,而不是一次性看整个数据集。

✕

Wrong: "The model is 95% accurate, so it is performing very well."

错误: "这个模型的准确率是 95%,所以表现非常好。"

✓

Right: "The model is 95% accurate, but the disease occurs in only 5% of cases, so a model that never predicts disease would also score 95%. Its recall must be checked separately to see whether it actually detects real cases."

正确: "这个模型的准确率是 95%,但这种疾病只出现在 5% 的病例中,所以一个从不预测患病的模型同样能得到 95% 的准确率。必须单独检查它的召回率,看它是否真的能检测出真实病例。"

Revision questions

复习题

Specimen-paper style. Write an answer, then reveal the marking points and mark yourself.

采用样卷风格。写下答案,然后展开评分要点给自己打分。

Question 1 · 2 marks 第 1 题 · 2 分

[2]State the difference between a parameter and a hyperparameter.

[2]说出参数和超参数之间的区别。

Question 2 · 4 marks 第 2 题 · 4 分

[4]A model correctly predicts 60 of 75 actual positives, and out of 90 total positive predictions, 60 are correct. Calculate the precision and the recall, showing your working.

[4]一个模型在 75 个真实阳性中正确预测了 60 个,而在总共 90 个阳性预测中,有 60 个是正确的。计算精确率和召回率,写出过程。

Question 3 · 3 marks 第 3 题 · 3 分

[3]Explain the difference between overfitting and underfitting.

[3]解释过拟合和欠拟合之间的区别。

Question 4 · 3 marks 第 4 题 · 3 分

[3]Explain why hyperparameter tuning is necessary even after a model has been trained once.

[3]解释为什么即使模型已经训练过一次,超参数调优仍然是必要的。

Flashcards

记忆卡片

Active recall for this statement’s key terms.

针对本条考纲陈述关键术语的主动回忆练习。

Prototype render · grounded in the IB CS 2027 subject guide and the Hodder and Oxford IB CS 2027 texts for A4.3.3 · not yet wired to live routes