{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch, numpy as np, pandas as pd, matplotlib.pyplot as plt\nfrom pathlib import Path\nfrom fastai.tabular.core import *\nfrom fastai.tabular.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-24T00:08:07.091957Z","iopub.execute_input":"2024-12-24T00:08:07.092434Z","iopub.status.idle":"2024-12-24T00:08:07.099957Z","shell.execute_reply.started":"2024-12-24T00:08:07.092398Z","shell.execute_reply":"2024-12-24T00:08:07.099032Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path = Path('/kaggle/input/playground-series-s4e12')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T00:08:07.455384Z","iopub.execute_input":"2024-12-24T00:08:07.455701Z","iopub.status.idle":"2024-12-24T00:08:07.459681Z","shell.execute_reply.started":"2024-12-24T00:08:07.455673Z","shell.execute_reply":"2024-12-24T00:08:07.458782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(path/'train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T00:08:07.765648Z","iopub.execute_input":"2024-12-24T00:08:07.766043Z","iopub.status.idle":"2024-12-24T00:08:11.139764Z","shell.execute_reply.started":"2024-12-24T00:08:07.766009Z","shell.execute_reply":"2024-12-24T00:08:11.138814Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:38:59.259302Z","iopub.execute_input":"2024-12-23T23:38:59.259594Z","iopub.status.idle":"2024-12-23T23:38:59.291774Z","shell.execute_reply.started":"2024-12-23T23:38:59.259573Z","shell.execute_reply":"2024-12-23T23:38:59.290916Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:44:54.751121Z","iopub.execute_input":"2024-12-23T23:44:54.751526Z","iopub.status.idle":"2024-12-23T23:44:54.756656Z","shell.execute_reply.started":"2024-12-23T23:44:54.751498Z","shell.execute_reply":"2024-12-23T23:44:54.755802Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:09:08.107132Z","iopub.execute_input":"2024-12-23T23:09:08.107488Z","iopub.status.idle":"2024-12-23T23:09:08.114433Z","shell.execute_reply.started":"2024-12-23T23:09:08.107456Z","shell.execute_reply":"2024-12-23T23:09:08.113634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:09:08.333127Z","iopub.execute_input":"2024-12-23T23:09:08.333445Z","iopub.status.idle":"2024-12-23T23:09:08.982484Z","shell.execute_reply.started":"2024-12-23T23:09:08.33342Z","shell.execute_reply":"2024-12-23T23:09:08.981704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe(include=object)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:09:09.413998Z","iopub.execute_input":"2024-12-23T23:09:09.414315Z","iopub.status.idle":"2024-12-23T23:09:11.284395Z","shell.execute_reply.started":"2024-12-23T23:09:09.414289Z","shell.execute_reply":"2024-12-23T23:09:11.283427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:37:08.108438Z","iopub.execute_input":"2024-12-23T23:37:08.108817Z","iopub.status.idle":"2024-12-23T23:37:08.686864Z","shell.execute_reply.started":"2024-12-23T23:37:08.108785Z","shell.execute_reply":"2024-12-23T23:37:08.685694Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modes = df.mode().iloc[0]\ndf.fillna(modes, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:09:11.851436Z","iopub.execute_input":"2024-12-23T23:09:11.851792Z","iopub.status.idle":"2024-12-23T23:09:15.560061Z","shell.execute_reply.started":"2024-12-23T23:09:11.851763Z","shell.execute_reply":"2024-12-23T23:09:15.559026Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:09:15.56119Z","iopub.execute_input":"2024-12-23T23:09:15.561455Z","iopub.status.idle":"2024-12-23T23:09:16.131316Z","shell.execute_reply.started":"2024-12-23T23:09:15.561431Z","shell.execute_reply":"2024-12-23T23:09:16.13044Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conts,cats = cont_cat_split(df)\nconts = conts[1:-1]\nconts","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:40:17.00277Z","iopub.execute_input":"2024-12-23T23:40:17.003196Z","iopub.status.idle":"2024-12-23T23:40:17.039088Z","shell.execute_reply.started":"2024-12-23T23:40:17.003143Z","shell.execute_reply":"2024-12-23T23:40:17.038263Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def encode_cats(df, cats=None):\n    for col in cats:\n        df[col] = pd.Categorical(df[col])\n        df[col] = df[col].cat.codes\n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T22:46:38.372409Z","iopub.execute_input":"2024-12-23T22:46:38.372711Z","iopub.status.idle":"2024-12-23T22:46:38.376706Z","shell.execute_reply.started":"2024-12-23T22:46:38.372686Z","shell.execute_reply":"2024-12-23T22:46:38.375885Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = encode_cats(df, cats=cats)\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T22:46:38.926934Z","iopub.execute_input":"2024-12-23T22:46:38.927203Z","iopub.status.idle":"2024-12-23T22:46:40.068977Z","shell.execute_reply.started":"2024-12-23T22:46:38.927183Z","shell.execute_reply":"2024-12-23T22:46:40.068049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"indep_cols = conts + cats\nindeps = df[indep_cols]\ndeps = df['Premium Amount']\nindeps.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:09:31.078711Z","iopub.execute_input":"2024-12-23T23:09:31.079039Z","iopub.status.idle":"2024-12-23T23:09:31.251808Z","shell.execute_reply.started":"2024-12-23T23:09:31.079015Z","shell.execute_reply":"2024-12-23T23:09:31.250747Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nrf = RandomForestRegressor(5, min_samples_leaf=1000, n_jobs=-1).fit(indeps, deps)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T22:24:49.264745Z","iopub.execute_input":"2024-12-23T22:24:49.265246Z","iopub.status.idle":"2024-12-23T22:25:19.907007Z","shell.execute_reply.started":"2024-12-23T22:24:49.265207Z","shell.execute_reply":"2024-12-23T22:25:19.905505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dict(zip(indeps.columns, rf.feature_importances_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T22:28:43.834265Z","iopub.execute_input":"2024-12-23T22:28:43.834773Z","iopub.status.idle":"2024-12-23T22:28:43.858262Z","shell.execute_reply.started":"2024-12-23T22:28:43.83473Z","shell.execute_reply":"2024-12-23T22:28:43.857142Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.DataFrame(dict(cols=indeps.columns, importance=rf.feature_importances_)).plot('cols', 'importance', 'barh')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T22:25:26.910009Z","iopub.execute_input":"2024-12-23T22:25:26.910411Z","iopub.status.idle":"2024-12-23T22:25:27.315015Z","shell.execute_reply.started":"2024-12-23T22:25:26.910375Z","shell.execute_reply":"2024-12-23T22:25:27.313825Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rf.feature_importances_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T22:25:41.264616Z","iopub.execute_input":"2024-12-23T22:25:41.264988Z","iopub.status.idle":"2024-12-23T22:25:41.287316Z","shell.execute_reply.started":"2024-12-23T22:25:41.264957Z","shell.execute_reply":"2024-12-23T22:25:41.286163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from fastai.data.transforms import RandomSplitter\ntrain_split,valid_split = RandomSplitter(valid_pct=0.2)(df)\ntrain_indeps,train_deps = indeps.iloc[train_split],deps.iloc[train_split]\nvalid_indeps,valid_deps = indeps.iloc[valid_split],deps.iloc[valid_split]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:09:37.274859Z","iopub.execute_input":"2024-12-23T23:09:37.275173Z","iopub.status.idle":"2024-12-23T23:09:38.370109Z","shell.execute_reply.started":"2024-12-23T23:09:37.275149Z","shell.execute_reply":"2024-12-23T23:09:38.369328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"splits = RandomSplitter(valid_pct=0.2)(range_of(df))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:10:17.422748Z","iopub.execute_input":"2024-12-23T23:10:17.423087Z","iopub.status.idle":"2024-12-23T23:10:17.612726Z","shell.execute_reply.started":"2024-12-23T23:10:17.42305Z","shell.execute_reply":"2024-12-23T23:10:17.61172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:10:19.036048Z","iopub.execute_input":"2024-12-23T23:10:19.036371Z","iopub.status.idle":"2024-12-23T23:10:19.041602Z","shell.execute_reply.started":"2024-12-23T23:10:19.036348Z","shell.execute_reply":"2024-12-23T23:10:19.040725Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"splits","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:10:19.651901Z","iopub.execute_input":"2024-12-23T23:10:19.652348Z","iopub.status.idle":"2024-12-23T23:10:19.658374Z","shell.execute_reply.started":"2024-12-23T23:10:19.652309Z","shell.execute_reply":"2024-12-23T23:10:19.657292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import gc\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:24:22.849776Z","iopub.execute_input":"2024-12-23T23:24:22.850107Z","iopub.status.idle":"2024-12-23T23:24:23.292406Z","shell.execute_reply.started":"2024-12-23T23:24:22.850079Z","shell.execute_reply":"2024-12-23T23:24:23.291486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.lr_find()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from fastai.tabular.all import *\ndls = TabularDataLoaders.from_df(df, procs=[Categorify, FillMissing, Normalize], cat_names=cats, cont_names=conts,\n                                 y_names='Premium Amount', splits=RandomSplitter(valid_pct=0.2)(range_of(df)), bs=128)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:45:15.161514Z","iopub.execute_input":"2024-12-23T23:45:15.162549Z","iopub.status.idle":"2024-12-23T23:45:18.370477Z","shell.execute_reply.started":"2024-12-23T23:45:15.162481Z","shell.execute_reply":"2024-12-23T23:45:18.369523Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_log_error\ndef rmsle(preds, targs):\n    preds = preds.cpu().numpy() if preds.is_cuda else preds.numpy()\n    targs = targs.cpu().numpy() if targs.is_cuda else targs.numpy()\n    preds = np.maximum(0, preds)\n    targs = np.maximum(0, targs)\n    return np.sqrt(mean_squared_log_error(targs, preds))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:45:05.870179Z","iopub.execute_input":"2024-12-23T23:45:05.87052Z","iopub.status.idle":"2024-12-23T23:45:05.875306Z","shell.execute_reply.started":"2024-12-23T23:45:05.870495Z","shell.execute_reply":"2024-12-23T23:45:05.874272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn = tabular_learner(dls, metrics=rmsle)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:41:17.523679Z","iopub.execute_input":"2024-12-23T23:41:17.523986Z","iopub.status.idle":"2024-12-23T23:41:19.254103Z","shell.execute_reply.started":"2024-12-23T23:41:17.523962Z","shell.execute_reply":"2024-12-23T23:41:19.253343Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torch.cuda.device_count()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:22:41.55376Z","iopub.execute_input":"2024-12-23T23:22:41.554099Z","iopub.status.idle":"2024-12-23T23:22:41.559166Z","shell.execute_reply.started":"2024-12-23T23:22:41.55407Z","shell.execute_reply":"2024-12-23T23:22:41.558161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.to_parallel()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.fit_one_cycle(5)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df = pd.read_csv(path/'test.csv')\nsubmit_df = pd.read_csv(path/'sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:51:16.507657Z","iopub.execute_input":"2024-12-23T23:51:16.507949Z","iopub.status.idle":"2024-12-23T23:51:18.848374Z","shell.execute_reply.started":"2024-12-23T23:51:16.507927Z","shell.execute_reply":"2024-12-23T23:51:18.847321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:51:33.511874Z","iopub.execute_input":"2024-12-23T23:51:33.512321Z","iopub.status.idle":"2024-12-23T23:51:33.539297Z","shell.execute_reply.started":"2024-12-23T23:51:33.51228Z","shell.execute_reply":"2024-12-23T23:51:33.538444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dl = learn.dls.test_dl(test_df)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds, _ = learn.get_preds(dl=test_dl)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submit_df['Premium Amount'] = preds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:58:47.138095Z","iopub.execute_input":"2024-12-23T23:58:47.138434Z","iopub.status.idle":"2024-12-23T23:58:47.142963Z","shell.execute_reply.started":"2024-12-23T23:58:47.138408Z","shell.execute_reply":"2024-12-23T23:58:47.141925Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submit_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T23:58:49.135945Z","iopub.execute_input":"2024-12-23T23:58:49.136303Z","iopub.status.idle":"2024-12-23T23:58:49.144024Z","shell.execute_reply.started":"2024-12-23T23:58:49.136273Z","shell.execute_reply":"2024-12-23T23:58:49.14323Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submit_df.to_csv('insurance_v1.csv', index=False)\n!head submission.csv","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T00:02:39.183004Z","iopub.execute_input":"2024-12-24T00:02:39.183476Z","iopub.status.idle":"2024-12-24T00:02:40.542326Z","shell.execute_reply.started":"2024-12-24T00:02:39.183437Z","shell.execute_reply":"2024-12-24T00:02:40.541106Z"}},"outputs":[],"execution_count":null}]}