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%22linear_regression%22%2C%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%22Linear%20Regression%22%2C%0A%20%20%20%20%20%20%20%20%22types%22%3A%20%5B%22algorithm%22%5D%2C%0A%20%20%20%20%20%20%20%20%22families%22%3A%20%5B%22linear%22%5D%2C%0A%20%20%20%20%20%20%20%20%22tasks%22%3A%20%5B%22regression%22%5D%2C%0A%20%20%20%20%20%20%20%20%22data%22%3A%20%5B%22tabular%22%5D%2C%0A%20%20%20%20%20%20%20%20%22learning%22%3A%20%5B%22supervised%22%5D%2C%0A%20%20%20%20%20%20%20%20%22capacity%22%3A%20%22parametric%22%2C%0A%20%20%20%20%20%20%20%20%22mechanisms%22%3A%20%5B%22weighted_sum%22%2C%20%22least_squares%22%5D%2C%0A%20%20%20%20%20%20%20%20%22properties%22%3A%20%5B%22extrapolates_linearly%22%2C%20%22interpretable%22%5D%2C%0A%20%20%20%20%20%20%20%20%22constraints%22%3A%20%5B%22sensitive_to_outliers%22%5D%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%22explainability%22%3A%20%22high%22%2C%0A%20%20%20%20%20%20%20%20%22training_cost%22%3A%20%22low%22%2C%0A%20%20%20%20%20%20%20%20%22inference_cost%22%3A%20%22low%22%2C%0A%20%20%20%20%20%20%20%20%22data_appetite%22%3A%20%22low%22%2C%0A%20%20%20%20%7D%0A%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%20Linear%20Regression%20predicts%20a%20number%20by%20adding%20weighted%20feature%20values%20and%20a%20baseline%20offset.%0A%0A%20%20%20%20%23%23%20Mental%20model%0A%0A%20%20%20%20Imagine%20a%20control%20panel%20with%20one%20lever%20per%20feature.%20Training%20decides%20how%20strongly%20each%20lever%20pushes%20the%20prediction%20up%20or%20down.%20The%20final%20prediction%20is%3A%0A%0A%20%20%20%20%24%24%5Chat%7By%7D%20%3D%20b%20%2B%20w_1x_1%20%2B%20w_2x_2%20%2B%20%5Ccdots%20%2B%20w_px_p%24%24%0A%0A%20%20%20%20The%20picture%20is%20useful%2C%20but%20a%20coefficient%20is%20not%20automatically%20a%20causal%20effect.%20It%20describes%20the%20fitted%20relationship%20while%20the%20other%20included%20features%20are%20held%20fixed.%0A%0A%20%20%20%20%23%23%20Input%20and%20output%0A%0A%20%20%20%20-%20**Input%3A**%20one%20row%20of%20numerical%20features%2C%20or%20encoded%20categories.%0A%20%20%20%20-%20**Output%3A**%20one%20continuous%20value%20such%20as%20price%2C%20demand%2C%20temperature%2C%20or%20duration.%0A%20%20%20%20-%20**Learns%3A**%20one%20coefficient%20per%20feature%20plus%20an%20intercept.%0A%0A%20%20%20%20%23%23%20How%20it%20works%0A%0A%20%20%20%20Ordinary%20least%20squares%20chooses%20coefficients%20that%20minimize%20the%20sum%20of%20squared%20residuals%3A%0A%0A%20%20%20%20%24%24%5Cmin_w%20%5Csum_i%20(y_i%20-%20%5Chat%7By%7D_i)%5E2%24%24%0A%0A%20%20%20%20Squaring%20makes%20large%20mistakes%20count%20much%20more%20than%20small%20ones.%20It%20also%20gives%20a%20smooth%20objective%20with%20an%20exact%20mathematical%20solution%2C%20although%20libraries%20often%20use%20numerically%20stable%20matrix%20methods%20rather%20than%20explicitly%20inverting%20a%20matrix.%0A%0A%20%20%20%20Feature%20engineering%20changes%20what%20%22linear%22%20can%20express.%20Adding%20%24x%5E2%24%2C%20interactions%2C%20or%20splines%20can%20represent%20curved%20relationships%20while%20the%20model%20remains%20linear%20in%20its%20learned%20coefficients.%0A%20%20%20%20%22%22%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%20A%20practical%20example%0A%0A%20%20%20%20For%20delivery%20time%2C%20features%20might%20include%20distance%2C%20number%20of%20stops%2C%20traffic%20index%2C%20and%20vehicle%20type.%20Coefficients%20reveal%20the%20direction%20and%20scale%20of%20associations.%20Residual%20plots%20then%20show%20whether%20the%20model%20systematically%20misses%20short%20urban%20trips%20or%20long%20rural%20ones.%0A%0A%20%20%20%20%23%23%20When%20to%20use%20it%0A%0A%20%20%20%20-%20You%20need%20a%20transparent%20baseline%20for%20a%20numeric%20target.%0A%20%20%20%20-%20The%20signal%20is%20approximately%20additive%20and%20smooth.%0A%20%20%20%20-%20Data%20is%20limited%20and%20a%20complex%20model%20would%20be%20unstable.%0A%20%20%20%20-%20Extrapolation%20is%20required%20and%20a%20linear%20continuation%20is%20defensible.%0A%20%20%20%20-%20Coefficients%20and%20uncertainty%20estimates%20are%20valuable.%0A%0A%20%20%20%20%23%23%20When%20to%20avoid%20it%0A%0A%20%20%20%20-%20Important%20thresholds%20and%20feature%20interactions%20are%20unknown%20and%20complex.%0A%20%20%20%20-%20Extreme%20outliers%20dominate%20squared%20error.%0A%20%20%20%20-%20The%20future%20relationship%20is%20unlikely%20to%20continue%20linearly.%0A%20%20%20%20-%20You%20need%20causal%20conclusions%20but%20only%20have%20predictive%20observational%20data.%0A%0A%20%20%20%20%23%23%20Data%20preparation%0A%0A%20%20%20%20Handle%20missing%20values%20explicitly.%20One-hot%20encode%20nominal%20categories.%20Scaling%20is%20not%20required%20for%20ordinary%20fitting%2C%20but%20it%20makes%20coefficient%20magnitudes%20comparable%20and%20becomes%20important%20with%20regularization.%20Inspect%20target%20skew%2C%20outliers%2C%20correlated%20features%2C%20and%20time%20or%20group%20structure%20before%20splitting.%0A%0A%20%20%20%20%23%23%20Important%20variants%20and%20controls%0A%0A%20%20%20%20%7C%20Variant%20%7C%20Role%20%7C%0A%20%20%20%20%7C---------%7C------%7C%0A%20%20%20%20%7C%20Ridge%20(L2)%20%7C%20Stabilizes%20correlated%20coefficients%20%7C%0A%20%20%20%20%7C%20Lasso%20(L1)%20%7C%20Can%20set%20some%20coefficients%20to%20zero%20%7C%0A%20%20%20%20%7C%20Elastic%20Net%20%7C%20Combines%20both%20penalties%20%7C%0A%20%20%20%20%7C%20Robust%20regression%20%7C%20Reduces%20influence%20of%20extreme%20residuals%20%7C%0A%0A%20%20%20%20Plain%20Linear%20Regression%20has%20few%20hyperparameters%3B%20the%20hard%20work%20is%20feature%20design%2C%20split%20design%2C%20and%20deciding%20whether%20the%20assumptions%20are%20useful.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20Notebook%20%E2%80%94%20fit%2C%20residuals%2C%20and%20extrapolation%0A%0A%20%20%20%20**Question%3A**%20what%20can%20a%20straight-line%20model%20reveal%20even%20when%20it%20is%20not%20the%20final%20model%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_absolute_error%0A%0A%20%20%20%20rng%20%3D%20np.random.default_rng(7)%0A%20%20%20%20x%20%3D%20np.linspace(0%2C%2010%2C%2080)%0A%20%20%20%20y%20%3D%204%20%2B%202.5%20*%20x%20%2B%20rng.normal(0%2C%202%2C%20len(x))%0A%20%20%20%20X%20%3D%20x.reshape(-1%2C%201)%0A%20%20%20%20return%20LinearRegression%2C%20X%2C%20mean_absolute_error%2C%20np%2C%20plt%2C%20x%2C%20y%0A%0A%0A%40app.cell%0Adef%20_(LinearRegression%2C%20X%2C%20mean_absolute_error%2C%20plt%2C%20x%2C%20y)%3A%0A%20%20%20%20model%20%3D%20LinearRegression().fit(X%2C%20y)%0A%20%20%20%20prediction%20%3D%20model.predict(X)%0A%20%20%20%20print(f%22intercept%3D%7Bmodel.intercept_%3A.2f%7D%2C%20slope%3D%7Bmodel.coef_%5B0%5D%3A.2f%7D%22)%0A%20%20%20%20print(f%22training%20MAE%3D%7Bmean_absolute_error(y%2C%20prediction)%3A.2f%7D%22)%0A%0A%20%20%20%20fig%2C%20axes%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(11%2C%204))%0A%20%20%20%20axes%5B0%5D.scatter(x%2C%20y%2C%20alpha%3D0.65%2C%20label%3D%22observations%22)%0A%20%20%20%20axes%5B0%5D.plot(x%2C%20prediction%2C%20color%3D%22crimson%22%2C%20label%3D%22fitted%20line%22)%0A%20%20%20%20axes%5B0%5D.set(title%3D%22Fit%22%2C%20xlabel%3D%22x%22%2C%20ylabel%3D%22target%22)%0A%20%20%20%20axes%5B0%5D.legend()%0A%20%20%20%20axes%5B1%5D.scatter(prediction%2C%20y%20-%20prediction%2C%20alpha%3D0.65)%0A%20%20%20%20axes%5B1%5D.axhline(0%2C%20color%3D%22black%22%2C%20linewidth%3D1)%0A%20%20%20%20axes%5B1%5D.set(title%3D%22Residuals%22%2C%20xlabel%3D%22prediction%22%2C%20ylabel%3D%22actual%20-%20prediction%22)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20return%20(model%2C)%0A%0A%0A%40app.cell%0Adef%20_(LinearRegression%2C%20X%2C%20model%2C%20np%2C%20plt%2C%20y)%3A%0A%20%20%20%20%23%20One%20extreme%20point%20can%20pull%20a%20squared-error%20fit.%0A%20%20%20%20X_outlier%20%3D%20np.vstack(%5BX%2C%20%5B%5B10%5D%5D%5D)%0A%20%20%20%20y_outlier%20%3D%20np.append(y%2C%2060)%0A%20%20%20%20pulled%20%3D%20LinearRegression().fit(X_outlier%2C%20y_outlier)%0A%0A%20%20%20%20future%20%3D%20np.linspace(0%2C%2015%2C%20120).reshape(-1%2C%201)%0A%20%20%20%20plt.figure(figsize%3D(8%2C%204))%0A%20%20%20%20plt.scatter(X_outlier%5B%3A%2C%200%5D%2C%20y_outlier%2C%20alpha%3D0.6)%0A%20%20%20%20plt.plot(future%5B%3A%2C%200%5D%2C%20model.predict(future)%2C%20label%3D%22original%20fit%22)%0A%20%20%20%20plt.plot(future%5B%3A%2C%200%5D%2C%20pulled.predict(future)%2C%20label%3D%22fit%20with%20outlier%22)%0A%20%20%20%20plt.axvline(10%2C%20color%3D%22grey%22%2C%20linestyle%3D%22--%22%2C%20label%3D%22training%20range%20ends%22)%0A%20%20%20%20plt.legend()%0A%20%20%20%20plt.title(%22Outlier%20influence%20and%20assumed%20extrapolation%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---%0A%0A%20%20%20%20%23%23%20Evaluation%20and%20diagnosis%0A%0A%20%20%20%20Use%20MAE%20for%20an%20understandable%20typical%20error%20and%20RMSE%20when%20large%20misses%20deserve%20extra%20weight.%20Plot%20residuals%20against%20predictions%20and%20important%20features.%20Patterns%20suggest%20missing%20nonlinearity%3B%20widening%20spread%20suggests%20nonconstant%20variance%3B%20time%20patterns%20suggest%20drift%20or%20leakage.%0A%0A%20%20%20%20Check%20performance%20against%20a%20mean%20baseline%20and%20across%20meaningful%20slices.%20A%20good%20global%20score%20can%20hide%20systematic%20failure%20for%20a%20region%20or%20customer%20segment.%0A%0A%20%20%20%20%23%23%20Cost%20profile%0A%0A%20%20%20%20Training%20and%20inference%20are%20fast.%20Memory%20grows%20roughly%20with%20the%20number%20of%20features%2C%20making%20the%20model%20attractive%20for%20constrained%20systems.%0A%0A%20%20%20%20%23%23%20Related%20models%0A%0A%20%20%20%20-%20**Logistic%20Regression**%20uses%20a%20linear%20score%20for%20classification.%0A%20%20%20%20-%20**Decision%20Tree**%20captures%20thresholds%20and%20interactions%20with%20little%20feature%20engineering.%0A%20%20%20%20-%20**Gradient%20Boosting**%20is%20a%20stronger%20nonlinear%20tabular%20candidate.%0A%0A%20%20%20%20%23%23%20Practical%20takeaway%0A%0A%20%20%20%20Start%20here%20for%20numeric%20prediction.%20If%20it%20loses%2C%20the%20pattern%20of%20its%20errors%20often%20tells%20you%20what%20the%20next%20model%20must%20learn.%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
415347d65a2ede003e1f41b6cf02ee72