{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Import Libraries ","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings(\"ignore\")\npd.set_option('display.max_columns', None)\n\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.preprocessing import StandardScaler\n\n\nimport os","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:19.352973Z","iopub.execute_input":"2025-06-24T15:00:19.353254Z","iopub.status.idle":"2025-06-24T15:00:33.505036Z","shell.execute_reply.started":"2025-06-24T15:00:19.353216Z","shell.execute_reply":"2025-06-24T15:00:33.504362Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Import Data","metadata":{}},{"cell_type":"code","source":"\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:33.506467Z","iopub.execute_input":"2025-06-24T15:00:33.507097Z","iopub.status.idle":"2025-06-24T15:00:33.513851Z","shell.execute_reply.started":"2025-06-24T15:00:33.507076Z","shell.execute_reply":"2025-06-24T15:00:33.513102Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### EDA & Data Preprocessing","metadata":{}},{"cell_type":"code","source":"df1 = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\ndf1.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:33.514488Z","iopub.execute_input":"2025-06-24T15:00:33.514775Z","iopub.status.idle":"2025-06-24T15:00:38.633727Z","shell.execute_reply.started":"2025-06-24T15:00:33.514748Z","shell.execute_reply":"2025-06-24T15:00:38.633114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df2 = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")\ndf2.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:38.635094Z","iopub.execute_input":"2025-06-24T15:00:38.635336Z","iopub.status.idle":"2025-06-24T15:00:41.758781Z","shell.execute_reply.started":"2025-06-24T15:00:38.63532Z","shell.execute_reply":"2025-06-24T15:00:41.758027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df1.shape, df2.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:41.759547Z","iopub.execute_input":"2025-06-24T15:00:41.759803Z","iopub.status.idle":"2025-06-24T15:00:41.764648Z","shell.execute_reply.started":"2025-06-24T15:00:41.75978Z","shell.execute_reply":"2025-06-24T15:00:41.764102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.concat([df1,df2])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:41.765452Z","iopub.execute_input":"2025-06-24T15:00:41.765675Z","iopub.status.idle":"2025-06-24T15:00:42.094928Z","shell.execute_reply.started":"2025-06-24T15:00:41.765659Z","shell.execute_reply":"2025-06-24T15:00:42.094379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample(4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:42.095698Z","iopub.execute_input":"2025-06-24T15:00:42.095941Z","iopub.status.idle":"2025-06-24T15:00:42.158862Z","shell.execute_reply.started":"2025-06-24T15:00:42.095921Z","shell.execute_reply":"2025-06-24T15:00:42.158281Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:42.159505Z","iopub.execute_input":"2025-06-24T15:00:42.159716Z","iopub.status.idle":"2025-06-24T15:00:42.173834Z","shell.execute_reply.started":"2025-06-24T15:00:42.1597Z","shell.execute_reply":"2025-06-24T15:00:42.17328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Policy Start Date'] = pd.to_datetime(df['Policy Start Date'])\n\ndf['Policy_Year'] = df['Policy Start Date'].dt.year\n\ndf['Policy_Month'] = df['Policy Start Date'].dt.month\n\ndf['Policy_Day'] = df['Policy Start Date'].dt.day\n\ndf['Policy_Weekday'] = df['Policy Start Date'].dt.weekday\n\ndf['Policy_Is_Weekend'] = df['Policy_Weekday'].isin([5, 6]).astype(int)\n\ndef get_season(month):\n    if month in [12, 1, 2]:\n        return \"Winter\"  \n    elif month in [3, 4, 5]:\n        return \"Spring\"  \n    elif month in [6, 7, 8]:\n        return \"Summer\"  \n    else:\n        return \"Autumn\"\n\ndf['Policy_Season'] = df['Policy_Month'].apply(get_season)\n\ndf['Policy_Age_Days'] = (pd.to_datetime(\"today\") - df['Policy Start Date']).dt.days","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:42.174493Z","iopub.execute_input":"2025-06-24T15:00:42.174706Z","iopub.status.idle":"2025-06-24T15:00:43.579592Z","shell.execute_reply.started":"2025-06-24T15:00:42.174682Z","shell.execute_reply":"2025-06-24T15:00:43.578987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.drop(\"Policy Start Date\", axis=1, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:43.582356Z","iopub.execute_input":"2025-06-24T15:00:43.582563Z","iopub.status.idle":"2025-06-24T15:00:43.954038Z","shell.execute_reply.started":"2025-06-24T15:00:43.582546Z","shell.execute_reply":"2025-06-24T15:00:43.953498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe().round(3).T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:43.955089Z","iopub.execute_input":"2025-06-24T15:00:43.955353Z","iopub.status.idle":"2025-06-24T15:00:45.2466Z","shell.execute_reply.started":"2025-06-24T15:00:43.955337Z","shell.execute_reply":"2025-06-24T15:00:45.245967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_columns = df.select_dtypes(include=['object']).columns\nnumerical_columns = df.select_dtypes(exclude=['object']).columns\n\nprint(\"\\nCategorical Columns:\", categorical_columns.tolist())\nprint(\"\\nNumerical Columns:\", numerical_columns.tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:45.247277Z","iopub.execute_input":"2025-06-24T15:00:45.247519Z","iopub.status.idle":"2025-06-24T15:00:46.288053Z","shell.execute_reply.started":"2025-06-24T15:00:45.2475Z","shell.execute_reply":"2025-06-24T15:00:46.287478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column in categorical_columns:\n    num_unique = df[column].nunique()\n    print(f\"'{column}' has {num_unique} unique categories.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:46.288802Z","iopub.execute_input":"2025-06-24T15:00:46.289078Z","iopub.status.idle":"2025-06-24T15:00:47.164141Z","shell.execute_reply.started":"2025-06-24T15:00:46.289053Z","shell.execute_reply":"2025-06-24T15:00:47.163525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:47.164847Z","iopub.execute_input":"2025-06-24T15:00:47.165035Z","iopub.status.idle":"2025-06-24T15:00:47.232905Z","shell.execute_reply.started":"2025-06-24T15:00:47.165021Z","shell.execute_reply":"2025-06-24T15:00:47.232292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:47.233647Z","iopub.execute_input":"2025-06-24T15:00:47.234417Z","iopub.status.idle":"2025-06-24T15:00:48.209634Z","shell.execute_reply.started":"2025-06-24T15:00:47.234392Z","shell.execute_reply":"2025-06-24T15:00:48.208936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_cols = df.columns[df.isnull().sum() > 0]\nmissing_cols = missing_cols.drop('Premium Amount')\n\nnum_cols = df[missing_cols].select_dtypes(include='number').columns\ncat_cols = df[missing_cols].select_dtypes(include='object').columns\n\ndf[num_cols] = SimpleImputer(strategy='median').fit_transform(df[num_cols])\ndf[cat_cols] = SimpleImputer(strategy='constant', fill_value='Unknown').fit_transform(df[cat_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:48.210449Z","iopub.execute_input":"2025-06-24T15:00:48.210747Z","iopub.status.idle":"2025-06-24T15:00:52.2388Z","shell.execute_reply.started":"2025-06-24T15:00:48.210724Z","shell.execute_reply":"2025-06-24T15:00:52.238267Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"education_order = {\n    \"High School\": 0,\n    \"Bachelor's\": 1,\n    \"Master's\": 2,\n    \"PhD\": 3\n}\ndf[\"Education Level\"] = df[\"Education Level\"].map(education_order)\n\nexercise_order = {\n    \"Rarely\": 0,\n    \"Monthly\": 1,\n    \"Weekly\": 2,\n    \"Daily\": 3\n}\ndf[\"Exercise Frequency\"] = df[\"Exercise Frequency\"].map(exercise_order)\n\npolicy_type_order = {\n    \"Basic\": 0,\n    \"Comprehensive\": 1,\n    \"Premium\": 2\n}\ndf[\"Policy Type\"] = df[\"Policy Type\"].map(policy_type_order)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:52.239545Z","iopub.execute_input":"2025-06-24T15:00:52.239761Z","iopub.status.idle":"2025-06-24T15:00:52.542481Z","shell.execute_reply.started":"2025-06-24T15:00:52.239746Z","shell.execute_reply":"2025-06-24T15:00:52.54174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.get_dummies(df, drop_first=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:52.543328Z","iopub.execute_input":"2025-06-24T15:00:52.543682Z","iopub.status.idle":"2025-06-24T15:00:54.371226Z","shell.execute_reply.started":"2025-06-24T15:00:52.54365Z","shell.execute_reply":"2025-06-24T15:00:54.370695Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Modelling","metadata":{}},{"cell_type":"code","source":"train=df[:1200000]\ntest=df[1200000:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:54.371924Z","iopub.execute_input":"2025-06-24T15:00:54.37213Z","iopub.status.idle":"2025-06-24T15:00:54.375845Z","shell.execute_reply.started":"2025-06-24T15:00:54.372114Z","shell.execute_reply":"2025-06-24T15:00:54.375299Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = train.drop([\"id\",\"Premium Amount\"], axis=1)\ny = np.log1p(train[\"Premium Amount\"])\ntest = test.drop([\"id\",\"Premium Amount\"], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:54.376615Z","iopub.execute_input":"2025-06-24T15:00:54.377004Z","iopub.status.idle":"2025-06-24T15:00:54.471102Z","shell.execute_reply.started":"2025-06-24T15:00:54.376978Z","shell.execute_reply":"2025-06-24T15:00:54.470583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler = StandardScaler()\nx = scaler.fit_transform(x)\ntest = scaler.transform(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:54.471819Z","iopub.execute_input":"2025-06-24T15:00:54.472021Z","iopub.status.idle":"2025-06-24T15:00:57.480922Z","shell.execute_reply.started":"2025-06-24T15:00:54.472006Z","shell.execute_reply":"2025-06-24T15:00:57.480333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.10, random_state=8)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:57.481924Z","iopub.execute_input":"2025-06-24T15:00:57.482173Z","iopub.status.idle":"2025-06-24T15:00:58.08312Z","shell.execute_reply.started":"2025-06-24T15:00:57.482153Z","shell.execute_reply":"2025-06-24T15:00:58.082576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential([\n    Dense(512, activation='relu', input_shape=(x_train.shape[1],)),\n    Dense(256, activation='relu'),\n    Dense(128, activation='relu'),\n    Dense(64, activation='relu'),\n    Dense(1)\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:00:58.083837Z","iopub.execute_input":"2025-06-24T15:00:58.084062Z","iopub.status.idle":"2025-06-24T15:01:00.372867Z","shell.execute_reply.started":"2025-06-24T15:00:58.084042Z","shell.execute_reply":"2025-06-24T15:01:00.372344Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer=Adam(learning_rate=0.001),\n    loss='mean_absolute_error',\n    metrics=['mean_absolute_error']\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:01:00.373806Z","iopub.execute_input":"2025-06-24T15:01:00.374019Z","iopub.status.idle":"2025-06-24T15:01:00.385592Z","shell.execute_reply.started":"2025-06-24T15:01:00.374003Z","shell.execute_reply":"2025-06-24T15:01:00.385047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\n\nhistory = model.fit(\n    x_train, y_train,\n    validation_split=0.1,\n    epochs=20,\n    batch_size=512,  \n    callbacks=[early_stop],\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:01:00.386322Z","iopub.execute_input":"2025-06-24T15:01:00.386544Z","iopub.status.idle":"2025-06-24T15:02:33.993444Z","shell.execute_reply.started":"2025-06-24T15:01:00.38652Z","shell.execute_reply":"2025-06-24T15:02:33.992628Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"early_stop = EarlyStopping(monitor='loss', patience=5, restore_best_weights=True)\nmodel.fit(x, y, epochs=20, batch_size=512, callbacks=[early_stop], verbose=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:02:33.994567Z","iopub.execute_input":"2025-06-24T15:02:33.995139Z","iopub.status.idle":"2025-06-24T15:04:13.811997Z","shell.execute_reply.started":"2025-06-24T15:02:33.99512Z","shell.execute_reply":"2025-06-24T15:04:13.811204Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction_log = model.predict(test)\nprediction = np.expm1(prediction_log)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:04:13.812866Z","iopub.execute_input":"2025-06-24T15:04:13.813075Z","iopub.status.idle":"2025-06-24T15:04:57.594053Z","shell.execute_reply.started":"2025-06-24T15:04:13.813061Z","shell.execute_reply":"2025-06-24T15:04:57.593453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:04:57.597253Z","iopub.execute_input":"2025-06-24T15:04:57.597482Z","iopub.status.idle":"2025-06-24T15:04:57.60298Z","shell.execute_reply.started":"2025-06-24T15:04:57.597466Z","shell.execute_reply":"2025-06-24T15:04:57.602107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"id\": df2[\"id\"].values,\n    \"Premium Amount\": prediction.flatten()\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:04:57.603595Z","iopub.execute_input":"2025-06-24T15:04:57.603767Z","iopub.status.idle":"2025-06-24T15:04:57.619533Z","shell.execute_reply.started":"2025-06-24T15:04:57.603754Z","shell.execute_reply":"2025-06-24T15:04:57.618943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:04:57.620418Z","iopub.execute_input":"2025-06-24T15:04:57.620665Z","iopub.status.idle":"2025-06-24T15:04:57.634004Z","shell.execute_reply.started":"2025-06-24T15:04:57.620642Z","shell.execute_reply":"2025-06-24T15:04:57.633515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T15:04:57.634601Z","iopub.execute_input":"2025-06-24T15:04:57.634833Z","iopub.status.idle":"2025-06-24T15:04:58.660989Z","shell.execute_reply.started":"2025-06-24T15:04:57.634803Z","shell.execute_reply":"2025-06-24T15:04:58.660459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}