{"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":"none","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch, numpy as np, pandas as pd, matplotlib.pyplot as plt\nimport xgboost as xgb, lightgbm as lgb\nfrom pathlib import Path\nfrom fastai.tabular.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:32:39.030173Z","iopub.execute_input":"2024-12-31T23:32:39.030421Z","iopub.status.idle":"2024-12-31T23:32:47.136464Z","shell.execute_reply.started":"2024-12-31T23:32:39.030395Z","shell.execute_reply":"2024-12-31T23:32:47.135462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:32:47.137503Z","iopub.execute_input":"2024-12-31T23:32:47.138214Z","iopub.status.idle":"2024-12-31T23:32:47.142663Z","shell.execute_reply.started":"2024-12-31T23:32:47.138174Z","shell.execute_reply":"2024-12-31T23:32:47.141466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path = Path('/kaggle/input/playground-series-s4e12')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:32:49.390183Z","iopub.execute_input":"2024-12-31T23:32:49.390508Z","iopub.status.idle":"2024-12-31T23:32:49.394903Z","shell.execute_reply.started":"2024-12-31T23:32:49.390482Z","shell.execute_reply":"2024-12-31T23:32:49.393981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(path/'train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:32:50.023015Z","iopub.execute_input":"2024-12-31T23:32:50.023412Z","iopub.status.idle":"2024-12-31T23:32:56.306133Z","shell.execute_reply.started":"2024-12-31T23:32:50.02338Z","shell.execute_reply":"2024-12-31T23:32:56.304962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def extract_features(df):\n    df.columns = df.columns.str.replace(' ', '_')\n    df['Policy_Start_Date'] = pd.to_datetime(df.Policy_Start_Date)\n    date = df.Policy_Start_Date.dt\n    df.drop(['Policy_Start_Date'], axis=1)\n    \n    df['Year'] = date.year.astype(float)\n    df['Month'] = date.month.astype(float)\n    df['Day'] = date.day.astype(float)\n    df['Week']  = date.isocalendar().week.astype(float)\n    df['Weekday'] = date.weekday.astype(float)\n    df['Epoch'] = df.Policy_Start_Date.astype(np.int64) / 10**9\n    \n    df['Year_sin'] = np.sin(2 * np.pi * df.Year)\n    df['Year_cos'] = np.cos(2 * np.pi * df.Year)\n    df['Month_sin'] = np.sin(2 * np.pi * df.Month / 12) \n    df['Month_cos'] = np.cos(2 * np.pi * df.Month / 12)\n    \n    df['Income_Age_Ratio'] = df.Annual_Income / df.Age\n    df['Log_Annual_Income'] = np.log1p(df.Annual_Income)\n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:32:56.307572Z","iopub.execute_input":"2024-12-31T23:32:56.307981Z","iopub.status.idle":"2024-12-31T23:32:56.318322Z","shell.execute_reply.started":"2024-12-31T23:32:56.307945Z","shell.execute_reply":"2024-12-31T23:32:56.317124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def fill_null(df):\n    for col in df:\n        if df[col].isna().sum() > 0: df[col + '_na'] = df[col].isna().astype(float)\n    df = df.fillna(df.mode().iloc[0])\n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:32:56.320335Z","iopub.execute_input":"2024-12-31T23:32:56.320709Z","iopub.status.idle":"2024-12-31T23:32:56.33414Z","shell.execute_reply.started":"2024-12-31T23:32:56.320678Z","shell.execute_reply":"2024-12-31T23:32:56.332647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def categorify(df, encode=False):\n    conts,cats = cont_cat_split(df)\n    for col in cats:\n        if encode:\n            df[col] = pd.Categorical(df[col])\n            df[col] = df[col].cat.codes\n        else: df[col] = df[col].astype('category')\n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:54:51.58545Z","iopub.execute_input":"2024-12-31T23:54:51.586197Z","iopub.status.idle":"2024-12-31T23:54:51.591786Z","shell.execute_reply.started":"2024-12-31T23:54:51.586154Z","shell.execute_reply":"2024-12-31T23:54:51.590767Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def split_indeps_deps(df, log=False):\n    indeps = df\n    deps = None\n    if 'id' in df.columns: indeps = df.drop(['id'], axis=1)\n    if 'Premium_Amount' in df.columns:\n        indeps = indeps.drop('Premium_Amount', axis=1)\n        deps = df.Premium_Amount\n        if log: deps = np.log1p(deps)\n    return indeps,deps","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:32:56.34518Z","iopub.execute_input":"2024-12-31T23:32:56.345521Z","iopub.status.idle":"2024-12-31T23:32:56.35376Z","shell.execute_reply.started":"2024-12-31T23:32:56.345489Z","shell.execute_reply":"2024-12-31T23:32:56.35254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_indeps(df, encode=False, log=False):\n    df = extract_features(df)\n    df = fill_null(df)\n    df = categorify(df, encode=encode)\n    indeps,_ = split_indeps_deps(df, log=log)\n    return indeps","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:52:50.614234Z","iopub.execute_input":"2024-12-31T23:52:50.614725Z","iopub.status.idle":"2024-12-31T23:52:50.621092Z","shell.execute_reply.started":"2024-12-31T23:52:50.61468Z","shell.execute_reply":"2024-12-31T23:52:50.619725Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_deps(df):\n    _,deps = split_indeps_deps(df)\n    return deps","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:54:37.650083Z","iopub.execute_input":"2024-12-31T23:54:37.650528Z","iopub.status.idle":"2024-12-31T23:54:37.656593Z","shell.execute_reply.started":"2024-12-31T23:54:37.650488Z","shell.execute_reply":"2024-12-31T23:54:37.655142Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"deps = get_deps(df)\ndeps_log = np.log1p(deps)\ncategory_indeps = get_indeps(df, encode=False)\nencoded_indeps = get_indeps(df, encode=True)\ncategory_indeps.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:54:53.048352Z","iopub.execute_input":"2024-12-31T23:54:53.048684Z","iopub.status.idle":"2024-12-31T23:55:10.150785Z","shell.execute_reply.started":"2024-12-31T23:54:53.048657Z","shell.execute_reply":"2024-12-31T23:55:10.149693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoded_indeps.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:55:11.2799Z","iopub.execute_input":"2024-12-31T23:55:11.280266Z","iopub.status.idle":"2024-12-31T23:55:11.312561Z","shell.execute_reply.started":"2024-12-31T23:55:11.280236Z","shell.execute_reply":"2024-12-31T23:55:11.311418Z"}},"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},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ensemble_settings = {\n    'XGB': {\n        'params': {\n            'learning_rate': 0.040343472274915984,\n            'max_depth': 8,\n            'n_estimators': 187,\n            'reg_alpha': 0.005329228444240297,\n            'reg_lambda': 10.243393225105073\n        },\n        'drop_cols': ['Year', 'Income_Age_Ratio_na'],\n        'indeps': get_indeps(df, encode=True)\n        'test_indeps': get_indeps(test_df, encode=True)\n    },\n    'LGB': {\n        'params': {\n            'learning_rate': 0.11040568023673476,\n            'max_depth': 10,\n            'n_estimators': 100,\n            'reg_alpha': 0.03833403913374451,\n            'reg_lambda': 0.508018478116339\n        },\n        'drop_cols': ['Epoch', 'Year_cos', 'Vehicle_Age_na', 'Insurance_Duration_na', 'Log_Annual_Income_na', 'Income_Age_Ratio_na', 'Age_na', 'Gender'],\n        'indeps': get_indeps(df, encode=False)\n        'test_indeps': get_indeps(test_df, encode=False)\n    }\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:55:49.80763Z","iopub.execute_input":"2024-12-31T23:55:49.808089Z","iopub.status.idle":"2024-12-31T23:56:05.319695Z","shell.execute_reply.started":"2024-12-31T23:55:49.808049Z","shell.execute_reply":"2024-12-31T23:56:05.318283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_model(arch):\n    params = ensemble_settings[arch]['params']\n    if arch == 'XGB': return xgb.XGBRegressor(**params)\n    if arch == 'LGB': return lgb.LGBMRegressor(**params, verbose=-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:57:48.400151Z","iopub.execute_input":"2024-12-31T23:57:48.400509Z","iopub.status.idle":"2024-12-31T23:57:48.405162Z","shell.execute_reply.started":"2024-12-31T23:57:48.400477Z","shell.execute_reply":"2024-12-31T23:57:48.404072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"individual_preds = []\nfor arch in ['XGB', 'LGB']:\n    model = get_model(arch)\n    drop_cols = ensemble_settings[arch]['drop_cols']\n    indeps = ensemble_settings[arch]['indeps']\n    model.fit(indeps.drop(drop_cols, axis=1), deps_log)\n    test_indeps = ensemble_settings[arch]['test_indeps']\n    individual_preds.append(model.predict(test_indeps.drop(drop_cols, axis=1)))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ensemble_preds = np.mean(individual_preds, axis=0)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submit_df['Premium Amount'] = ensemble_preds\nsubmit_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:46:22.509692Z","iopub.execute_input":"2024-12-31T23:46:22.510165Z","iopub.status.idle":"2024-12-31T23:46:22.521348Z","shell.execute_reply.started":"2024-12-31T23:46:22.510124Z","shell.execute_reply":"2024-12-31T23:46:22.520361Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submit_df.to_csv('insurance_v13.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T23:46:31.036803Z","iopub.execute_input":"2024-12-31T23:46:31.037198Z","iopub.status.idle":"2024-12-31T23:46:32.597198Z","shell.execute_reply.started":"2024-12-31T23:46:31.037164Z","shell.execute_reply":"2024-12-31T23:46:32.596163Z"}},"outputs":[],"execution_count":null}]}