{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">Insurance Price Prediction</p>","metadata":{}},{"cell_type":"markdown","source":"![](https://online.maryville.edu/wp-content/uploads/sites/97/2020/10/MVU-MSDSCI-2020-Q1-Skyscraper-Predictive-Analytics-in-Insurance-Types-Tools-and-the-Future-header-v1.jpg)","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport optuna\nimport math\nimport xgboost as xgb\nimport catboost as cb\nimport lightgbm as lgb\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.ensemble import VotingRegressor\nfrom lightgbm import LGBMRegressor\nfrom sklearn.model_selection import KFold, train_test_split\nfrom sklearn.impute import SimpleImputer\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:33.144374Z","iopub.execute_input":"2024-12-14T07:34:33.144698Z","iopub.status.idle":"2024-12-14T07:34:33.15025Z","shell.execute_reply.started":"2024-12-14T07:34:33.144673Z","shell.execute_reply":"2024-12-14T07:34:33.149234Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🚀 Introduction to the Dataset 🚀</p>","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ntest = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')\nsubmission = pd.read_csv('/kaggle/input/playground-series-s4e12/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:33.175132Z","iopub.execute_input":"2024-12-14T07:34:33.175835Z","iopub.status.idle":"2024-12-14T07:34:38.874945Z","shell.execute_reply.started":"2024-12-14T07:34:33.175806Z","shell.execute_reply":"2024-12-14T07:34:38.874232Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🌟 Overview of Variables and Types</p>","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:38.876279Z","iopub.execute_input":"2024-12-14T07:34:38.876548Z","iopub.status.idle":"2024-12-14T07:34:38.897035Z","shell.execute_reply.started":"2024-12-14T07:34:38.876522Z","shell.execute_reply":"2024-12-14T07:34:38.896256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:38.898112Z","iopub.execute_input":"2024-12-14T07:34:38.898435Z","iopub.status.idle":"2024-12-14T07:34:39.454287Z","shell.execute_reply.started":"2024-12-14T07:34:38.898399Z","shell.execute_reply":"2024-12-14T07:34:39.453361Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:39.455807Z","iopub.execute_input":"2024-12-14T07:34:39.456095Z","iopub.status.idle":"2024-12-14T07:34:40.036165Z","shell.execute_reply.started":"2024-12-14T07:34:39.456068Z","shell.execute_reply":"2024-12-14T07:34:40.035225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Null Values in Train Data:\")\ntrain.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:40.037233Z","iopub.execute_input":"2024-12-14T07:34:40.037503Z","iopub.status.idle":"2024-12-14T07:34:40.570331Z","shell.execute_reply.started":"2024-12-14T07:34:40.037477Z","shell.execute_reply":"2024-12-14T07:34:40.569474Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Duplicated Rows in Train Data:\",train.duplicated().sum())\nprint('-'*30)\nprint(\"Number of Rows:\",train.shape[0])\nprint(\"-\"*30)\nprint(\"Number of Column:\",train.shape[1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:40.571657Z","iopub.execute_input":"2024-12-14T07:34:40.572071Z","iopub.status.idle":"2024-12-14T07:34:41.956063Z","shell.execute_reply.started":"2024-12-14T07:34:40.572017Z","shell.execute_reply":"2024-12-14T07:34:41.955118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_column_names =train.select_dtypes(include=['number']).columns\nprint(\"Numerical Column Names:\", numerical_column_names.tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:41.957372Z","iopub.execute_input":"2024-12-14T07:34:41.957734Z","iopub.status.idle":"2024-12-14T07:34:41.995322Z","shell.execute_reply.started":"2024-12-14T07:34:41.957695Z","shell.execute_reply":"2024-12-14T07:34:41.994511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"object_column_names = train.select_dtypes(include=['object']).columns\nprint(\"Object Column Names:\", object_column_names.tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:41.996485Z","iopub.execute_input":"2024-12-14T07:34:41.996759Z","iopub.status.idle":"2024-12-14T07:34:42.145594Z","shell.execute_reply.started":"2024-12-14T07:34:41.996732Z","shell.execute_reply":"2024-12-14T07:34:42.144575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Unique Value in Train Data:\")\ntrain.nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:42.146714Z","iopub.execute_input":"2024-12-14T07:34:42.147006Z","iopub.status.idle":"2024-12-14T07:34:43.060564Z","shell.execute_reply.started":"2024-12-14T07:34:42.14698Z","shell.execute_reply":"2024-12-14T07:34:43.059651Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_column = ['Age', \n                    'Annual Income', \n                    'Number of Dependents',\n                    'Health Score',\n                    'Previous Claims', \n                    'Vehicle Age',\n                    'Credit Score',\n                    'Insurance Duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:43.063131Z","iopub.execute_input":"2024-12-14T07:34:43.063386Z","iopub.status.idle":"2024-12-14T07:34:43.067511Z","shell.execute_reply.started":"2024-12-14T07:34:43.063361Z","shell.execute_reply":"2024-12-14T07:34:43.066587Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"object_column = ['Marital Status',\n                 'Education Level',\n                 'Occupation',\n                 'Location', \n                 'Policy Type',\n                 'Customer Feedback', \n                 'Exercise Frequency',\n                 'Property Type']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:43.068711Z","iopub.execute_input":"2024-12-14T07:34:43.069175Z","iopub.status.idle":"2024-12-14T07:34:43.081481Z","shell.execute_reply.started":"2024-12-14T07:34:43.069137Z","shell.execute_reply":"2024-12-14T07:34:43.080712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Unique Values in Categorical Data:\")\nfor column in object_column:\n    unique_values = train[column].unique()  \n    print(f\"Unique values in '{column}':\")\n    print(unique_values)\n    print() ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:43.082451Z","iopub.execute_input":"2024-12-14T07:34:43.082733Z","iopub.status.idle":"2024-12-14T07:34:43.492146Z","shell.execute_reply.started":"2024-12-14T07:34:43.082708Z","shell.execute_reply":"2024-12-14T07:34:43.491253Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column in object_column:\n    print(f\"\\nTop value counts in '{column}':\\n{train[column].value_counts()}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:43.493321Z","iopub.execute_input":"2024-12-14T07:34:43.493682Z","iopub.status.idle":"2024-12-14T07:34:44.090264Z","shell.execute_reply.started":"2024-12-14T07:34:43.493642Z","shell.execute_reply":"2024-12-14T07:34:44.089453Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Policy Start Date'] = pd.to_datetime(train['Policy Start Date'])\n\ntrain['Start Year'] = train['Policy Start Date'].dt.year\ntrain['Start Month'] = train['Policy Start Date'].dt.month\ntrain['Start Day'] = train['Policy Start Date'].dt.day\n\ntrain.drop(columns=['Policy Start Date'], inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:44.091186Z","iopub.execute_input":"2024-12-14T07:34:44.091425Z","iopub.status.idle":"2024-12-14T07:34:44.721804Z","shell.execute_reply.started":"2024-12-14T07:34:44.0914Z","shell.execute_reply":"2024-12-14T07:34:44.720974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:44.723202Z","iopub.execute_input":"2024-12-14T07:34:44.72356Z","iopub.status.idle":"2024-12-14T07:34:44.743273Z","shell.execute_reply.started":"2024-12-14T07:34:44.723525Z","shell.execute_reply":"2024-12-14T07:34:44.742292Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🏆 Target Variable: Premium Amount</p>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(6, 5))\nsns.histplot(train['Premium Amount'], kde=True, color='purple', bins=30)\nplt.title('Distribution of Premium Amount')\nplt.xlabel('Premium Amount')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:44.744291Z","iopub.execute_input":"2024-12-14T07:34:44.744516Z","iopub.status.idle":"2024-12-14T07:34:49.595857Z","shell.execute_reply.started":"2024-12-14T07:34:44.744493Z","shell.execute_reply":"2024-12-14T07:34:49.594977Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🚬 Smoking Status: Sizzle Behind the Smoke! </p>","metadata":{}},{"cell_type":"code","source":"smoking_status_counts = train['Smoking Status'].value_counts()\n\nplt.figure(figsize=(5, 5))\nplt.pie(smoking_status_counts, labels=smoking_status_counts.index, \n        autopct='%1.1f%%', startangle=90, \n        colors=['#a6c1e1', '#c0c0c0'])  \nplt.title('Smoking Status Distribution')\nplt.axis('equal')  \nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:49.596833Z","iopub.execute_input":"2024-12-14T07:34:49.597104Z","iopub.status.idle":"2024-12-14T07:34:49.752773Z","shell.execute_reply.started":"2024-12-14T07:34:49.597078Z","shell.execute_reply":"2024-12-14T07:34:49.751712Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🎉✨Gender: The Fabulous Spectrum</p>","metadata":{}},{"cell_type":"code","source":"gender_counts = train['Gender'].value_counts()\n\nplt.figure(figsize=(5, 5))\nplt.pie(gender_counts, labels=gender_counts.index, \n        autopct='%1.1f%%', startangle=90, \n        colors=['#a6c1e1', '#c0c0c0'])  \nplt.title('Gender Distribution')\nplt.axis('equal')  \nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:49.754097Z","iopub.execute_input":"2024-12-14T07:34:49.754546Z","iopub.status.idle":"2024-12-14T07:34:49.987601Z","shell.execute_reply.started":"2024-12-14T07:34:49.754495Z","shell.execute_reply":"2024-12-14T07:34:49.986397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"comparison_data = pd.DataFrame({\n    'Smoking Status': smoking_status_counts,\n    'Gender': gender_counts\n}).fillna(0).T \n\ncomparison_data.plot(kind='bar', figsize=(6, 5), color=['#a6c1e1', '#c0c0c0'])\nplt.title('Gender vs Smoking Status Distribution')\nplt.ylabel('Count')\nplt.xticks(rotation=0)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:49.989204Z","iopub.execute_input":"2024-12-14T07:34:49.989665Z","iopub.status.idle":"2024-12-14T07:34:50.266055Z","shell.execute_reply.started":"2024-12-14T07:34:49.989611Z","shell.execute_reply":"2024-12-14T07:34:50.265187Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🎭 Categorical Variables Distribution</p>","metadata":{}},{"cell_type":"code","source":"rows, cols = 4, 2\n\nfig, axes = plt.subplots(rows, cols, figsize=(15, 23))\naxes = axes.flatten()  \n\nfor i, col in enumerate(object_column):\n    train[col].value_counts().plot(kind='bar', color='skyblue', ax=axes[i])\n    axes[i].set_title(f'{col} Distribution', fontsize=14)\n    axes[i].set_xlabel(col, fontsize=12)\n    axes[i].set_ylabel('Count', fontsize=12)\n    axes[i].tick_params(axis='x', rotation=45)\n\nfor i in range(len(object_column), len(axes)):\n    axes[i].axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:50.267613Z","iopub.execute_input":"2024-12-14T07:34:50.268Z","iopub.status.idle":"2024-12-14T07:34:52.246101Z","shell.execute_reply.started":"2024-12-14T07:34:50.26796Z","shell.execute_reply":"2024-12-14T07:34:52.245241Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🔢 Numerical Variables Distribution</p>","metadata":{}},{"cell_type":"code","source":"rows, cols = 4, 2\n\nfig, axes = plt.subplots(rows, cols, figsize=(15, 23))\naxes = axes.flatten()  \n\nfor i, col in enumerate(numerical_column):\n    axes[i].hist(train[col], bins=20, color='lightblue', edgecolor='black')\n    axes[i].set_title(f'{col} Distribution', fontsize=14)\n    axes[i].set_xlabel(col, fontsize=12)\n    axes[i].set_ylabel('Frequency', fontsize=12)\n\nfor i in range(len(numerical_column), len(axes)):\n    axes[i].axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:52.247168Z","iopub.execute_input":"2024-12-14T07:34:52.247434Z","iopub.status.idle":"2024-12-14T07:34:54.179942Z","shell.execute_reply.started":"2024-12-14T07:34:52.247407Z","shell.execute_reply":"2024-12-14T07:34:54.179127Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🎨 Box Plot of Numerical Variables Distribution 🎉</p>","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(4, 2, figsize=(15, 23))\naxes = axes.flatten() \n\nfor i, column in enumerate(numerical_column):\n    sns.boxplot(data=train, y=column, ax=axes[i], palette=\"Set2\")\n    axes[i].set_title(f'Box Plot of {column}', fontsize=12)\n    axes[i].set_xlabel('')  \n    axes[i].set_ylabel(column)\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:54.181175Z","iopub.execute_input":"2024-12-14T07:34:54.181755Z","iopub.status.idle":"2024-12-14T07:34:55.778895Z","shell.execute_reply.started":"2024-12-14T07:34:54.181716Z","shell.execute_reply":"2024-12-14T07:34:55.778085Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">💼 Policy Types Across Occupations 🚀</p>","metadata":{}},{"cell_type":"code","source":"colors = sns.color_palette('Set2', n_colors=3)\n\nplt.figure(figsize=(5, 5))\nsns.countplot(x='Occupation', hue='Policy Type', data=train, palette=colors)\n\nplt.title('Policy Types Across Occupations', fontsize=14)\nplt.xlabel('Occupation', fontsize=14)\nplt.ylabel('Count', fontsize=14)\n\nplt.xticks(rotation=45)\n\nplt.tight_layout()\n\nplt.legend(title='Policy Type', bbox_to_anchor=(1.05, 1), loc='upper left')\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:55.780219Z","iopub.execute_input":"2024-12-14T07:34:55.780566Z","iopub.status.idle":"2024-12-14T07:34:57.047713Z","shell.execute_reply.started":"2024-12-14T07:34:55.780527Z","shell.execute_reply":"2024-12-14T07:34:57.046947Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">💍💔 Policy Type Distribution by Marital Status 💑🔥</p>","metadata":{}},{"cell_type":"code","source":"policy_counts = train.groupby(['Marital Status', 'Policy Type']).size().unstack()\n\nax = policy_counts.plot(kind='bar', stacked=True, figsize=(5, 5), color=colors)\n\nplt.title('Policy Type Distribution by Marital Status', fontsize=14)\nplt.xlabel('Marital Status', fontsize=12)\nplt.ylabel('Count', fontsize=12)\n\nplt.tight_layout()\n\nplt.legend(title='Policy Type', bbox_to_anchor=(1.05, 1), loc='upper left')\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:57.048712Z","iopub.execute_input":"2024-12-14T07:34:57.049087Z","iopub.status.idle":"2024-12-14T07:34:57.435244Z","shell.execute_reply.started":"2024-12-14T07:34:57.049048Z","shell.execute_reply":"2024-12-14T07:34:57.434395Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">💪🏽🤕 Health vs Claims: The Risky Business! 🤕💪🏽</p>","metadata":{}},{"cell_type":"code","source":"sns.scatterplot(x='Health Score', y='Previous Claims', data=train)\nplt.title('Health Score vs Previous Claims')\nplt.xlabel('Health Score')\nplt.ylabel('Previous Claims')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:57.436229Z","iopub.execute_input":"2024-12-14T07:34:57.436578Z","iopub.status.idle":"2024-12-14T07:34:59.006391Z","shell.execute_reply.started":"2024-12-14T07:34:57.436539Z","shell.execute_reply":"2024-12-14T07:34:59.005505Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">💸Yearly Chronicles: Unveiling Trends One Date at a Time!📅</p>","metadata":{}},{"cell_type":"code","source":"print(\"Insurance start year:\",train['Start Year'].min())\nprint('-'* 50)\nprint(\"Insurance ends year:\",train['Start Year'].max())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:59.007528Z","iopub.execute_input":"2024-12-14T07:34:59.007802Z","iopub.status.idle":"2024-12-14T07:34:59.013301Z","shell.execute_reply.started":"2024-12-14T07:34:59.007776Z","shell.execute_reply":"2024-12-14T07:34:59.012445Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"annual_premium = train.pivot_table(index='Start Year', values='Premium Amount', aggfunc='sum').reset_index()\nannual_premium['Average Premium'] = annual_premium['Premium Amount'].mean()\n\nannual_premium","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:59.014514Z","iopub.execute_input":"2024-12-14T07:34:59.014842Z","iopub.status.idle":"2024-12-14T07:34:59.051438Z","shell.execute_reply.started":"2024-12-14T07:34:59.014806Z","shell.execute_reply":"2024-12-14T07:34:59.050616Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">💸 Avg Premium Amount vs Policy Start Year: Trends Unfold! 📅</p>","metadata":{}},{"cell_type":"code","source":"avg_premium = train.groupby('Start Year')['Premium Amount'].mean().reset_index()\nplt.figure(figsize=(15, 5))\nsns.barplot(x='Start Year', y='Premium Amount', data=avg_premium, palette=\"coolwarm\")\n\nplt.title('Average Premium Amount vs Policy Start Year')\nplt.xlabel('Policy Start Year', fontsize=14)\nplt.ylabel('Average Premium Amount', fontsize=14)\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:59.056122Z","iopub.execute_input":"2024-12-14T07:34:59.056364Z","iopub.status.idle":"2024-12-14T07:34:59.321656Z","shell.execute_reply.started":"2024-12-14T07:34:59.056341Z","shell.execute_reply":"2024-12-14T07:34:59.320786Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">💧 Premium Amount vs Start Day: Splash of Blue! 📅</p>","metadata":{}},{"cell_type":"code","source":"day_premium = train.groupby('Start Day')['Premium Amount'].mean().reset_index()\n\nplt.figure(figsize=(10, 5))\nsns.lineplot(x='Start Day', y='Premium Amount', data=day_premium, marker='o', color='green', markersize=6, linestyle='--')\n\nplt.title('Premium Amount vs Start Day', color='black')\nplt.xlabel('Start Day', color='black')\nplt.ylabel('Average Premium Amount',color='black')\nplt.xticks(ticks=range(1, 32))\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:59.322775Z","iopub.execute_input":"2024-12-14T07:34:59.323064Z","iopub.status.idle":"2024-12-14T07:34:59.581409Z","shell.execute_reply.started":"2024-12-14T07:34:59.323034Z","shell.execute_reply":"2024-12-14T07:34:59.580583Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🎉 Premiums Across the Year: Monthly Thrills!</p>","metadata":{}},{"cell_type":"code","source":"monthly_premium = train.groupby('Start Month')['Premium Amount'].sum().reset_index()\n\nplt.figure(figsize=(12, 5))\nsns.barplot(x='Start Month', y='Premium Amount', data=monthly_premium, palette='coolwarm')\n\nplt.title('Premium Amount by Start Month', color='black', fontsize=16, fontweight='bold')\nplt.xlabel('Start Month', color='black')\nplt.ylabel('Total Premium Amount', color='black')\nplt.xticks(ticks=range(0, 12), labels=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:59.583006Z","iopub.execute_input":"2024-12-14T07:34:59.583519Z","iopub.status.idle":"2024-12-14T07:34:59.876586Z","shell.execute_reply.started":"2024-12-14T07:34:59.58348Z","shell.execute_reply":"2024-12-14T07:34:59.875644Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">📖 Fun-Filled Null Value Handling</p>","metadata":{}},{"cell_type":"code","source":"train['Customer Feedback'] = train['Customer Feedback'].fillna(train['Customer Feedback'].mode()[0])\ntrain['Occupation'] = train['Occupation'].fillna(train['Occupation'].mode()[0])\ntrain['Marital Status'] = train['Marital Status'].fillna(train['Marital Status'].mode()[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:34:59.877779Z","iopub.execute_input":"2024-12-14T07:34:59.878507Z","iopub.status.idle":"2024-12-14T07:35:00.286078Z","shell.execute_reply.started":"2024-12-14T07:34:59.878465Z","shell.execute_reply":"2024-12-14T07:35:00.285115Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features_list = [\n    'Gender',\n    'Marital Status',\n    'Education Level',\n    'Occupation',\n    'Location',\n    'Policy Type',\n    'Customer Feedback',\n    'Exercise Frequency',\n    'Property Type',\n    'Smoking Status'\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:00.287304Z","iopub.execute_input":"2024-12-14T07:35:00.287593Z","iopub.status.idle":"2024-12-14T07:35:00.291839Z","shell.execute_reply.started":"2024-12-14T07:35:00.287566Z","shell.execute_reply":"2024-12-14T07:35:00.29101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_features = ['Age', \n                'Annual Income', \n                'Number of Dependents',\n                'Health Score',\n                'Vehicle Age',\n                'Credit Score',\n                'Previous Claims',\n                'Insurance Duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:00.292989Z","iopub.execute_input":"2024-12-14T07:35:00.293254Z","iopub.status.idle":"2024-12-14T07:35:00.301991Z","shell.execute_reply.started":"2024-12-14T07:35:00.293229Z","shell.execute_reply":"2024-12-14T07:35:00.301175Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoders = {feature: LabelEncoder() for feature in features_list}\n\nfor feature, encoder in encoders.items():\n    train[feature] = encoder.fit_transform(train[feature])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:00.302832Z","iopub.execute_input":"2024-12-14T07:35:00.303151Z","iopub.status.idle":"2024-12-14T07:35:02.204487Z","shell.execute_reply.started":"2024-12-14T07:35:00.303125Z","shell.execute_reply":"2024-12-14T07:35:02.203602Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"imputer = SimpleImputer(strategy='median')\ntrain[num_features] = imputer.fit_transform(train[num_features])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:02.205634Z","iopub.execute_input":"2024-12-14T07:35:02.20599Z","iopub.status.idle":"2024-12-14T07:35:03.361467Z","shell.execute_reply.started":"2024-12-14T07:35:02.205952Z","shell.execute_reply":"2024-12-14T07:35:03.360618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:03.362477Z","iopub.execute_input":"2024-12-14T07:35:03.362754Z","iopub.status.idle":"2024-12-14T07:35:03.38533Z","shell.execute_reply.started":"2024-12-14T07:35:03.362728Z","shell.execute_reply":"2024-12-14T07:35:03.384346Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🔀 Data Split</p>","metadata":{}},{"cell_type":"code","source":"X = train.drop(columns=['Premium Amount'])\ny = train['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:03.386478Z","iopub.execute_input":"2024-12-14T07:35:03.386757Z","iopub.status.idle":"2024-12-14T07:35:03.515949Z","shell.execute_reply.started":"2024-12-14T07:35:03.38673Z","shell.execute_reply":"2024-12-14T07:35:03.514989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:03.517086Z","iopub.execute_input":"2024-12-14T07:35:03.517368Z","iopub.status.idle":"2024-12-14T07:35:03.874487Z","shell.execute_reply.started":"2024-12-14T07:35:03.517342Z","shell.execute_reply":"2024-12-14T07:35:03.873506Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">📌 Train Model </p>","metadata":{}},{"cell_type":"code","source":"best_params = {\n    'objective': 'mae',  \n    'learning_rate': 0.05820157052133642,\n    'n_estimators': 97,\n    'max_depth': 10,\n    'num_leaves': 22,\n    'reg_alpha': 0.025041488939909966,  \n    'reg_lambda': 0.04398938070361757,   \n    'colsample_bytree': 0.513282987217108,  \n    'verbose': -1,\n    'n_jobs': -1,\n    'device': 'gpu'\n}\n\nmodel = lgb.LGBMRegressor(**best_params)\n\nmodel.fit(X, y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:03.875643Z","iopub.execute_input":"2024-12-14T07:35:03.875948Z","iopub.status.idle":"2024-12-14T07:35:08.671412Z","shell.execute_reply.started":"2024-12-14T07:35:03.875906Z","shell.execute_reply":"2024-12-14T07:35:08.67072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Customer Feedback'] = test['Customer Feedback'].fillna(test['Customer Feedback'].mode()[0])\ntest['Occupation'] = test['Occupation'].fillna(test['Occupation'].mode()[0])\ntest['Marital Status'] = test['Marital Status'].fillna(test['Marital Status'].mode()[0])\ntest['Policy Start Date'] = pd.to_datetime(test['Policy Start Date'])\ntest['Start Year'] = test['Policy Start Date'].dt.year\ntest['Start Month'] = test['Policy Start Date'].dt.month\ntest['Start Day'] = test['Policy Start Date'].dt.day\ntest.drop(columns=['Policy Start Date'], inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:08.672215Z","iopub.execute_input":"2024-12-14T07:35:08.672485Z","iopub.status.idle":"2024-12-14T07:35:09.430571Z","shell.execute_reply.started":"2024-12-14T07:35:08.672457Z","shell.execute_reply":"2024-12-14T07:35:09.429569Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for feature, encoder in encoders.items():\n    test[feature] = encoder.transform(test[feature])\n\ntest[num_features] = imputer.transform(test[num_features])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:09.431683Z","iopub.execute_input":"2024-12-14T07:35:09.431973Z","iopub.status.idle":"2024-12-14T07:35:10.595551Z","shell.execute_reply.started":"2024-12-14T07:35:09.43192Z","shell.execute_reply":"2024-12-14T07:35:10.594431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:10.596774Z","iopub.execute_input":"2024-12-14T07:35:10.597068Z","iopub.status.idle":"2024-12-14T07:35:10.619535Z","shell.execute_reply.started":"2024-12-14T07:35:10.59704Z","shell.execute_reply":"2024-12-14T07:35:10.618796Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🚀 Submission Showdown</p>","metadata":{}},{"cell_type":"code","source":"preds = model.predict(test)\npredictions_df = pd.DataFrame(preds, columns=['Premium Amount'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:10.620479Z","iopub.execute_input":"2024-12-14T07:35:10.620718Z","iopub.status.idle":"2024-12-14T07:35:12.295977Z","shell.execute_reply.started":"2024-12-14T07:35:10.620694Z","shell.execute_reply":"2024-12-14T07:35:12.295169Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'id': test['id'],  \n    'Premium Amount': predictions_df['Premium Amount']\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:12.296871Z","iopub.execute_input":"2024-12-14T07:35:12.297174Z","iopub.status.idle":"2024-12-14T07:35:12.305067Z","shell.execute_reply.started":"2024-12-14T07:35:12.297142Z","shell.execute_reply":"2024-12-14T07:35:12.30434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(submission)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:12.30782Z","iopub.execute_input":"2024-12-14T07:35:12.308308Z","iopub.status.idle":"2024-12-14T07:35:12.317245Z","shell.execute_reply.started":"2024-12-14T07:35:12.308273Z","shell.execute_reply":"2024-12-14T07:35:12.316567Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)\nprint(\"File saved...\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:35:12.319531Z","iopub.execute_input":"2024-12-14T07:35:12.31985Z","iopub.status.idle":"2024-12-14T07:35:13.727297Z","shell.execute_reply.started":"2024-12-14T07:35:12.319816Z","shell.execute_reply":"2024-12-14T07:35:13.726369Z"}},"outputs":[],"execution_count":null}]}