{"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":"markdown","source":"## Analysis of Insurance Premiums\n\nThis notebook presents a comprehensive analysis of insurance premiums, examining various factors such as gender, smoking status, location, property type, and the number of dependents. The data provides valuable insights into how these variables impact premium amounts.\n\n### Objectives\n- **Framework to Uncover Trends**: Identify patterns within the dataset to better understand the pricing dynamics in the insurance premiums dataset.\n- **Enhance Pricing Strategies**: Emphasize the need to utilize machine learning algorithms for improving pricing accuracy, improve risk assessment, and support decision-making.\n  ","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:46:17.456372Z","iopub.execute_input":"2024-12-01T13:46:17.456794Z","iopub.status.idle":"2024-12-01T13:46:17.463038Z","shell.execute_reply.started":"2024-12-01T13:46:17.456758Z","shell.execute_reply":"2024-12-01T13:46:17.461566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")\ndf_submission = pd.read_csv(\"/kaggle/input/playground-series-s4e12/sample_submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:46:17.835273Z","iopub.execute_input":"2024-12-01T13:46:17.835777Z","iopub.status.idle":"2024-12-01T13:46:25.856758Z","shell.execute_reply.started":"2024-12-01T13:46:17.835721Z","shell.execute_reply":"2024-12-01T13:46:25.855647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:46:25.85864Z","iopub.execute_input":"2024-12-01T13:46:25.858967Z","iopub.status.idle":"2024-12-01T13:46:26.512152Z","shell.execute_reply.started":"2024-12-01T13:46:25.858935Z","shell.execute_reply":"2024-12-01T13:46:26.5108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:46:26.513351Z","iopub.execute_input":"2024-12-01T13:46:26.51371Z","iopub.status.idle":"2024-12-01T13:46:26.946166Z","shell.execute_reply.started":"2024-12-01T13:46:26.513668Z","shell.execute_reply":"2024-12-01T13:46:26.945141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:46:26.94829Z","iopub.execute_input":"2024-12-01T13:46:26.948607Z","iopub.status.idle":"2024-12-01T13:46:27.744776Z","shell.execute_reply.started":"2024-12-01T13:46:26.948574Z","shell.execute_reply":"2024-12-01T13:46:27.743657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.describe(include=\"all\").T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:46:27.746069Z","iopub.execute_input":"2024-12-01T13:46:27.746399Z","iopub.status.idle":"2024-12-01T13:46:30.400626Z","shell.execute_reply.started":"2024-12-01T13:46:27.746366Z","shell.execute_reply":"2024-12-01T13:46:30.399515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_heatmap_median(df, cat_var1, cat_var2, num_var,aggfun=\"median\",figsize=(12, 6)):\n    pivot_table = df.pivot_table(values=num_var, index=cat_var1, columns=cat_var2, aggfunc=aggfun)\n    plt.figure(figsize=figsize)\n    sns.heatmap(pivot_table, annot=True, fmt=\".1f\", cmap=\"YlGnBu\")\n    plt.title(f'M{aggfun[1:]} {num_var} by {cat_var1} and {cat_var2}')\n    plt.xlabel(cat_var2)\n    plt.ylabel(cat_var1)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:46:31.010372Z","iopub.execute_input":"2024-12-01T13:46:31.010694Z","iopub.status.idle":"2024-12-01T13:46:31.016699Z","shell.execute_reply.started":"2024-12-01T13:46:31.01066Z","shell.execute_reply":"2024-12-01T13:46:31.015699Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_heatmap_median(df_train, 'Location', 'Education Level', 'Premium Amount',\"mean\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:46:34.655206Z","iopub.execute_input":"2024-12-01T13:46:34.656142Z","iopub.status.idle":"2024-12-01T13:46:35.131863Z","shell.execute_reply.started":"2024-12-01T13:46:34.656098Z","shell.execute_reply":"2024-12-01T13:46:35.130811Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- Education level demonstrates differing effects on mean premium amounts across distinct locations.\n- High School education has the highest premiums in urban areas.\n- Bachelor's degrees show the highest premiums in suburban areas.\n- PhDs have the highest premiums in rural regions.","metadata":{}},{"cell_type":"code","source":"plot_heatmap_median(df_train, 'Gender', 'Location', 'Premium Amount',\"median\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:47:10.622715Z","iopub.execute_input":"2024-12-01T13:47:10.623072Z","iopub.status.idle":"2024-12-01T13:47:11.123656Z","shell.execute_reply.started":"2024-12-01T13:47:10.623041Z","shell.execute_reply":"2024-12-01T13:47:11.12254Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- Female policyholders in urban areas have the highest median premium amount, followed by those in suburban areas .\n- Male policyholders in rural areas have the highest median premium amount, followed by those in urban areas.","metadata":{}},{"cell_type":"code","source":"plot_heatmap_median(df_train, 'Gender', 'Education Level', 'Premium Amount',\"median\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:48:01.207147Z","iopub.execute_input":"2024-12-01T13:48:01.207589Z","iopub.status.idle":"2024-12-01T13:48:01.711804Z","shell.execute_reply.started":"2024-12-01T13:48:01.20755Z","shell.execute_reply":"2024-12-01T13:48:01.710826Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- High School Education: Male policyholders tend to have higher median premiums than female policyholders\n- Master's Degree: Female policyholders demonstrate lower median premiums than male policyholders\n- PhD Level: Female policyholders showcase higher median premiums relative to male policyholders","metadata":{}},{"cell_type":"code","source":"plot_heatmap_median(df_train, 'Location', 'Smoking Status', 'Premium Amount',\"median\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:49:33.125354Z","iopub.execute_input":"2024-12-01T13:49:33.125755Z","iopub.status.idle":"2024-12-01T13:49:33.676335Z","shell.execute_reply.started":"2024-12-01T13:49:33.125711Z","shell.execute_reply":"2024-12-01T13:49:33.675245Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- Urban non-smokers tend to have the highest median premium,\n- while rural and suburban areas show less variation in median premiums based on smoking status.","metadata":{}},{"cell_type":"code","source":"plot_heatmap_median(df_train, 'Education Level', 'Smoking Status', 'Premium Amount',\"median\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:49:40.820571Z","iopub.execute_input":"2024-12-01T13:49:40.820992Z","iopub.status.idle":"2024-12-01T13:49:41.321707Z","shell.execute_reply.started":"2024-12-01T13:49:40.820954Z","shell.execute_reply":"2024-12-01T13:49:41.320626Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- Individuals with a High School education level exhibit the largest difference in median premiums based on smoking status, with smokers having a higher median premium compared to non-smokers","metadata":{}},{"cell_type":"code","source":"plot_heatmap_median(df_train, 'Gender', 'Smoking Status', 'Premium Amount',\"median\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:49:57.315055Z","iopub.execute_input":"2024-12-01T13:49:57.315974Z","iopub.status.idle":"2024-12-01T13:49:57.780269Z","shell.execute_reply.started":"2024-12-01T13:49:57.315926Z","shell.execute_reply":"2024-12-01T13:49:57.779108Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- In this dataset, there seems to be a minimal impact of smoking status on median premium amounts for both males and females.","metadata":{}},{"cell_type":"code","source":"plot_heatmap_median(df_train,'Property Type',  'Location', 'Premium Amount',\"mean\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:50:14.032776Z","iopub.execute_input":"2024-12-01T13:50:14.033163Z","iopub.status.idle":"2024-12-01T13:50:14.596818Z","shell.execute_reply.started":"2024-12-01T13:50:14.033129Z","shell.execute_reply":"2024-12-01T13:50:14.595771Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- For apartments, urban areas have the highest mean premium (1106.06), followed by rural (1103.64) and suburban (1102.77) areas.\n- Condos show a similar trend, with urban areas having the highest mean premium (1103.95), followed by suburban (1101.20) and rural (1100.63) areas.\n- In the case of houses, suburban areas have the highest mean premium (1103.41), followed by urban (1101.14) and rural (1100.14) areas.","metadata":{}},{"cell_type":"code","source":"plot_heatmap_median(df_train,'Property Type',  'Gender', 'Premium Amount',\"mean\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:50:31.997846Z","iopub.execute_input":"2024-12-01T13:50:31.998383Z","iopub.status.idle":"2024-12-01T13:50:32.570443Z","shell.execute_reply.started":"2024-12-01T13:50:31.998331Z","shell.execute_reply":"2024-12-01T13:50:32.569321Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- For apartments, males have a higher mean premium (1107.3) compared to females (1101.00).\n- Condos show a reverse trend, with females having a higher mean premium (1104.13) than males (1099.72).\n- In the case of houses, the mean premium for females (1102.07) is slightly higher than that for males (1101.06).","metadata":{}},{"cell_type":"code","source":"plot_heatmap_median(df_train,'Number of Dependents',  'Location', 'Premium Amount',\"median\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:51:41.372683Z","iopub.execute_input":"2024-12-01T13:51:41.373457Z","iopub.status.idle":"2024-12-01T13:51:41.928158Z","shell.execute_reply.started":"2024-12-01T13:51:41.373417Z","shell.execute_reply":"2024-12-01T13:51:41.926807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndef plot_two_numerical_vars(df, var1, var2, n_bins=10, figsize=(12, 6)):\n   \n    fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=figsize)\n\n    # Plot a histogram of var2 on the first subplot\n    sns.histplot(df[var2], bins=n_bins, kde=False, ax=ax1)\n    ax1.set_title(f'Histogram of {var2}')\n    ax1.set_xlabel(var2)\n    ax1.set_ylabel('Frequency')\n\n    hb = ax2.hexbin(df[var1], df[var2], gridsize=25, cmap='Blues')\n    ax2.set_title(f\"Hexbin of {var1} vs {var2}\")\n    ax2.set_xlabel(var1)\n    ax2.set_ylabel(var2)\n    cb = fig.colorbar(hb, ax=ax2)\n    cb.set_label('Count')\n\n    plt.tight_layout()\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:02:06.473076Z","iopub.execute_input":"2024-12-01T14:02:06.474017Z","iopub.status.idle":"2024-12-01T14:02:06.481859Z","shell.execute_reply.started":"2024-12-01T14:02:06.473977Z","shell.execute_reply":"2024-12-01T14:02:06.480605Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_two_numerical_vars(df_train,\"Premium Amount\",\"Annual Income\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:09:02.891584Z","iopub.execute_input":"2024-12-01T14:09:02.891981Z","iopub.status.idle":"2024-12-01T14:09:04.586502Z","shell.execute_reply.started":"2024-12-01T14:09:02.891946Z","shell.execute_reply":"2024-12-01T14:09:04.585282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_two_numerical_vars(df_train,\"Premium Amount\",\"Credit Score\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:52:14.865915Z","iopub.execute_input":"2024-12-01T13:52:14.866367Z","iopub.status.idle":"2024-12-01T13:52:16.453514Z","shell.execute_reply.started":"2024-12-01T13:52:14.866326Z","shell.execute_reply":"2024-12-01T13:52:16.452488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_two_numerical_vars(df_train,\"Premium Amount\",\"Age\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:52:16.455878Z","iopub.execute_input":"2024-12-01T13:52:16.456307Z","iopub.status.idle":"2024-12-01T13:52:18.016237Z","shell.execute_reply.started":"2024-12-01T13:52:16.456261Z","shell.execute_reply":"2024-12-01T13:52:18.014896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_two_numerical_vars(df_train,\"Premium Amount\",\"Vehicle Age\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:52:18.017904Z","iopub.execute_input":"2024-12-01T13:52:18.018355Z","iopub.status.idle":"2024-12-01T13:52:19.620985Z","shell.execute_reply.started":"2024-12-01T13:52:18.018304Z","shell.execute_reply":"2024-12-01T13:52:19.619815Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Overall Insights:\n- Gender plays a significant role in determining median premium amounts, with males generally having higher premiums.\n- Location is a crucial factor in premium calculation, with urban areas often associated with higher median premiums.\n- Property type also influences median premium amounts, with variations observed across different types of properties and locations.\n- These findings highlight the complex interplay of factors such as smoking status, gender, location, and property type in determining insurance premium amounts, emphasizing the need for a nuanced approach in insurance pricing and underwriting.","metadata":{}}]}