{"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":"none","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# imports\nimport pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:03:51.384819Z","iopub.execute_input":"2024-12-03T02:03:51.385186Z","iopub.status.idle":"2024-12-03T02:03:51.390412Z","shell.execute_reply.started":"2024-12-03T02:03:51.385154Z","shell.execute_reply":"2024-12-03T02:03:51.389261Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#loading Data\ndf_train = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:59:50.822003Z","iopub.execute_input":"2024-12-03T01:59:50.822581Z","iopub.status.idle":"2024-12-03T02:00:01.113394Z","shell.execute_reply.started":"2024-12-03T01:59:50.822535Z","shell.execute_reply":"2024-12-03T02:00:01.112445Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:00:01.114613Z","iopub.execute_input":"2024-12-03T02:00:01.115066Z","iopub.status.idle":"2024-12-03T02:00:01.158472Z","shell.execute_reply.started":"2024-12-03T02:00:01.115024Z","shell.execute_reply":"2024-12-03T02:00:01.157398Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:00:01.160463Z","iopub.execute_input":"2024-12-03T02:00:01.160816Z","iopub.status.idle":"2024-12-03T02:00:01.827454Z","shell.execute_reply.started":"2024-12-03T02:00:01.160783Z","shell.execute_reply":"2024-12-03T02:00:01.825473Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:00:01.829387Z","iopub.execute_input":"2024-12-03T02:00:01.830106Z","iopub.status.idle":"2024-12-03T02:00:02.585895Z","shell.execute_reply.started":"2024-12-03T02:00:01.830045Z","shell.execute_reply":"2024-12-03T02:00:02.584772Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.drop(columns = [\"id\"], inplace= True)\ndf_test.drop(columns = [\"id\"], inplace = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:00:02.587381Z","iopub.execute_input":"2024-12-03T02:00:02.587845Z","iopub.status.idle":"2024-12-03T02:00:02.888237Z","shell.execute_reply.started":"2024-12-03T02:00:02.587796Z","shell.execute_reply":"2024-12-03T02:00:02.886922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:00:02.889355Z","iopub.execute_input":"2024-12-03T02:00:02.889676Z","iopub.status.idle":"2024-12-03T02:00:02.898108Z","shell.execute_reply.started":"2024-12-03T02:00:02.889645Z","shell.execute_reply":"2024-12-03T02:00:02.897036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = ['Age', 'Gender', 'Annual Income', 'Marital Status','Number of Dependents', \n            'Education Level', 'Occupation', 'Health Score',\n            'Location', 'Policy Type', 'Previous Claims', 'Vehicle Age',\n            'Credit Score', 'Insurance Duration', 'Policy Start Date',\n            'Customer Feedback', 'Smoking Status', 'Exercise Frequency',\n            'Property Type']\nnumerical_features = ['Age', 'Annual Income', 'Number of Dependents', \n                      'Health Score', 'Credit Score', 'Insurance Duration', \n                      'Previous Claims', 'Vehicle Age']\ncategorical_features = ['Gender', 'Marital Status', 'Education Level', \n                        'Occupation', 'Location', 'Policy Type', \n                        'Customer Feedback', 'Smoking Status', 'Exercise Frequency', 'Property Type']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:37:20.797882Z","iopub.execute_input":"2024-12-03T02:37:20.798281Z","iopub.status.idle":"2024-12-03T02:37:20.805584Z","shell.execute_reply.started":"2024-12-03T02:37:20.798246Z","shell.execute_reply":"2024-12-03T02:37:20.804303Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Target Variable (Premium Amount)","metadata":{}},{"cell_type":"code","source":"sns.kdeplot(x = \"Premium Amount\", data=df_train, zorder=2, fill=True)\nplt.title(\"Distribution of Premium Amount\")\nplt.grid(zorder=1)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:14:23.523597Z","iopub.execute_input":"2024-12-03T02:14:23.524334Z","iopub.status.idle":"2024-12-03T02:14:28.624145Z","shell.execute_reply.started":"2024-12-03T02:14:23.524296Z","shell.execute_reply":"2024-12-03T02:14:28.623038Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Numerical Features","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(2, 4, figsize = (16, 8))\naxes = axes.flatten()\n\nfor i, feature in enumerate(numerical_features):\n    bin_count = min(df_train[feature].nunique(), 20)\n    sns.histplot(x = feature, data = df_train, ax=axes[i], bins=bin_count, zorder=2)\n    axes[i].set_title(f\"Distribution of {feature}\")\n    axes[i].grid(zorder=1)\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:03:54.60718Z","iopub.execute_input":"2024-12-03T02:03:54.60752Z","iopub.status.idle":"2024-12-03T02:03:59.727638Z","shell.execute_reply.started":"2024-12-03T02:03:54.607486Z","shell.execute_reply":"2024-12-03T02:03:59.726521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, axes = plt.subplots(4, 2, figsize=(16,32))\naxes = axes.flatten()\n\nfor i, feature in enumerate(numerical_features):\n    sns.scatterplot(data=df_train, x=feature, y=\"Premium Amount\", ax=axes[i], zorder=2)\n    axes[i].set_title(f\"Relation Between Premium Account and {feature}\")\n    axes[i].grid(zorder=1)\n    axes[i].set_xlim(df_train[feature].min(), df_train[feature].max())\n    axes[i].set_ylim(df_train[\"Premium Amount\"].min(), df_train[\"Premium Amount\"].max())\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:18:55.375362Z","iopub.execute_input":"2024-12-03T02:18:55.375746Z","iopub.status.idle":"2024-12-03T02:19:15.821002Z","shell.execute_reply.started":"2024-12-03T02:18:55.375711Z","shell.execute_reply":"2024-12-03T02:19:15.81939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train[\"Number of Dependents\"].unique()\nfor i in df_train[\"Number of Dependents\"].unique():\n    print(i)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:22:56.002154Z","iopub.execute_input":"2024-12-03T02:22:56.002523Z","iopub.status.idle":"2024-12-03T02:22:56.038633Z","shell.execute_reply.started":"2024-12-03T02:22:56.00249Z","shell.execute_reply":"2024-12-03T02:22:56.037265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# looking at average premium amount for numerical variables that have small number of categories\nsmall_cat_numerical = [\"Number of Dependents\", \"Insurance Duration\", \"Previous Claims\", \"Vehicle Age\"]\n\nfig, axes = plt.subplots(2, 2, figsize=(16, 16))\naxes = axes.flatten()\n\nfor i, feature in enumerate(small_cat_numerical):\n    average = df_train.groupby(feature)[\"Premium Amount\"].mean()\n    average.plot(kind=\"bar\", ax=axes[i], zorder=2)\n    axes[i].set_title(f\"Relation Between {feature} and Premium Amount\")\n    axes[i].grid(zorder=1)\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:29:51.481794Z","iopub.execute_input":"2024-12-03T02:29:51.482151Z","iopub.status.idle":"2024-12-03T02:29:52.551844Z","shell.execute_reply.started":"2024-12-03T02:29:51.482121Z","shell.execute_reply":"2024-12-03T02:29:52.550621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_numeric = numerical_features + [\"Premium Amount\"]\ncorr_matrix = df_train[all_numeric].corr()\nplt.figure(figsize=(8,8))\nsns.heatmap(corr_matrix, annot=True, cmap=\"coolwarm\", fmt=\".2f\")\nplt.title(\"Correlation for Numerical Variables\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:33:09.101632Z","iopub.execute_input":"2024-12-03T02:33:09.102094Z","iopub.status.idle":"2024-12-03T02:33:10.03671Z","shell.execute_reply.started":"2024-12-03T02:33:09.102057Z","shell.execute_reply":"2024-12-03T02:33:10.035414Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Categorical Features","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(2,5, figsize=(32, 16))\naxes = axes.flatten()\n\nfor i, feature in enumerate(categorical_features):\n    sns.countplot(data=df_train, x=feature, ax=axes[i],zorder=2)\n    axes[i].set_title(f\"Distribution of {feature}\")\n    axes[i].grid(zorder=1)\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:38:16.799284Z","iopub.execute_input":"2024-12-03T02:38:16.79962Z","iopub.status.idle":"2024-12-03T02:38:25.232803Z","shell.execute_reply.started":"2024-12-03T02:38:16.799591Z","shell.execute_reply":"2024-12-03T02:38:25.231306Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# relation between all categorical and dependent variable\nfig, axes = plt.subplots(2, 5, figsize=(16, 8))\naxes = axes.flatten()\n\nfor i, feature in enumerate(categorical_features):\n    sns.boxplot(data=df_train, x=feature, y=\"Premium Amount\", ax=axes[i])\n    axes[i].set_title(f\"Relation Between {feature} and Premium Amount\")\n\nplt.tight_layout()\nplt.show","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:38:52.352492Z","iopub.execute_input":"2024-12-03T02:38:52.352898Z","iopub.status.idle":"2024-12-03T02:39:01.800766Z","shell.execute_reply.started":"2024-12-03T02:38:52.352856Z","shell.execute_reply":"2024-12-03T02:39:01.799655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# relation between all categorical and all numerical features\nfig, axes = plt.subplots(16, 5, figsize=(16, 80))\naxes = axes.flatten()\n\nfor i, num_feature in enumerate(numerical_features):\n    for j, cat_feature in enumerate(categorical_features):\n        ax = axes[i * len(categorical_features) + j]\n        sns.boxplot(data=df_train, x=cat_feature, y=\"Age\", ax=ax)\n        ax.set_title(f\"{cat_feature} and {num_feature}\")\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:40:12.933781Z","iopub.execute_input":"2024-12-03T02:40:12.934292Z","iopub.status.idle":"2024-12-03T02:40:20.414681Z","shell.execute_reply.started":"2024-12-03T02:40:12.934248Z","shell.execute_reply":"2024-12-03T02:40:20.413562Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# relation between all categorical and Health Score\nfig, axes = plt.subplots(2, 5, figsize=(16, 8))\naxes = axes.flatten()\n\nfor i, feature in enumerate(categorical_features):\n    sns.boxplot(data=df_train, x=feature, y=\"Health Score\", ax=axes[i])\n    axes[i].set_title(f\"{feature} and Health\")\n\nplt.tight_layout()\nplt.show","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T02:41:21.552787Z","iopub.execute_input":"2024-12-03T02:41:21.553178Z","iopub.status.idle":"2024-12-03T02:41:28.862951Z","shell.execute_reply.started":"2024-12-03T02:41:21.553145Z","shell.execute_reply":"2024-12-03T02:41:28.86189Z"}},"outputs":[],"execution_count":null}]}