Lab · Overfitting过拟合练习 · 过拟合
Will this year’s curve work next year?今年的曲线明年还管用吗?
A community-centre officer has one number for each of 24 districts this year: how willing residents were to come to the centre’s activities, and what share of households live in public rental housing. The officer fits a curve so next year’s activity posters go to districts the curve marks as least willing. A curve that sits on all 24 dots can miss the next 400 districts drawn the same way. That too-close fit is overfitting.社区中心的同事今年有 24 个区的数字:居民有多愿意来参加活动,以及住公屋的家庭占多少。同事拟合一条曲线,好把明年的活动海报贴到曲线标成“最不愿意来”的区。一条把 24 个点都贴住的曲线,可能对不上按同样方式再抽的 400 个区。贴得太紧,就叫过拟合。
Why it matters.为什么要紧。 A curve that sits closer to this year’s 24 districts can miss next year’s. The bend is how far the curve is allowed to chase a single district. The two numbers under the chart are how far off the curve is this year, and how far off it is next year.一条更贴近今年这 24 个区的曲线,明年可能更离谱。弯曲,是允许曲线跟着单个区跑多远。图下面两个数,是今年偏了多少,明年又偏了多少。
Try this试一试 Move the bend from 1 to 3, then to 12. Watch the solid curve, and compare this year’s error with next year’s. Draw a new set of districts before you treat one curve as the poster plan.把弯曲从 1 调到 3,再调到 12。看那条实线,比较今年偏了多少、明年偏了多少。先再抽一批区,再决定海报贴去哪里。
How to read the chart怎么读这张图
If you read this wrong要是读错了
If the officer sends next year’s posters to the districts where this curve is lowest, the posters can miss the districts where people are actually least willing to come. The small error is only on this year’s 24 dots. A curve allowed to bend more can sit farther from next year’s districts.如果同事把明年的海报贴到这条曲线最低的区,海报可能错过真正最不愿意来的区。那个小误差只出在今年这 24 个点上。允许弯得更多的曲线,可以离明年的区更远。
What this picture is enough for这张图够说明什么
Enough to see this year’s error on the 24 districts and next year’s error move apart as the curve is allowed to bend more.看得出:曲线被允许弯得更多时,今年 24 个区上的误差和明年的误差会分开。
Not enough to decide where the posters go. You still need a later year of districts that nobody watched while choosing the bend, and a reason a polynomial — a curve allowed to bend a set number of times — is the right shape for willingness to come.还不够拿来决定海报贴去哪里。你还需要一批在挑选弯曲时没有盯着的下一年的区,也需要一个理由:多项式,也就是允许弯固定这么多次的曲线,为什么适合“愿意来”这个分数。
The equation, and where it stops式子、假设和边界
The picture is a made-up curve, not a real housing survey. X, the public-rental share, is drawn evenly between −1 and 1. Willingness is a smooth wave, sin(πX), plus some random jumpiness σe. The fitted curve is a polynomial of degree d: nearly a straight line when d is 1, and more bends as d grows. It is ordinary least squares, with no penalty pulling the slopes in. A Chebyshev basis and a re-orthogonalised QR only keep the arithmetic stable. They do not change the model, and there is no regularization.这张图是编出来的曲线,不是真实的房屋调查。横轴 X 是公屋住户占比,在 −1 和 1 之间均匀抽取。愿意来的程度是一条平滑的波,sin(πX),再加上一点随机的跳动 σe。拟合用的是 d 次多项式:d 为 1 时几乎是直线,d 越大弯越多。这是普通最小二乘,没有把斜率往回拉的惩罚。切比雪夫基和再正交的 QR 只是让计算更稳,模型还是那个多项式,也没有正则化。
Mean squared error (MSE) averages the squared misses. A miss of 4 counts more than four misses of 1. In exact arithmetic, a more flexible curve cannot make the smallest possible error on the 24 districts any larger. The error on the 400 need not get better. One draw does not guarantee a neat U-shape.均方误差(MSE)是把每次偏了多少先平方、再平均。偏了 4 的一次,比偏了 1 的四次更重。在精确计算里,曲线更灵活,并不能让这 24 个区上的最小误差变大。那 400 个区上的误差却不一定变好。抽一次,不能保证误差画出整齐的 U 形。
Because you can change the bend while watching the 400 districts, those 400 are a validation set, not a final test that nobody has touched. Real work also needs a sensible split — by year, or by who the residents are — and one last check after the curve is already fixed. This page is not every modern machine-learning model.你可以一边看那 400 个区、一边改弯曲,所以它们是验证集,不是一份曲线定下来之后才打开的测试。真要拿去贴海报,还得按年份或按居民是谁来划分,并在曲线定了之后再做一次谁也没看过的核对。这一页不是所有现代机器学习模型。
Easy to get wrong.容易踩的坑。 Textbooks talk about a bias–variance tradeoff. A stiff curve can miss the wave in a similar way every time you draw. A very flexible curve can change shape from one draw to the next. This page does not split the error into those two pieces. It shows the error on the 24 and the error on the 400, for one bend at a time. It is easy to treat the 400, which you watched while choosing the bend, as an untouched test. It is easy to expect a neat U in every draw. It is easy to assume a wiggly curve must have been regularized, that is, pulled back by a penalty. This fit is ordinary least squares. Flexibility here is the bend, not a penalty.教材里会讲偏差—方差权衡:僵硬的曲线每次抽取都用差不多的方式错过那条波;特别灵活的曲线,换一批区就会换一个形状。这一页不把误差拆成那两块。它一次只给你看一个弯曲下,24 个区的误差和 400 个区的误差。容易把你挑选弯曲时一直盯着的那 400 个区,当成没碰过的测试。容易指望每一次都出现整齐的 U 形。容易以为扭来扭去的曲线一定做了正则化,也就是被惩罚往回拉过。这里的拟合是普通最小二乘。灵活来自弯曲次数,不是惩罚。