Lab · Regularization正则化练习 · 正则化
What stays in the note when slopes are pulled toward zero?斜率被往零拉时,说明里还留下什么?
Same 24 districts, and the same willingness to come. The curve is now fixed so it may bend 10 times. The officer no longer chooses the bend. They choose a pull, written λ, instead, then decide which bends to name in a one-page note. In the textbooks that pull is called regularization: a penalty that keeps the curve from hugging this year’s dots. Ridge shortens every slope. Lasso can set some slopes to exactly zero. A bar that disappears was dropped by this pull on these 24 districts.还是这 24 个区,还是“愿意来”这个分数。曲线现在固定为可以弯 10 次。同事不再选弯多少,而选一个拉力,写成 λ,再决定一页说明里要写哪些弯。教材里这个拉力叫正则化:给拟合加一点约束,不让曲线把今年的点贴太紧。岭回归把每个斜率缩短。套索可以把一些斜率恰好设成零。消失的一条,是这个拉力在这 24 个区上丢掉的。
Why it matters.为什么要紧。 A curve that chases this year’s jumpy scores can miss next year. Pulling the slopes toward zero makes the curve move less on a new set of districts. The price is a larger error this year. The page shows those two errors, and the slopes.一条去追今年那些乱跳的分数的曲线,可能错过明年。把斜率往零拉,曲线在新的一批区上就少动一些。代价是今年的误差变大。这一页给你看这两个误差,以及那些斜率。
Try this试一试 Leave the pull at zero, then raise it. Switch from “shorten only” to “can delete,” and watch bars disappear. Compare this year’s error with next year’s before you say the pull helped.先把拉力留在零,再把它调高。从“只缩短”换到“可以删掉”,看条子消失。先比较今年偏了多少、明年偏了多少,再说拉力有没有帮上忙。
How to read the chart怎么读这张图
If you read this wrong要是读错了
If the officer drops a bend from the one-page note because its bar vanished, the note can omit something that marked districts where people were unwilling to come. The zero is this pull on these 24 districts. Change the pull, or score the public-rental share as a percent instead of a fraction, and the bar can come back.如果同事因为条子消失了,就从一页说明里删掉那个弯,说明可能会漏掉一个标出“不愿意来”的区的东西。这个零,是这个拉力在这 24 个区上的结果。改拉力,或把公屋占比从小数改成百分数,条子可以回来。
What this picture is enough for这张图够说明什么
Enough to see ridge shorten every slope, lasso delete some, and both this year’s error and next year’s error move.看得出:岭回归缩短每个斜率,套索删掉一些,今年的误差和明年的误差都会动。
Not enough for the one-page note. The pull still has to be chosen without looking at a final year of districts that nobody has touched, and you still have to check that the shortened curve falls where people are least willing to come.还不够拿来写那一页说明。拉力仍要在不看最后一年、谁也没碰过的那些区的情况下选定,还得检查缩短后的曲线仍然落在人们最不愿意来的地方。
The equation, and where it stops式子、假设和边界
As on the previous page, the public-rental share is drawn evenly between −1 and 1, and willingness is the wave sin(πX) plus a little jumpiness, 0.3e. The curve may bend 10 times. Columns other than the starting height are centered and rescaled on the 24 districts, so the number λ only means something for that scaling. Ridge adds λ times the sum of squared slopes to the sum of squared misses. Lasso adds λ times the sum of absolute slopes. The starting height is free in both.和上一页一样,公屋占比在 −1 和 1 之间均匀抽取,愿意来的程度是波 sin(πX) 再加上一点跳动 0.3e。曲线可以弯 10 次。除了起点高度,其余各项都在这 24 个区上移到中心并重新缩放,所以 λ 这个数只对这种缩放有意义。岭回归把 λ 乘斜率的平方和,加到误差的平方和上。套索把 λ 乘斜率的绝对值之和加进去。两种做法里,起点高度都不受拉力。
Shortening the slopes can make this year’s error larger and next year’s smaller, until the pull is so hard that the curve misses the wave and both errors rise. Lasso’s exact zeros come from using absolute slopes. Ridge almost never hits an exact zero. One draw does not guarantee a neat U in next year’s error.把斜率缩短,可以让今年的误差变大、明年的误差变小,直到拉力大到曲线错过那条波,两个误差一起上升。套索能正好变成零,是因为它用的是斜率的绝对值。岭回归几乎不会正好碰到零。抽一次,不能保证明年的误差画出整齐的 U 形。
Easy to get wrong.容易踩的坑。 The λ that looks best while you watch next year’s error is a tuning choice. That number is not an untouched test. Another mistake is to read a lasso zero as “this bend does not matter in the world.” It did not matter to this pulled fit, on these districts. A third is to carry the same λ to a differently scaled column. Change how the public-rental share is scored, and the same λ is a different pull.你盯着明年的误差时显得最好的那个 λ,是调参时的选择。那个数不是一份没碰过的测试。另一个错,是把套索的零读成“这个弯在世界上无关”。它只是在这一次被往回拉的拟合里、在这些区上无关。第三个错,是把同一个 λ 搬到缩放不同的一列上。公屋占比的记法一变,同一个 λ 就是不一样的拉力。