import%20marimo%0A%0A__generated_with%20%3D%20%220.23.16%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20METADATA%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22generalization%22%2C%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%22Generalization%22%2C%0A%20%20%20%20%20%20%20%20%22kind%22%3A%20%22concept%22%2C%0A%20%20%20%20%20%20%20%20%22difficulty%22%3A%20%22beginner%22%2C%0A%20%20%20%20%20%20%20%20%22status%22%3A%20%22complete%22%2C%0A%20%20%20%20%20%20%20%20%22topics%22%3A%20%5B%22generalization%22%2C%20%22evaluation%22%2C%20%22model_complexity%22%5D%2C%0A%20%20%20%20%20%20%20%20%22related_models%22%3A%20%5B%22linear_regression%22%2C%20%22decision_tree%22%2C%20%22knn%22%2C%20%22mlp%22%5D%2C%0A%20%20%20%20%20%20%20%20%22prerequisites%22%3A%20%5B%22train_validation_test%22%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20mo.md(f%22%23%20%7BMETADATA%5B'name'%5D%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20In%20one%20sentence%0A%0A%20%20%20%20Generalization%20is%20the%20ability%20to%20perform%20well%20on%20new%20examples%20from%20the%20conditions%20where%20the%20model%20will%20actually%20be%20used.%0A%0A%20%20%20%20%23%23%20Mental%20model%0A%0A%20%20%20%20A%20useful%20model%20learns%20the%20rule%20beneath%20the%20examples%2C%20not%20a%20detailed%20tracing%20of%20every%20training%20point.%20Training%20fit%20is%20evidence%20of%20capacity%3B%20performance%20on%20representative%20new%20data%20is%20evidence%20of%20usefulness.%0A%0A%20%20%20%20%23%23%20Why%20it%20matters%0A%0A%20%20%20%20A%20model%20can%20achieve%20almost%20zero%20training%20error%20by%20memorizing%20noise.%20Deployment%20immediately%20exposes%20whether%20the%20learned%20pattern%20survives%20beyond%20the%20sample.%0A%0A%20%20%20%20%23%23%20Experiment%0A%0A%20%20%20%20**Question%3A**%20which%20fitted%20curve%20captures%20the%20signal%20rather%20than%20the%20accidents%20of%20one%20dataset%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20sklearn.linear_model%20import%20LinearRegression%0A%20%20%20%20from%20sklearn.metrics%20import%20mean_squared_error%0A%20%20%20%20from%20sklearn.pipeline%20import%20make_pipeline%0A%20%20%20%20from%20sklearn.preprocessing%20import%20PolynomialFeatures%0A%0A%20%20%20%20rng%20%3D%20np.random.default_rng(21)%0A%20%20%20%20x_train%20%3D%20np.sort(rng.uniform(-2.5%2C%202.5%2C%2028))%0A%20%20%20%20y_train%20%3D%20np.sin(1.5%20*%20x_train)%20%2B%20rng.normal(0%2C%200.22%2C%20len(x_train))%0A%20%20%20%20x_test%20%3D%20np.linspace(-2.5%2C%202.5%2C%20300)%0A%20%20%20%20y_test%20%3D%20np.sin(1.5%20*%20x_test)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20LinearRegression%2C%0A%20%20%20%20%20%20%20%20PolynomialFeatures%2C%0A%20%20%20%20%20%20%20%20make_pipeline%2C%0A%20%20%20%20%20%20%20%20mean_squared_error%2C%0A%20%20%20%20%20%20%20%20plt%2C%0A%20%20%20%20%20%20%20%20x_test%2C%0A%20%20%20%20%20%20%20%20x_train%2C%0A%20%20%20%20%20%20%20%20y_test%2C%0A%20%20%20%20%20%20%20%20y_train%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20LinearRegression%2C%0A%20%20%20%20PolynomialFeatures%2C%0A%20%20%20%20make_pipeline%2C%0A%20%20%20%20mean_squared_error%2C%0A%20%20%20%20plt%2C%0A%20%20%20%20x_test%2C%0A%20%20%20%20x_train%2C%0A%20%20%20%20y_test%2C%0A%20%20%20%20y_train%2C%0A)%3A%0A%20%20%20%20degrees%20%3D%20%5B1%2C%205%2C%2018%5D%0A%20%20%20%20fig%2C%20axes%20%3D%20plt.subplots(1%2C%203%2C%20figsize%3D(13%2C%203.8)%2C%20sharey%3DTrue)%0A%20%20%20%20for%20ax%2C%20degree%20in%20zip(axes%2C%20degrees)%3A%0A%20%20%20%20%20%20%20%20model%20%3D%20make_pipeline(PolynomialFeatures(degree)%2C%20LinearRegression())%0A%20%20%20%20%20%20%20%20model.fit(x_train%5B%3A%2C%20None%5D%2C%20y_train)%0A%20%20%20%20%20%20%20%20train_mse%20%3D%20mean_squared_error(y_train%2C%20model.predict(x_train%5B%3A%2C%20None%5D))%0A%20%20%20%20%20%20%20%20test_mse%20%3D%20mean_squared_error(y_test%2C%20model.predict(x_test%5B%3A%2C%20None%5D))%0A%20%20%20%20%20%20%20%20ax.scatter(x_train%2C%20y_train%2C%20color%3D%22black%22%2C%20s%3D24%2C%20label%3D%22training%20sample%22)%0A%20%20%20%20%20%20%20%20ax.plot(x_test%2C%20y_test%2C%20color%3D%22grey%22%2C%20linestyle%3D%22--%22%2C%20label%3D%22true%20signal%22)%0A%20%20%20%20%20%20%20%20ax.plot(x_test%2C%20model.predict(x_test%5B%3A%2C%20None%5D)%2C%20color%3D%22crimson%22%2C%20label%3D%22fitted%20model%22)%0A%20%20%20%20%20%20%20%20ax.set(title%3Df%22degree%20%7Bdegree%7D%5Cntrain%20%7Btrain_mse%3A.3f%7D%20%C2%B7%20new%20data%20%7Btest_mse%3A.3f%7D%22%2C%20xlabel%3D%22x%22)%0A%20%20%20%20%20%20%20%20ax.set_ylim(-2%2C%202)%0A%20%20%20%20axes%5B0%5D.set_ylabel(%22target%22)%0A%20%20%20%20axes%5B0%5D.legend(fontsize%3D8)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Common%20mistake%0A%0A%20%20%20%20Treating%20a%20small%20train%E2%80%93validation%20gap%20as%20proof%20of%20generalization.%20Both%20scores%20can%20be%20misleading%20when%20the%20split%20leaks%20information%20or%20fails%20to%20represent%20deployment.%0A%0A%20%20%20%20%23%23%20Diagnosis%0A%0A%20%20%20%20Compare%20against%20a%20baseline%2C%20inspect%20the%20train%E2%80%93validation%20gap%2C%20repeat%20across%20sensible%20splits%2C%20and%20evaluate%20slices%20that%20reflect%20time%2C%20groups%2C%20locations%2C%20or%20user%20populations.%0A%0A%20%20%20%20%23%23%20Practical%20rule%0A%0A%20%20%20%20Trust%20performance%20only%20when%20the%20evaluation%20data%20reproduces%20the%20information%20boundaries%20and%20variation%20of%20the%20real%20prediction%20setting.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
41032e9167c70ddd79a352d2b9558892