import%20marimo%0A%0A__generated_with%20%3D%20%220.16.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20seaborn%20as%20sns%0A%20%20%20%20return%20mo%2C%20np%2C%20pd%2C%20plt%2C%20sns%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%20Machine%20Learning%20Zoomcamp%0A%0A%20%20%20%20%23%23%20Module%202%3A%20**Regression**%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(pd)%3A%0A%20%20%20%20repository_root%20%3D%20%22https%3A%2F%2Fgithub.com%2FDataTalksClub%2Fmachine-learning-zoomcamp%2Fblob%2Fmaster%2F%22%0A%0A%20%20%20%20chapters%20%3D%20pd.DataFrame(%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Car%20price%20prediction%20project%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22vM3SqPNlStE%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F01-car-price-intro.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Data%20preparation%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22Kd74oR4QWGM%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F02-data-preparation.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Exploratory%20data%20analysis%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22k6k8sQ0GhPM%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F03-eda.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Setting%20up%20the%20validation%20framework%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22ck0IfiPaQi0%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F04-validation-framework.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Linear%20regression%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22Dn1eTQLsOdA%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F05-linear-regression-simple.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Linear%20regression%3A%20vector%20form%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22YkyevnYyAww%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F06-linear-regression-vector.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Training%20linear%20regression%3A%20Normal%20equation%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22hx6nak-Y11g%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F07-linear-regression-training.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Baseline%20model%20for%20car%20price%20prediction%20project%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22SvPpMMYtYbU%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F08-baseline-model.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Root%20mean%20squared%20error%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%220LWoFtbzNUM%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F09-rmse.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Using%20RMSE%20on%20validation%20data%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22rawGPXg2ofE%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F10-car-price-validation.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Feature%20engineering%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22-aEShw4ftB0%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F11-feature-engineering.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Categorical%20variables%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22sGLAToAAMa4%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F12-categorical-variables.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Regularization%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%2291ve3EJlHBc%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F13-regularization.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Tuning%20the%20model%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22lW-YVxPgzQw%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F14-tuning-model.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Using%20the%20model%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22KT--uIJozes%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F15-using-model.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Car%20price%20prediction%20project%20summary%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20%22_qI01YXbyro%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F16-summary.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Explore%20more%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20None%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2F17-explore-more.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Homework%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22youtube_id%22%3A%20None%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22contents%22%3A%20repository_root%2B%2202-regression%2Fhomework.md%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%5D)%0A%0A%20%20%20%20chapters.insert(loc%3D0%2C%20column%3D%22snapshot%22%2C%20value%3D%22https%3A%2F%2Fimg.youtube.com%2Fvi%2F%22%2Bchapters.youtube_id.astype(str)%2B%22%2Fhqdefault.jpg%22)%0A%20%20%20%20chapters.insert(loc%3D2%2C%20column%3D%22youtube%22%2C%20value%3D%22https%3A%2F%2Fyoutube.com%2Fwatch%3Fv%3D%22%2Bchapters.youtube_id.astype(str))%0A%0A%20%20%20%20videos%20%3D%20chapters%5Bchapters%5B%22youtube_id%22%5D.notnull()%5D%0A%20%20%20%20videos%5B%5B%22snapshot%22%2C%20%22title%22%2C%20%22youtube%22%5D%5D%0A%20%20%20%20return%20(chapters%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(chapters)%3A%0A%20%20%20%20contents%20%3D%20chapters%5Bchapters%5B%22contents%22%5D.notnull()%5D%0A%20%20%20%20contents%5B%5B%22title%22%2C%20%22contents%22%5D%5D%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Car%20Price%20Prediction%20Project%0A%0A%20%20%20%20In%20this%20chapter%20we'll%20be%20working%20with%20the%20%5BCar%20Features%20and%20MSRP%5D(https%3A%2F%2Fwww.kaggle.com%2Fdatasets%2FCooperUnion%2Fcardataset)%20Kaggle%20dataset.%0A%0A%20%20%20%201.%20Prepare%20the%20data%20and%20do%20some%20Exploratory%20Data%20Analysis%20(EDA)%0A%20%20%20%202.%20Use%20linear%20regression%20for%20predicting%20price%0A%20%20%20%203.%20Understand%20the%20internals%20of%20linear%20regression%0A%20%20%20%204.%20Evaluate%20the%20model%20using%20Root%20Mean%20Square%20Error%20(RMSE)%0A%20%20%20%205.%20Do%20some%20feature%20engineering%0A%20%20%20%206.%20Regualization%0A%20%20%20%207.%20Use%20the%20model%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Data%20Preparation%0A%0A%20%20%20%20To%20facilitate%20the%20analysis%2C%20the%20CSV%20file%20has%20been%20already%20downloaded%20in%20**data%2Fcar-prices.csv**.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(pd)%3A%0A%20%20%20%20car_prices%20%3D%20pd.read_csv('.%2Fmodule-2%2Fdata%2Fcar-prices.csv')%0A%20%20%20%20car_prices.head()%0A%20%20%20%20return%20(car_prices%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%23%20Standardize%20Column%20Names%0A%0A%20%20%20%20Some%20of%20the%20columns%20are%20named%20using%20spaces%20as%20separator%20and%20other%20columns%20were%20named%20using%20underscores%20as%20a%20separator.%20We'll%20now%20standardize%20their%20names.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(car_prices)%3A%0A%20%20%20%20car_prices.columns%20%3D%20car_prices.columns.str.lower().str.replace('%20'%2C%20'_')%0A%20%20%20%20car_prices.head()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%23%20Standardize%20Values%0A%0A%20%20%20%20First%20we'll%20get%20the%20list%20of%20all%20string%20columns.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(car_prices)%3A%0A%20%20%20%20def%20get_string_columns(df)%3A%0A%20%20%20%20%20%20%20%20return%20list(df.dtypes%5Bdf.dtypes%20%3D%3D%20'object'%5D.index)%0A%0A%20%20%20%20get_string_columns(car_prices)%0A%20%20%20%20return%20(get_string_columns%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(car_prices%2C%20get_string_columns)%3A%0A%20%20%20%20for%20string_column%20in%20get_string_columns(car_prices)%3A%0A%20%20%20%20%20%20%20%20car_prices%5Bstring_column%5D%20%3D%20car_prices%5Bstring_column%5D.str.lower().str.replace('%20'%2C%20'_')%0A%0A%20%20%20%20car_prices.head()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Exploratory%20Data%20Analysis%0A%0A%20%20%20%20%23%23%23%20Check%20Unique%20Values%20per%20Column%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(car_prices)%3A%0A%20%20%20%20def%20show_unique_values(df)%3A%0A%20%20%20%20%20%20%20%20unique_values%20%3D%20%7B%7D%0A%0A%20%20%20%20%20%20%20%20for%20described_column%20in%20df.columns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20unique_values%5Bdescribed_column%5D%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22sample%22%3A%20df%5Bdescribed_column%5D.unique()%5B%3A5%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22count%22%3A%20df%5Bdescribed_column%5D.nunique()%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20return%20unique_values%0A%0A%20%20%20%20show_unique_values(car_prices)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%23%20Check%20the%20Price%20Column%0A%0A%20%20%20%20A%20tiny%20number%20of%20cars%20with%20a%20very%20high%20price%20is%20ruining%20our%20plot.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(car_prices%2C%20sns)%3A%0A%20%20%20%20sns.histplot(car_prices.msrp%2C%20bins%3D100)%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%22We%20could%20filter%20them%20to%20get%20a%20better%20view%20but%20that%20implies%20making%20a%20choice%20on%20where%20to%20cut.%20Which%20could%20confuse%20our%20model.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(car_prices%2C%20sns)%3A%0A%20%20%20%20sns.histplot(car_prices.msrp%5Bcar_prices.msrp%20%3C%20100000%5D%2C%20bins%3D100)%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%22Instead%20of%20making%20a%20manual%20cut%2C%20we%20can%20take%20the%20logarithm%20of%20the%20price.%20By%20doing%20so%2C%20outlier%20(few%20cases%20with%20extreme%20values)%20are%20handled%20in%20a%20more%20elegant%20way.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(car_prices%2C%20np%2C%20sns)%3A%0A%20%20%20%20def%20plot_price_logs(df)%3A%0A%20%20%20%20%20%20%20%20price_logs%20%3D%20np.log1p(df.msrp)%0A%20%20%20%20%20%20%20%20sns.histplot(price_logs%2C%20bins%3D100)%0A%0A%20%20%20%20plot_price_logs(car_prices)%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%23%23%23%20Check%20for%20Empty%20Values%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(car_prices)%3A%0A%20%20%20%20car_prices.isna().sum()%5Bcar_prices.isna().any()%5D%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Setting%20Up%20the%20Validation%20Framework%0A%0A%20%20%20%20We%20want%20to%20define%20our%20feature%20matrix%20%24X%24%20and%20a%20target%20variable%20%24y%24%20and%20then%20separate%20it%20into%20our%20train%20%24X_T%24%20and%20%24y_T%24%2C%20validation%20%24X_V%24%20and%20%24y_V%24%20and%20test%20%24X_%7Btest%7D%24%20and%20%24t_%7Btest%7D%24%20splits.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(car_prices)%3A%0A%20%20%20%20n%20%3D%20len(car_prices)%0A%20%20%20%20n_val%20%3D%20n_test%20%3D%20int(len(car_prices)%20*%200.2)%0A%20%20%20%20n_train%20%3D%20len(car_prices)%20-%20n_val%20-%20n_test%0A%0A%20%20%20%20%7B%22n%22%3A%20n%2C%20%22n_train%22%3A%20n_train%2C%20%22n_val%22%3A%20n_val%2C%20%22n_test%22%3A%20n_test%7D%0A%20%20%20%20return%20n%2C%20n_train%2C%20n_val%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20To%20avoid%20spliting%20the%20records%20in%20the%20same%20order%20they%20have%20in%20the%20dataset%20(so%20that%20we%20don't%20get%20oddly%20distributed%20splits)%2C%20we'll%20make%20a%20randomized%20selection.%0A%0A%20%20%20%20First%2C%20we%20create%20a%20list%20with%20the%20identifiers%20of%20each%20sample%20in%20a%20random%20order.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(n%2C%20np)%3A%0A%20%20%20%20np.random.seed(2)%0A%20%20%20%20shuffled_indexes%20%3D%20np.arange(n)%0A%20%20%20%20np.random.shuffle(shuffled_indexes)%0A%0A%20%20%20%20shuffled_indexes%0A%20%20%20%20return%20(shuffled_indexes%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Then%20we%20use%20that%20as%20our%20reference%20for%20creating%20our%20splits.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(car_prices%2C%20n_train%2C%20n_val%2C%20shuffled_indexes)%3A%0A%20%20%20%20car_prices_train%20%3D%20car_prices.iloc%5Bshuffled_indexes%5B%3An_train%5D%5D%0A%20%20%20%20car_prices_val%20%3D%20car_prices.iloc%5Bshuffled_indexes%5Bn_train%3An_train%2Bn_val%5D%5D%0A%20%20%20%20car_prices_test%20%3D%20car_prices.iloc%5Bshuffled_indexes%5Bn_train%2Bn_val%3A%5D%5D%0A%20%20%20%20return%20car_prices_test%2C%20car_prices_train%2C%20car_prices_val%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22As%20our%20index%20column%20has%20now%20a%20random%20order%2C%20we'll%20reset%20it%20in%20each%20of%20the%20splits.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(car_prices_test%2C%20car_prices_train%2C%20car_prices_val)%3A%0A%20%20%20%20car_prices_train.reset_index(drop%3DTrue%2C%20inplace%3DTrue)%0A%20%20%20%20car_prices_val.reset_index(drop%3DTrue%2C%20inplace%3DTrue)%0A%20%20%20%20car_prices_test.reset_index(drop%3DTrue%2C%20inplace%3DTrue)%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%22Now%2C%20we'll%20define%20our%20target%20variables%20adjusting%20the%20price%20column%20using%20the%20logarithm%20function%20described%20above.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(car_prices_test%2C%20car_prices_train%2C%20car_prices_val%2C%20np)%3A%0A%20%20%20%20y_train%20%3D%20np.log1p(car_prices_train.msrp.values)%0A%20%20%20%20y_val%20%3D%20np.log1p(car_prices_val.msrp.values)%0A%20%20%20%20y_test%20%3D%20np.log1p(car_prices_test.msrp.values)%0A%20%20%20%20return%20y_train%2C%20y_val%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22And%2C%20finally%2C%20we'll%20define%20our%20feature%20matrices.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(car_prices_test%2C%20car_prices_train%2C%20car_prices_val)%3A%0A%20%20%20%20X_train%20%3D%20car_prices_train.fillna(0)%0A%20%20%20%20X_val%20%3D%20car_prices_val.fillna(0)%0A%20%20%20%20X_test%20%3D%20car_prices_test.fillna(0)%0A%0A%20%20%20%20del%20X_train%5B%22msrp%22%5D%0A%20%20%20%20del%20X_val%5B%22msrp%22%5D%0A%20%20%20%20del%20X_test%5B%22msrp%22%5D%0A%20%20%20%20return%20X_train%2C%20X_val%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Finally%2C%20let's%20wrap%20all%20this%20into%20a%20funciton.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20def%20prepare_split(df)%3A%0A%20%20%20%20%20%20%20%20df%20%3D%20df.copy()%0A%20%20%20%20%20%20%20%20df.reset_index(drop%3DTrue%2C%20inplace%3DTrue)%0A%0A%20%20%20%20%20%20%20%20y%20%3D%20np.log1p(df.msrp.values)%0A%0A%20%20%20%20%20%20%20%20del%20df%5B%22msrp%22%5D%0A%0A%20%20%20%20%20%20%20%20return%20df%2C%20y%0A%0A%20%20%20%20def%20split_dataset(df)%3A%0A%20%20%20%20%20%20%20%20n%20%3D%20len(df)%0A%20%20%20%20%20%20%20%20n_val%20%3D%20n_test%20%3D%20int(len(df)%20*%200.2)%0A%20%20%20%20%20%20%20%20n_train%20%3D%20len(df)%20-%20n_val%20-%20n_test%0A%0A%20%20%20%20%20%20%20%20np.random.seed(2)%0A%20%20%20%20%20%20%20%20shuffled_indexes%20%3D%20np.arange(n)%0A%20%20%20%20%20%20%20%20np.random.shuffle(shuffled_indexes)%0A%0A%20%20%20%20%20%20%20%20train%20%3D%20prepare_split(df.iloc%5Bshuffled_indexes%5B%3An_train%5D%5D)%0A%20%20%20%20%20%20%20%20val%20%3D%20prepare_split(df.iloc%5Bshuffled_indexes%5Bn_train%3An_train%2Bn_val%5D%5D)%0A%20%20%20%20%20%20%20%20test%20%3D%20prepare_split(df.iloc%5Bshuffled_indexes%5Bn_train%2Bn_val%3A%5D%5D)%0A%0A%20%20%20%20%20%20%20%20return%20train%2C%20val%2C%20test%0A%20%20%20%20return%20(split_dataset%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Linear%20Regression%0A%0A%20%20%20%20We%20are%20trying%20to%20compute%20a%20%24g%24%20estimator%20function%20so%20that%20given%20the%20features%20of%20a%20car%20%24i%24%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20x_i%20%3D%20(x_%7Bi1%7D%2C%20x_%7Bi2%7D%2C%20...%2C%20x_%7Bin%7D)%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20Our%20function%20evaluated%20with%20those%20features%20approaches%20the%20target%20price%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20g_i(x_%7Bi1%7D%2C%20x_%7Bi2%7D%2C%20...%2C%20x_%7Bin%7D)%20%5Capprox%20y_i%0A%20%20%20%20%5C%5D%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20def%20raw_linear_regression(x%2C%20w)%3A%0A%20%20%20%20%20%20%20%20assert%20len(x)%20%2B%201%20%3D%3D%20len(w)%2C%20%22The%20size%20of%20the%20features%20vector%20does%20not%20match%20the%20size%20of%20the%20weights%22%0A%0A%20%20%20%20%20%20%20%20y_log%20%3D%20w%5B0%5D%20%2B%20np.sum(%5Bx%5Bi%5D%20*%20w%5Bi%20%2B%201%5D%20for%20i%20in%20range(len(x))%5D)%0A%0A%20%20%20%20%20%20%20%20return%20np.expm1(y_log).astype(int)%0A%20%20%20%20return%20(raw_linear_regression%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22In%20order%20to%20show%20how%20linear%20regression%20works%2C%20we'll%20manually%20compute%20it%20for%20a%20selection%20of%20features%3A%20%60engine_hp%60%2C%20%60city_mpg%60%20and%20%60popularity%60%20of%20a%20randomly%20selected%20car.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_train)%3A%0A%20%20%20%20def%20select_car(df%2C%20id)%3A%0A%20%20%20%20%20%20%20%20return%20df.iloc%5Bid%3Aid%20%2B%201%5D%0A%0A%20%20%20%20random_car_id%20%3D%2010%0A%20%20%20%20select_car(X_train%2C%20random_car_id)%0A%20%20%20%20return%20random_car_id%2C%20select_car%0A%0A%0A%40app.cell%0Adef%20_(X_train)%3A%0A%20%20%20%20def%20select_features(df)%3A%0A%20%20%20%20%20%20%20%20return%20df%5B%5B%22engine_hp%22%2C%20%22city_mpg%22%2C%20%22popularity%22%5D%5D.values%0A%0A%20%20%20%20select_features(X_train)%0A%20%20%20%20return%20(select_features%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20random_car_id%2C%20select_car%2C%20select_features)%3A%0A%20%20%20%20def%20get_random_car()%3A%0A%20%20%20%20%20%20%20%20return%20select_features(select_car(X_train%2C%20random_car_id))%0A%0A%20%20%20%20get_random_car()%0A%20%20%20%20return%20(get_random_car%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Given%20the%20%22randomly%22%20chosen%20car%2C%20we'll%20extract%20the%20selected%20features%20and%20its%20corresponding%20price%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(get_random_car%2C%20np%2C%20random_car_id%2C%20y_train)%3A%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22x%22%3A%20get_random_car()%5B0%5D%2C%0A%20%20%20%20%20%20%20%20%22y%22%3A%20np.expm1(y_train%5Brandom_car_id%3Arandom_car_id%20%2B%201%5D).astype(int)%0A%20%20%20%20%7D%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%22Now%20we%20need%20to%20define%20some%20weights.%20Typically%2C%20we'd%20be%20running%20a%20process%20that%20finds%20the%20optimal%20values%20but%20for%20the%20moment%20we'll%20manually%20assign%20them%20some%20numbers%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(get_random_car%2C%20raw_linear_regression)%3A%0A%20%20%20%20raw_linear_regression(get_random_car()%5B0%5D%2C%20%5B7.5%2C%200.01%2C%200.07%2C%200.003%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Linear%20Regression%20in%20Vector%20Form%0A%0A%20%20%20%20We%20want%20now%20to%20generalize%20our%20previous%20linear%20regression%20formula%20so%20that%20it%20works%20with%20any%20number%20of%20features%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20g_i(x_i)%20%3D%20w_o%20%2B%20%5Csum_%7Bj%3D1%7D%5En%20x_%7Bij%7D%20w_j%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20As%20the%20right%20term%20of%20the%20equation%20corresponds%20with%20a%20dot%20product%2C%20we%20can%20rewrite%20it%20in%20a%20simplified%20form%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20g_i(x_i)%20%3D%20w_0%20%2B%20%5Cbold%7Bw%7D%5ET%20%5Cbold%7Bx%7D_i%0A%20%20%20%20%5C%5D%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20def%20vectorial_linear_regression(x%2C%20w)%3A%0A%20%20%20%20%20%20%20%20assert%20len(x)%20%2B%201%20%3D%3D%20len(w)%2C%20%22The%20size%20of%20the%20features%20vector%20does%20not%20match%20the%20size%20of%20the%20weights%22%0A%0A%20%20%20%20%20%20%20%20y_log%20%3D%20w%5B0%5D%20%2B%20np.dot(x%2C%20w%5B1%3A%5D)%0A%0A%20%20%20%20%20%20%20%20return%20np.expm1(y_log).astype(int)%0A%20%20%20%20return%20(vectorial_linear_regression%2C)%0A%0A%0A%40app.cell%0Adef%20_(get_random_car%2C%20vectorial_linear_regression)%3A%0A%20%20%20%20vectorial_linear_regression(get_random_car()%5B0%5D%2C%20%5B7.5%2C%200.01%2C%200.07%2C%200.003%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20If%20we%20want%2C%20we%20can%20simplify%20our%20expression%20even%20further%20by%20defining%20an%20extra%20feature%20that%20always%20equals%20%241%24.%20By%20doing%20so%2C%20we'd%20get%20%24%5Cbold%7Bw%7D%24%20and%20%24%5Cbold%7Bx%7D%24%20vectors%20with%20%24n%2B1%24%20elements%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20w%20%3D%20%5Bw_0%2C%20w_1%2C%20...%2C%20w_n%5D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20x_i%20%3D%20%5B1%2C%20x_%7Bi1%7D%2C%20x_%7Bi2%7D%2C%20...%2C%20x_%7Bin%7D%5D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20Therefore%2C%20our%20estimator%20could%20be%20written%20as%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20g_i(x_i)%20%3D%20%5Cbold%7Bw%7D%5ET%20%5Cbold%7Bx%7D_i%0A%20%20%20%20%5C%5D%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20def%20final_linear_regression(x%2C%20w)%3A%0A%20%20%20%20%20%20%20%20assert%20len(x)%20%2B%201%20%3D%3D%20len(w)%2C%20%22The%20size%20of%20the%20features%20vector%20does%20not%20match%20the%20size%20of%20the%20weights%22%0A%0A%20%20%20%20%20%20%20%20x%20%3D%20np.concat((np.array(%5B1%5D)%2C%20np.array(x)))%0A%20%20%20%20%20%20%20%20y_log%20%3D%20np.dot(x%2C%20w)%0A%0A%20%20%20%20%20%20%20%20return%20np.expm1(y_log).astype(int)%0A%20%20%20%20return%20(final_linear_regression%2C)%0A%0A%0A%40app.cell%0Adef%20_(final_linear_regression%2C%20get_random_car)%3A%0A%20%20%20%20final_linear_regression(get_random_car()%5B0%5D%2C%20%5B7.5%2C%200.01%2C%200.07%2C%200.003%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20We%20can%20write%20our%20%24g%24%20function%20for%20all%20samples%20as%20a%20matrix%20multiplication.%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%5Cbold%7By_p%7D%20%3D%20%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%201%20%26%20x_%7B11%7D%20%26%20x_%7B12%7D%20%26%20%5Ccdots%20%26%20x_%7B1n%7D%20%5C%5C%0A%20%20%20%201%20%26%20x_%7B21%7D%20%26%20x_%7B22%7D%20%26%20%5Ccdots%20%26%20x_%7B2n%7D%20%5C%5C%0A%20%20%20%20%5Cvdots%20%26%20%5Cvdots%20%26%20%5Cvdots%20%26%20%5Cddots%20%26%20%5Cvdots%20%5C%5C%0A%20%20%20%201%20%26%20x_%7Bm1%7D%20%26%20x_%7Bm2%7D%20%26%20%5Ccdots%20%26%20x_%7Bmn%7D%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20w_0%20%5C%5C%0A%20%20%20%20w_1%20%5C%5C%0A%20%20%20%20w_2%20%5C%5C%0A%20%20%20%20%5Cvdots%20%5C%5C%0A%20%20%20%20w_n%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20%5Cbold%7Bx_1%7D%5ET%20%C2%B7%20%5Cbold%7Bw%7D%20%5C%5C%0A%20%20%20%20%5Cbold%7Bx_2%7D%5ET%20%C2%B7%20%5Cbold%7Bw%7D%20%5C%5C%0A%20%20%20%20%5Cvdots%20%5C%5C%0A%20%20%20%20%5Cbold%7Bx_m%7D%5ET%20%C2%B7%20%5Cbold%7Bw%7D%20%5C%5C%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%5C%5D%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20np%2C%20select_features)%3A%0A%20%20%20%20X_matrix%20%3D%20select_features(X_train.iloc%5B10%3A15%5D)%0A%20%20%20%20X_matrix%20%3D%20np.c_%5Bnp.ones(X_matrix.shape%5B0%5D)%2C%20X_matrix%5D%0A%0A%20%20%20%20X_matrix%0A%20%20%20%20return%20(X_matrix%2C)%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20y_train)%3A%0A%20%20%20%20y_matrix%20%3D%20y_train%5B10%3A15%5D%0A%0A%20%20%20%20np.expm1(y_matrix).astype(int)%0A%20%20%20%20return%20(y_matrix%2C)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20w%20%3D%20%5B7.5%2C%200.01%2C%200.07%2C%200.003%5D%0A%0A%20%20%20%20w%0A%20%20%20%20return%20(w%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_matrix%2C%20np%2C%20w)%3A%0A%20%20%20%20np.expm1(X_matrix.dot(w)).astype(int)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Training%20a%20Linear%20Regression%20Model%0A%0A%20%20%20%20We%20already%20saw%20that%20our%20%24g%24%20estimator%20looks%20like%20this%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20%5Cbold%7BX%7D%20%5Cbold%7Bw%7D%20%5Capprox%20%5Cbold%7By%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20So%20now%20we%20have%20to%20solve%20%24%5Cbold%7Bw%7D%24%20so%20that%20we%20can%20find%20the%20weights.%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20%5Cbold%7BX%7D%5E%7B-1%7D%20%5Cbold%7BX%7D%20%5Cbold%7Bw%7D%20%5Capprox%20%5Cbold%7BX%7D%5E%7B-1%7D%20%5Cbold%7By%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20What%20gives%20us%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20%5Cbold%7Bw%7D%20%5Capprox%20%5Cbold%7BX%7D%5E%7B-1%7D%20%5Cbold%7By%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20But%20there%20is%20an%20issue%3A%20we%20are%20working%20with%20a%20rectangular%20matrix%2C%20not%20with%20a%20squared%20one.%20So%20we%20won't%20be%20able%20to%20find%20its%20inverse.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%23%20Gram%20Matrix%0A%0A%20%20%20%20The%20first%20thing%20we%20can%20do%20to%20try%20to%20solve%20this%20is%20to%20take%20into%20account%20that%20%24X%5ET%20X%24%20is%20actually%20an%20squared%20matrix%2C%20it%20may%20have%20an%20inverse.%0A%0A%20%20%20%20Therefore%2C%20we%20could%20start%20with%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20%5Cbold%7BX%7D%5E%7BT%7D%20%5Cbold%7BX%7D%20%5Cbold%7Bw%7D%20%5Capprox%20%5Cbold%7BX%7D%5E%7BT%7D%20%5Cbold%7By%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20...%20and%20try%20to%20solve%20it%20with%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20(%5Cbold%7BX%7D%5E%7BT%7D%20%5Cbold%7BX%7D)%5E%7B-1%7D%20%5Cbold%7BX%7D%5E%7BT%7D%20%5Cbold%7BX%7D%20%5Cbold%7Bw%7D%20%5Capprox%20(%5Cbold%7BX%7D%5E%7BT%7D%20%5Cbold%7BX%7D)%5E%7B-1%7D%20%5Cbold%7BX%7D%5E%7BT%7D%20%5Cbold%7By%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20...%20which%20simplifies%20to%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20%5Cbold%7Bw%7D%20%5Capprox%20(%5Cbold%7BX%7D%5E%7BT%7D%20%5Cbold%7BX%7D)%5E%7B-1%7D%20%5Cbold%7BX%7D%5E%7BT%7D%20%5Cbold%7By%7D%0A%20%20%20%20%5C%5D%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_matrix%2C%20np%2C%20y_matrix)%3A%0A%20%20%20%20def%20solve_linear_regression(X%2C%20y)%3A%0A%20%20%20%20%20%20%20%20return%20np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)%0A%0A%20%20%20%20solve_linear_regression(X_matrix%2C%20y_matrix)%0A%20%20%20%20return%20(solve_linear_regression%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_matrix%2C%20np%2C%20solve_linear_regression%2C%20y_matrix)%3A%0A%20%20%20%20def%20predict_price(X%2C%20y)%3A%0A%20%20%20%20%20%20%20%20w%20%3D%20solve_linear_regression(X%2C%20y)%0A%20%20%20%20%20%20%20%20y%20%3D%20X.dot(w)%0A%20%20%20%20%20%20%20%20return%20np.expm1(y).astype(int)%0A%0A%20%20%20%20predict_price(X_matrix%2C%20y_matrix)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Baseline%20Model%20for%20Car%20Price%20Prediction%20Project%0A%0A%20%20%20%20We'll%20start%20by%20selecting%20all%20the%20numerical%20features%20from%20our%20train%20dataset.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_train)%3A%0A%20%20%20%20def%20naive_prepare_X(X)%3A%0A%20%20%20%20%20%20%20%20return%20X.select_dtypes(include%3D%22number%22).values%0A%0A%20%20%20%20naive_prepare_X(X_train)%0A%20%20%20%20return%20(naive_prepare_X%2C)%0A%0A%0A%40app.cell%0Adef%20_(y_train)%3A%0A%20%20%20%20y_train%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%22Now%2C%20following%20the%20same%20formula%20we%20used%20in%20the%20previous%20section%2C%20we%20define%20our%20training%20function%2C%20which%20will%20take%20the%20features%20and%20target%20variable%20and%20return%20the%20corresponding%20weights.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20naive_prepare_X%2C%20solve_linear_regression%2C%20y_train)%3A%0A%20%20%20%20solve_linear_regression(naive_prepare_X(X_train)%2C%20y_train)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20naive_prepare_X%2C%20solve_linear_regression%2C%20y_train)%3A%0A%20%20%20%20def%20naive_predict(X%2C%20y)%3A%0A%20%20%20%20%20%20%20%20w%20%3D%20solve_linear_regression(naive_prepare_X(X)%2C%20y)%0A%20%20%20%20%20%20%20%20return%20naive_prepare_X(X).dot(w)%0A%0A%20%20%20%20naive_predict(X_train%2C%20y_train)%0A%20%20%20%20return%20(naive_predict%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22By%20plotting%20at%20the%20same%20time%20the%20target%20variable%20we%20used%20during%20training%20and%20our%20predictions%2C%20we%20can%20have%20a%20first%20idea%20about%20how%20good%20our%20model%20is.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20naive_predict%2C%20sns%2C%20y_train)%3A%0A%20%20%20%20sns.histplot(y_train%2C%20color%3D%22blue%22)%0A%20%20%20%20sns.histplot(naive_predict(X_train%2C%20y_train)%2C%20color%3D%22red%22%2C%20alpha%3D0.5)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Root%20Mean%20Squared%20Error%0A%0A%20%20%20%20In%20order%20to%20measure%20how%20good%20our%20model%20performs%2C%20we'll%20use%20%60RMSE%60%2C%20which%20is%20a%20metric%20that%20will%20tell%20us%20how%20good%20(or%20bad)%20our%20model%20predicted%20by%20checking%20the%20average%20of%20the%20squared%20difference%20between%20our%20predictions%60g(x_i)%60%20and%20the%20target%20values%20%60y_i%60%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20RMSE%5E2%20%3D%20%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi%3D0%7D%5En%20(g(x_i)%20-%20y_i)%5E2%0A%20%20%20%20%5C%5D%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20def%20rmse(y_predicted%2C%20y_real)%3A%0A%20%20%20%20%20%20%20%20n%20%3D%20len(y_real)%0A%20%20%20%20%20%20%20%20return%20np.sqrt(((y_predicted%20-%20y_real)**2).sum()%20%2F%20n)%0A%20%20%20%20return%20(rmse%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20naive_predict%2C%20rmse%2C%20y_train)%3A%0A%20%20%20%20rmse(%0A%20%20%20%20%20%20%20%20naive_predict(X_train%2C%20y_train)%2C%0A%20%20%20%20%20%20%20%20y_train%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Using%20RMSE%20on%20Validation%20Data%0A%0A%20%20%20%20In%20the%20previous%20chapter%20we%20used%20%60RMSE%60%20on%20the%20same%20dataset%20we%20used%20to%20train%20our%20model%2C%20which%20isn't%20what%20we%20want.%20Instead%2C%20we%20want%20to%20use%20the%20validate%20split%20we%20created%20already.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_val%2C%20naive_predict%2C%20y_val)%3A%0A%20%20%20%20naive_predict(X_val%2C%20y_val)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_val%2C%20naive_predict%2C%20rmse%2C%20y_val)%3A%0A%20%20%20%20rmse(%0A%20%20%20%20%20%20%20%20naive_predict(X_val%2C%20y_val)%2C%0A%20%20%20%20%20%20%20%20y_val%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Feature%20Engineering%0A%0A%20%20%20%20Let's%20add%20a%20feature%20that%20computes%20the%20age%20of%20the%20cars.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20datetime%20import%20datetime%0A%0A%20%20%20%20def%20feature_prepare_X(X)%3A%0A%20%20%20%20%20%20%20%20X%20%3D%20X.copy()%0A%20%20%20%20%20%20%20%20X%5B%22age%22%5D%20%3D%20datetime.today().year%20-%20X.year%0A%20%20%20%20%20%20%20%20return%20X.select_dtypes(include%3D%22number%22).values%0A%20%20%20%20return%20datetime%2C%20feature_prepare_X%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20feature_prepare_X)%3A%0A%20%20%20%20feature_prepare_X(X_train)%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%22Retraining%20our%20model%20we%20can%20see%20how%20it%20performs%20better%20with%20the%20additional%20feature.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(feature_prepare_X%2C%20solve_linear_regression)%3A%0A%20%20%20%20def%20feature_train_and_predict(X_train%2C%20y_train%2C%20X_val%2C%20y_val)%3A%0A%20%20%20%20%20%20%20%20w%20%3D%20solve_linear_regression(feature_prepare_X(X_train)%2C%20y_train)%0A%20%20%20%20%20%20%20%20return%20feature_prepare_X(X_val).dot(w)%0A%20%20%20%20return%20(feature_train_and_predict%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20X_val%2C%20feature_train_and_predict%2C%20rmse%2C%20y_train%2C%20y_val)%3A%0A%20%20%20%20rmse(%0A%20%20%20%20%20%20%20%20feature_train_and_predict(X_train%2C%20y_train%2C%20X_val%2C%20y_val)%2C%0A%20%20%20%20%20%20%20%20y_val%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20X_val%2C%20feature_train_and_predict%2C%20sns%2C%20y_train%2C%20y_val)%3A%0A%20%20%20%20sns.histplot(y_val%2C%20color%3D%22blue%22)%0A%20%20%20%20sns.histplot(feature_train_and_predict(X_train%2C%20y_train%2C%20X_val%2C%20y_val)%2C%20color%3D%22red%22%2C%20alpha%3D0.5)%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%23%23%20Categorical%20Variables%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_train)%3A%0A%20%20%20%20def%20categorize_column(df%2C%20column_name%2C%20transform%3DNone%2C%20drop_first%3DTrue)%3A%0A%20%20%20%20%20%20%20%20df%20%3D%20df.copy()%0A%0A%20%20%20%20%20%20%20%20if%20not%20transform%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20unique_values%20%3D%20sorted(df%5Bcolumn_name%5D.unique().astype(str))%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20unique_values%20%3D%20sorted(df%5Bcolumn_name%5D.unique())%0A%0A%20%20%20%20%20%20%20%20%23%20Skip%20the%20first%20value%20if%20drop_first%20is%20True%0A%20%20%20%20%20%20%20%20start_idx%20%3D%201%20if%20drop_first%20else%200%0A%0A%20%20%20%20%20%20%20%20for%20value%20in%20unique_values%5Bstart_idx%3A%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20transformed_value%20%3D%20transform(value)%20if%20callable(transform)%20else%20value%0A%20%20%20%20%20%20%20%20%20%20%20%20df%5Bcolumn_name%20%2B%20%22_%22%20%2B%20str(transformed_value)%5D%20%3D%20(df%5Bcolumn_name%5D%20%3D%3D%20value).astype(int)%0A%0A%20%20%20%20%20%20%20%20return%20df%0A%0A%20%20%20%20categorize_column(X_train%2C%20%22number_of_doors%22%2C%20int)%0A%20%20%20%20return%20(categorize_column%2C)%0A%0A%0A%40app.cell%0Adef%20_(car_prices%2C%20categorize_column%2C%20datetime)%3A%0A%20%20%20%20def%20categorize_prepare(%0A%20%20%20%20%20%20%20%20X%2C%0A%20%20%20%20%20%20%20%20categorical_columns%20%3D%20%5B%22make%22%2C%20%22engine_fuel_type%22%2C%20%22transmission_type%22%2C%20%22driven_wheels%22%2C%20%22vehicle_size%22%2C%20%22vehicle_style%22%5D%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20X%20%3D%20X.copy()%0A%20%20%20%20%20%20%20%20X%20%3D%20X.fillna(0)%0A%0A%20%20%20%20%20%20%20%20X%5B%22age%22%5D%20%3D%20datetime.today().year%20-%20X.year%0A%0A%20%20%20%20%20%20%20%20%23%20We'll%20ignore%20model%20and%20market%20category%20as%20they%20are%20too%20fragmented%0A%20%20%20%20%20%20%20%20if%20%22model%22%20in%20X%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20del%20X%5B%22model%22%5D%0A%0A%20%20%20%20%20%20%20%20if%20%22market_category%22%20in%20X%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20del%20X%5B%22market_category%22%5D%0A%0A%20%20%20%20%20%20%20%20X%20%3D%20categorize_column(X%2C%20%22number_of_doors%22%2C%20int)%0A%0A%20%20%20%20%20%20%20%20for%20categorical_column%20in%20categorical_columns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20X%20%3D%20categorize_column(X%2C%20categorical_column)%0A%0A%20%20%20%20%20%20%20%20del%20X%5B%22number_of_doors%22%5D%0A%20%20%20%20%20%20%20%20del%20X%5B%22make%22%5D%0A%20%20%20%20%20%20%20%20del%20X%5B%22engine_fuel_type%22%5D%0A%20%20%20%20%20%20%20%20del%20X%5B%22transmission_type%22%5D%0A%20%20%20%20%20%20%20%20del%20X%5B%22driven_wheels%22%5D%0A%20%20%20%20%20%20%20%20del%20X%5B%22vehicle_size%22%5D%0A%20%20%20%20%20%20%20%20del%20X%5B%22vehicle_style%22%5D%0A%0A%20%20%20%20%20%20%20%20return%20X%0A%0A%20%20%20%20categorize_prepare(car_prices).head()%0A%20%20%20%20return%20(categorize_prepare%2C)%0A%0A%0A%40app.cell%0Adef%20_(solve_linear_regression)%3A%0A%20%20%20%20def%20categorized_train_and_predict(X_train%2C%20y_train%2C%20X_val%2C%20y_val)%3A%0A%20%20%20%20%20%20%20%20w%20%3D%20solve_linear_regression(X_train%2C%20y_train)%0A%20%20%20%20%20%20%20%20return%20X_val.dot(w)%0A%20%20%20%20return%20(categorized_train_and_predict%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22At%20this%20point%20we'd%20expect%20to%20solve%20our%20system%20and%20obtain%20a%20better%20performance.%20But%20we'll%20very%20likely%20get%20a%20horrible%20result.%20Maybe%20even%20a%20_Singular%20matrix_%20error!%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20car_prices%2C%0A%20%20%20%20categorize_prepare%2C%0A%20%20%20%20categorized_train_and_predict%2C%0A%20%20%20%20rmse%2C%0A%20%20%20%20split_dataset%2C%0A)%3A%0A%20%20%20%20def%20categorized_eval()%3A%0A%20%20%20%20%20%20%20%20df_categorized%20%3D%20categorize_prepare(car_prices%2C%20%5B%22make%22%2C%20%22vehicle_size%22%5D)%0A%20%20%20%20%20%20%20%20df_train%2C%20df_val%2C%20df_test%20%3D%20split_dataset(df_categorized)%0A%0A%20%20%20%20%20%20%20%20X_train%2C%20y_train%20%3D%20df_train%0A%20%20%20%20%20%20%20%20X_val%2C%20y_val%20%3D%20df_val%0A%0A%20%20%20%20%20%20%20%20return%20rmse(%0A%20%20%20%20%20%20%20%20%20%20%20%20categorized_train_and_predict(X_train%2C%20y_train%2C%20X_val%2C%20y_val)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y_val%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20categorized_eval()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Regularization%0A%0A%20%20%20%20When%20we%20introduced%20categorical%20variables%20in%20our%20model%2C%20we%20also%20increased%20the%20chance%20of%20having%20features%20that%20are%20dependent%20of%20each%20other.%20For%20instance%2C%20if%20we%20had%203%20possible%20values%20for%20**number%20of%20doors**%20and%20we%20created%203%20boolean%20columns%2C%20one%20for%20each%20of%20the%20values%2C%20then%20each%20of%20the%20columns%20could%20be%20deduced%20from%20the%20others.%20In%20other%20words%2C%20they%20would%20be%20linearly%20dependent.%20That%20means%20that%20the%20inverse%20of%20%24X%5ET%20X%24%20won't%20even%20be%20defined.%0A%0A%20%20%20%20In%20order%20to%20solve%20this%2C%20one%20approach%20is%20to%20add%20a%20diagonal%20multiplied%20by%20a%20small%20factor%20so%20that%20we%20get%20a%20%24X%5ET%20X%20%2B%20%5Cdelta%20%5Cmathbb%7BI%7D%24%20that%20has%20an%20inverse.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20delta%20%3D%200.01%0A%20%20%20%20return%20(delta%2C)%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20def%20regularize_and_solve_linear_regression(X%2C%20y%2C%20delta)%3A%0A%20%20%20%20%20%20%20%20return%20np.linalg.inv(X.T.dot(X)%20%2B%20delta%20*%20np.eye(X.shape%5B1%5D)).dot(X.T).dot(y)%0A%20%20%20%20return%20(regularize_and_solve_linear_regression%2C)%0A%0A%0A%40app.cell%0Adef%20_(regularize_and_solve_linear_regression)%3A%0A%20%20%20%20def%20regularized_train_and_predict(X_train%2C%20y_train%2C%20X_val%2C%20y_val%2C%20delta)%3A%0A%20%20%20%20%20%20%20%20w%20%3D%20regularize_and_solve_linear_regression(X_train%2C%20y_train%2C%20delta)%0A%20%20%20%20%20%20%20%20return%20X_val.dot(w)%0A%20%20%20%20return%20(regularized_train_and_predict%2C)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20car_prices%2C%0A%20%20%20%20categorize_prepare%2C%0A%20%20%20%20delta%2C%0A%20%20%20%20regularized_train_and_predict%2C%0A%20%20%20%20rmse%2C%0A%20%20%20%20split_dataset%2C%0A)%3A%0A%20%20%20%20def%20regularize_categorized_eval(delta)%3A%0A%20%20%20%20%20%20%20%20df_categorized%20%3D%20categorize_prepare(car_prices)%0A%20%20%20%20%20%20%20%20df_train%2C%20df_val%2C%20df_test%20%3D%20split_dataset(df_categorized)%0A%0A%20%20%20%20%20%20%20%20X_train%2C%20y_train%20%3D%20df_train%0A%20%20%20%20%20%20%20%20X_val%2C%20y_val%20%3D%20df_val%0A%0A%20%20%20%20%20%20%20%20return%20rmse(%0A%20%20%20%20%20%20%20%20%20%20%20%20regularized_train_and_predict(X_train%2C%20y_train%2C%20X_val%2C%20y_val%2C%20delta)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y_val%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20regularize_categorized_eval(delta)%0A%20%20%20%20return%20(regularize_categorized_eval%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Tuning%20the%20Model%0A%0A%20%20%20%20To%20obtain%20the%20best%20%24delta%24%20for%20our%20model%2C%20we%20can%20start%20by%20just%20trying%20some%20values%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20regularize_categorized_eval)%3A%0A%20%20%20%20def%20find_delta()%3A%0A%20%20%20%20%20%20%20%20results%20%3D%20%7B%7D%0A%0A%20%20%20%20%20%20%20%20for%20n%20in%20np.linspace(9.5%2C%2010%2C%2020)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20delta%20%3D%201%2F(10**n)%0A%20%20%20%20%20%20%20%20%20%20%20%20results%5Bdelta%5D%20%3D%20regularize_categorized_eval(delta)%0A%0A%20%20%20%20%20%20%20%20return%20results%0A%0A%20%20%20%20regularization_deltas%20%3D%20find_delta()%0A%0A%20%20%20%20regularization_deltas%0A%20%20%20%20return%20(regularization_deltas%2C)%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20regularization_deltas)%3A%0A%20%20%20%20def%20plot_deltas(regularization_deltas)%3A%0A%20%20%20%20%20%20%20%20x%20%3D%20regularization_deltas.keys()%0A%20%20%20%20%20%20%20%20y%20%3D%20regularization_deltas.values()%0A%20%20%20%20%20%20%20%20plt.plot(x%2C%20y)%0A%20%20%20%20%20%20%20%20plt.xlabel('%CE%B4')%0A%20%20%20%20%20%20%20%20plt.ylabel('RMSE')%0A%20%20%20%20%20%20%20%20plt.show()%0A%0A%20%20%20%20plot_deltas(regularization_deltas)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(regularization_deltas)%3A%0A%20%20%20%20def%20get_min_regularization(regularization_deltas)%3A%0A%20%20%20%20%20%20%20%20min_delta%20%3D%20None%0A%20%20%20%20%20%20%20%20min_rmse%20%3D%20None%0A%0A%20%20%20%20%20%20%20%20for%20delta%2C%20rmse%20in%20regularization_deltas.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20min_rmse%20is%20None%20or%20rmse%20%3C%20min_rmse%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min_rmse%20%3D%20rmse%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min_delta%20%3D%20delta%0A%0A%20%20%20%20%20%20%20%20return%20(min_delta%2C%20min_rmse)%0A%0A%20%20%20%20(regularization_delta%2C%20_)%20%3D%20get_min_regularization(regularization_deltas)%0A%20%20%20%20return%20(regularization_delta%2C)%0A%0A%0A%40app.cell%0Adef%20_(regularization_delta%2C%20regularize_categorized_eval)%3A%0A%20%20%20%20regularize_categorized_eval(regularization_delta)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Using%20the%20Model%0A%0A%20%20%20%20At%20this%20point%2C%20we'll%20train%20our%20%60g%60%20model%20on%20a%20split%20that%20combines%20the%20train%20and%20validation%20splits.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%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%23%23%23%20Prepare%20a%20Merged%20Dataset%20with%20Train%20and%20Validation%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(car_prices%2C%20categorize_prepare%2C%20np%2C%20split_dataset)%3A%0A%20%20%20%20def%20get_full_train()%3A%0A%20%20%20%20%20%20%20%20df_categorized%20%3D%20categorize_prepare(car_prices)%0A%20%20%20%20%20%20%20%20df_train%2C%20df_val%2C%20df_test%20%3D%20split_dataset(df_categorized)%0A%0A%20%20%20%20%20%20%20%20X_train%2C%20y_train%20%3D%20df_train%0A%20%20%20%20%20%20%20%20X_val%2C%20y_val%20%3D%20df_val%0A%20%20%20%20%20%20%20%20X_test%2C%20y_test%20%3D%20df_test%0A%0A%20%20%20%20%20%20%20%20X%20%3D%20np.concat(%5BX_train%2C%20X_val%5D)%0A%20%20%20%20%20%20%20%20y%20%3D%20np.concat(%5By_train%2C%20y_val%5D)%0A%0A%20%20%20%20%20%20%20%20return%20X%2C%20y%2C%20X_test.values%2C%20y_test%0A%0A%20%20%20%20get_full_train()%0A%20%20%20%20return%20(get_full_train%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%23%20Train%20the%20Model%0A%0A%20%20%20%20Here%20we%20are%20training%20the%20model%20in%20our%20merged%20dataset%20and%20displaying%20its%20weights.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20get_full_train%2C%0A%20%20%20%20regularization_delta%2C%0A%20%20%20%20regularize_and_solve_linear_regression%2C%0A)%3A%0A%20%20%20%20def%20solve_full_train(delta)%3A%0A%20%20%20%20%20%20%20%20X%2C%20y%2C%20X_test%2C%20y_test%20%3D%20get_full_train()%0A%0A%20%20%20%20%20%20%20%20return%20regularize_and_solve_linear_regression(X%2C%20y%2C%20delta)%0A%0A%20%20%20%20solve_full_train(regularization_delta)%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%23%23%23%20Check%20that%20we%20Can%20Predict%20Prices%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20get_full_train%2C%0A%20%20%20%20regularization_delta%2C%0A%20%20%20%20regularize_and_solve_linear_regression%2C%0A)%3A%0A%20%20%20%20def%20full_train_and_predict(delta)%3A%0A%20%20%20%20%20%20%20%20X%2C%20y%2C%20X_test%2C%20y_test%20%3D%20get_full_train()%0A%20%20%20%20%20%20%20%20w%20%3D%20regularize_and_solve_linear_regression(X%2C%20y%2C%20delta)%0A%0A%20%20%20%20%20%20%20%20return%20X.dot(w)%0A%0A%20%20%20%20full_train_and_predict(regularization_delta)%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%23%23%23%20Evaluate%20the%20Model%20on%20the%20Test%20Split%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20get_full_train%2C%0A%20%20%20%20regularization_delta%2C%0A%20%20%20%20regularize_and_solve_linear_regression%2C%0A%20%20%20%20rmse%2C%0A)%3A%0A%20%20%20%20def%20full_train_eval(delta)%3A%0A%20%20%20%20%20%20%20%20X%2C%20y%2C%20X_test%2C%20y_test%20%3D%20get_full_train()%0A%20%20%20%20%20%20%20%20w%20%3D%20regularize_and_solve_linear_regression(X%2C%20y%2C%20delta)%0A%0A%20%20%20%20%20%20%20%20return%20rmse(%0A%20%20%20%20%20%20%20%20%20%20%20%20X_test.dot(w)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y_test%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20full_train_eval(regularization_delta)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%23%20Use%20the%20Model%20to%20Predict%20a%20Car's%20Price%0A%0A%20%20%20%20Now%20we%20can%20use%20our%20model%20to%20predict%20the%20price%20of%20a%20given%20car.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20get_full_train%2C%0A%20%20%20%20np%2C%0A%20%20%20%20regularization_delta%2C%0A%20%20%20%20regularize_and_solve_linear_regression%2C%0A)%3A%0A%20%20%20%20def%20get_test_car_price(car_id%2C%20delta)%3A%0A%20%20%20%20%20%20%20%20X%2C%20y%2C%20X_test%2C%20y_test%20%3D%20get_full_train()%0A%20%20%20%20%20%20%20%20w%20%3D%20regularize_and_solve_linear_regression(X%2C%20y%2C%20delta)%0A%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22y%22%3A%20np.expm1(y_test%5Bcar_id%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22y_predicted%22%3A%20np.expm1(np.array(%5BX_test%5Bcar_id%5D%5D).dot(w)%5B0%5D)%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20car_id%20%3D%2020%0A%20%20%20%20get_test_car_price(car_id%2C%20regularization_delta)%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
5c6150815f7c04e016dd039fc4d0ae58fecfd930bbec364e21448696abbd26af