A4.3.1 · Machine learning approaches (HL only)
A4.3.1 · 机器学习方法(仅 HL)
How Linear Regression Predicts Continuous Outcomes
线性回归如何预测连续结果
Not every prediction problem is about sorting things into categories. Sometimes the answer you want is a number that could fall anywhere along a range, and linear regression is the simplest, most interpretable tool built for exactly that.
并非每一个预测问题都是把事物归入类别。有时候你想要的答案是一个可能落在某个范围内任意位置的数值,而线性回归正是为此设计的最简单、最容易解释的工具。
Explainer
讲解
Linear regression models the relationship between one or more independent variables and a dependent variable as a straight line, then uses that line's equation to predict the dependent variable's value for new, unseen inputs.
线性回归将一个或多个自变量与一个因变量之间的关系建模为一条直线,然后利用这条直线的方程,针对新的、未曾见过的输入预测因变量的值。
A continuous outcome is a value that can fall anywhere along a range rather than being sorted into a fixed set of categories, a temperature, a price, a duration, a weight. That's the kind of prediction linear regression is built for; it's the wrong tool the moment the outcome you actually want is a category, like "spam" or "not spam".
一个连续结果是指可以落在某个范围内任意位置的数值,而不是被归入一组固定类别的值,例如温度、价格、时长、重量。这正是线性回归所擅长预测的类型;一旦你实际想要的结果是一个类别,比如"垃圾邮件"或"非垃圾邮件",线性回归就成了错误的工具。
A gardening club wants to predict how tall a sunflower seedling will grow. Each week since planting, a member measures its height in centimetres. Weeks since planting is the independent variable, the one used to make the prediction; height is the dependent variable, the one being predicted, because how tall the seedling grows is treated as being driven by how many weeks have passed.
某园艺俱乐部想要预测一株向日葵幼苗最终能长多高。自种植以来的每一周,一名会员都会测量它的高度(单位:厘米)。种植后的周数是自变量,用来进行预测;高度是因变量,是被预测的对象,因为幼苗长多高被视为由经过的周数所驱动。
Worked example
示例讲解
Slope, intercept and the variables
斜率、截距与变量
The line of best fit through the sunflower's measurements takes the form height = intercept + slope × weeks. Click each part to see what it means and what it works out to for this data set.
穿过向日葵测量数据的最佳拟合直线形式为 height = intercept + slope × weeks(高度 = 截距 + 斜率 × 周数)。点击每个部分,查看它的含义以及在这组数据中的具体数值。
Try it yourself
动手试一试
Fitting a line with numpy
用 numpy 拟合一条直线
The slope and intercept above weren't guessed, they come from an actual calculation: the line that minimises the total squared distance between itself and every measured point. This runs that calculation on the sunflower's six weekly measurements, then uses the result to predict a seventh value the club hasn't measured yet.
上面的斜率和截距并非凭空猜测,而是来自一次真实的计算:找到一条使自身与每一个测量点之间的总平方距离最小的直线。这里对向日葵的六周测量数据执行这项计算,然后用结果预测俱乐部尚未测量过的第七个数值。
Running in offline fallback mode. This example needs numpy, which only the full Python engine (Pyodide) provides, so it isn't available here. If Pyodide can't load, usually a blocked school network or no internet connection, pressing Run will show a clear message explaining why, rather than a confusing crash.
正在以离线备用模式运行。此示例需要 numpy,而只有完整的 Python 引擎(Pyodide)才能提供,因此这里无法使用它。如果 Pyodide 无法加载(通常是因为学校网络被屏蔽或没有网络连接),按下"运行"会显示一条清楚说明原因的提示,而不是令人困惑的报错。
This prints a slope of about 4.34, an intercept of about 7.8, and an R-squared of about 0.99, an almost perfect fit. Try changing one height value to something that breaks the pattern (say, 34 to 5) and re-running: the R-squared drops sharply, and the week-10 prediction swings with it, exactly the kind of thing a single bad measurement can do to a small data set.
这会打印出约 4.34 的斜率、约 7.8 的截距,以及约 0.99 的 R 平方值,几乎是完美拟合。试着把某个高度值改成会打破规律的数字(例如把 34 改成 5)再重新运行:R 平方值会大幅下降,第 10 周的预测值也会随之剧烈波动,这正是一次糟糕的测量会对一个小数据集造成的影响。
Worked example
示例讲解
Fitting and plotting with scikit-learn
用 scikit-learn 拟合并绘图
The calculation above was done by hand, one NumPy line at a time, to show exactly where the slope and intercept come from. In practice, most real work uses scikit-learn's built-in LinearRegression instead, and matplotlib to actually see the line against the data. This fits the same six sunflower measurements and plots the result.
上面的计算是逐行手动用 NumPy 完成的,目的是准确展示斜率和截距的来源。而在实际工作中,大多数情况会改用 scikit-learn 内置的 LinearRegression,并用 matplotlib 实际查看这条线与数据的对比。这里对同样的六个向日葵测量值进行拟合并绘制结果。
import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression # The same six sunflower measurements, fitted with scikit-learn instead of by hand weeks = np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1) height = np.array([12, 17, 20, 26, 29, 34]) model = LinearRegression() model.fit(weeks, height) print("Slope:", round(model.coef_[0], 2)) print("Intercept:", round(model.intercept_, 2)) print("R-squared:", round(model.score(weeks, height), 2)) # Predict the height at week 10, the same value calculated by hand earlier week10 = np.array([[10]]) print("Predicted height at week 10:", round(model.predict(week10)[0], 2)) # Plot the six measurements against the fitted line of best fit plt.scatter(weeks, height, color="green", label="Measured height") line_x = np.array([[0], [10]]) line_y = model.predict(line_x) plt.plot(line_x, line_y, color="orange", label="Line of best fit") plt.xlabel("Weeks since planting") plt.ylabel("Height (cm)") plt.title("Sunflower growth: linear regression") plt.legend() plt.show()
Running this prints the same values as the numpy version above, Slope: 4.34, Intercept: 7.8, R-squared: 0.99, Predicted height at week 10: 51.23, since LinearRegression fits a line the same way (minimising squared distance) as the manual calculation. The plot window then opens showing the six green points with the orange line of best fit running through them. This isn't wired into this page's own in-browser console: between scikit-learn and matplotlib, that's a large amount of supporting library to download just for one page, so try this one in your own Python setup instead (pip install scikit-learn numpy matplotlib).
运行这段代码会打印出与上面 numpy 版本相同的数值,Slope: 4.34、Intercept: 7.8、R-squared: 0.99、Predicted height at week 10: 51.23,因为 LinearRegression 拟合直线的方式(最小化平方距离)与手动计算完全相同。随后会打开一个绘图窗口,显示六个绿色数据点以及穿过它们的橙色最佳拟合直线。这里没有接入本页自身的浏览器内控制台:scikit-learn 与 matplotlib 加起来是相当大量的支持库,仅为一个页面下载并不划算,因此请在你自己的 Python 环境中尝试(pip install scikit-learn numpy matplotlib)。
Explainer
讲解
How well does the line fit?
这条线拟合得有多好?
The R-squared value measures how much of the variation in the dependent variable the line actually accounts for. It ranges from 0 to 1: a value near 1 means the line tracks the real data closely, a value near 0 means the line barely captures the pattern at all, and the model's predictions shouldn't be trusted much beyond that.
R 平方值衡量这条线实际解释了因变量中多少比例的变化。它的取值范围是 0 到 1:接近 1 的值意味着这条线紧密贴合真实数据,接近 0 的值意味着这条线几乎无法捕捉到规律,模型的预测在这种情况下不应被过多信任。
For the sunflower, an R-squared of about 0.99 means the straight-line model explains almost all of the week-to-week variation in height, strong evidence the growth really has been roughly linear across the weeks measured. A much lower value would be a signal to question whether a straight line was ever the right shape for this data in the first place.
对于向日葵而言,约 0.99 的 R 平方值意味着这条直线模型解释了几乎所有逐周高度变化,这有力地证明了在测量的这些周内,生长确实大致呈线性。一个低得多的数值则会提示我们,应质疑直线是否从一开始就是适合这份数据的形状。
Check your understanding
检验你的理解
The sunflower model's slope works out to about 4.34. What does this figure represent?
向日葵模型的斜率约为 4.34。这个数值代表什么?
Common mistake
常见错误
RecurringA genuinely documented pattern: candidates apply linear regression to a data set without first checking that the relationship is actually roughly linear. Linear regression only makes sense where a straight line is a reasonable description of the data; forcing one onto a relationship that curves, plateaus, or has no real trend at all produces a line of best fit that fits nothing well, and predictions built on it, especially predictions beyond the range of data actually measured, can be seriously misleading.
反复出现这是一个真实记录在案的模式:考生在没有先检查关系是否真的大致呈线性的情况下,就对数据集应用了线性回归。只有当直线是对数据的合理描述时,线性回归才有意义;把直线硬套到一个曲折、趋于平缓或根本没有真实趋势的关系上,得到的最佳拟合直线什么都拟合不好,基于它建立的预测,尤其是超出实际测量数据范围的预测,可能会产生严重误导。
Applies the model blindly: "Linear regression was used to predict the sunflower's height after a full year, since it worked well for the first six weeks." Assumes a trend measured over a few weeks of early growth will keep extending in a straight line indefinitely, when real plant growth typically slows and plateaus, a pattern no straight line can capture.
盲目套用模型: "由于线性回归在最初六周表现良好,便用它来预测向日葵一整年后的高度。" 这假设了在早期生长的几周内测得的趋势会无限延续为一条直线,而实际植物生长通常会放缓并趋于平稳,这种模式是任何直线都无法捕捉的。
Checks the assumption first: "Before predicting further weeks, the six measurements were plotted to check the relationship stayed roughly linear; the model's predictions are only trusted within, or close to, the range of weeks actually measured, since sunflower growth is known to slow down later on." Treats linearity as something to verify, and extrapolation as something to be cautious about, not a free extension of the line.
先检验假设: "在预测更远的周数之前,先将六个测量值绘制成图,检查关系是否保持大致线性;由于已知向日葵的生长会在后期放缓,模型的预测只在实际测量周数范围内,或接近该范围时才被信任。" 把线性关系视为需要验证的事项,把外推视为需要谨慎对待的事,而不是直线可以随意延伸的自由。
Revision questions
复习题
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
采用历年真题风格。写下答案,然后展开评分要点给自己打分。
[2]State what the independent variable and the dependent variable represent in a linear regression model.
[2]陈述在一个线性回归模型中,自变量和因变量分别代表什么。
Marking points
评分要点
- The independent variable is the input feature used to make the prediction (plotted on the x axis).
- The dependent variable is the outcome being predicted, whose value depends on the independent variable (plotted on the y axis).
- 自变量是用来做预测的输入特征(画在 x 轴上)。
- 因变量是被预测的结果,其值取决于自变量(画在 y 轴上)。
[4]A linear regression model predicting a delivery driver's daily earnings from hours worked has a slope of 18.5 and an intercept of 12. Explain what each of these two values means in context.
[4]某线性回归模型根据外送员的工作小时数预测其日收入,其斜率为 18.5,截距为 12。解释这两个数值在此情境中各自的含义。
Marking points
评分要点
- The slope of 18.5 means that for each extra hour worked, the model predicts earnings rise by 18.5 (currency units).
- The intercept of 12 means that for zero hours worked the model predicts earnings of 12.
- The intercept here may have little real meaning (a driver working zero hours), so it is mainly there to position the line.
- Together they give the prediction: earnings = 12 + 18.5 times hours.
- 斜率 18.5 表示每多工作一小时,模型预测收入增加 18.5(货币单位)。
- 截距 12 表示工作零小时时,模型预测收入为 12。
- 这里的截距可能没有多少实际意义(司机工作零小时),所以它主要用来定位这条线。
- 两者合起来给出预测:收入 = 12 + 18.5 乘以小时数。
[6]A café uses linear regression to predict weekend ice-cream sales from that day's temperature, based on ten past weekends. Explain how the R-squared value could be used to judge whether this model should be trusted, and describe one reason the model might still make an unreliable prediction on an unusually hot day far outside the range of temperatures in the original ten weekends.
[6]某咖啡馆根据过去十个周末的数据,使用线性回归通过当天的气温预测周末冰淇淋的销量。解释如何利用 R 平方值判断这个模型是否值得信赖,并说明在某个异常炎热、远超原本十个周末气温范围的日子里,该模型的预测仍可能不可靠的一个原因。
Marking points
评分要点
- R-squared is the fraction of the variation in weekend sales that the temperature model explains, from 0 to 1.
- An R-squared near 1 means temperature explains most of the sales variation, so the model is worth trusting; near 0 means it predicts little better than just using the average.
- The café should also check the value is stable, not driven by one unusual weekend.
- One reason not to trust it despite a high R-squared: only ten past weekends is a very small sample, so the fit could be coincidental and may not hold on new data.
- Other valid reasons: the relationship may not really be linear, the range of past temperatures may be narrow, or a lurking factor (a local event, school holidays) may drive both.
- High R-squared shows association, not causation, so acting on it assumes temperature is the real driver.
- R 平方是温度模型所解释的周末销售变异的比例,从 0 到 1。
- R 平方接近 1 表示温度解释了大部分销售变异,模型值得信任;接近 0 表示它的预测几乎不比直接用平均值好。
- 咖啡馆还应检查该值是否稳定,而不是由某个异常的周末所驱动。
- 尽管 R 平方高仍不该信任的一个理由:只有十个过去的周末是非常小的样本,拟合可能是巧合,在新数据上可能不成立。
- 其他有效理由:关系可能并非真的线性,过去温度的范围可能很窄,或潜在因素(本地活动、学校假期)可能同时驱动两者。
- 高 R 平方表明关联,而非因果,所以据此行动是假定温度是真正的驱动因素。
Flashcards
记忆卡片
Active recall for this statement’s key terms.
针对本条考纲陈述关键术语的主动回忆练习。
Keep this statement’s practice with you
把这条考纲陈述的练习保存下来
Free account: unlock every revision question and flashcard for A4.3.1, and every other IB CS 2027 statement.
免费账户:解锁 A4.3.1 以及所有其他 IB CS 2027 考纲陈述的全部复习题和记忆卡片。