{"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":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-06T10:55:14.576561Z","iopub.execute_input":"2024-12-06T10:55:14.577087Z","iopub.status.idle":"2024-12-06T10:55:16.010848Z","shell.execute_reply.started":"2024-12-06T10:55:14.577035Z","shell.execute_reply":"2024-12-06T10:55:16.009505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nimport pandas as pd\npd.set_option('display.max_columns', None)\nfrom sklearn.preprocessing import LabelEncoder, StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom xgboost.sklearn import XGBRegressor\nfrom sklearn.metrics import mean_absolute_error","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:33:10.985993Z","iopub.execute_input":"2024-12-06T12:33:10.986506Z","iopub.status.idle":"2024-12-06T12:33:11.268498Z","shell.execute_reply.started":"2024-12-06T12:33:10.98645Z","shell.execute_reply":"2024-12-06T12:33:11.267021Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"code","source":"# Train Data\ntrain = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\ntrain","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:06:35.142532Z","iopub.execute_input":"2024-12-06T12:06:35.143132Z","iopub.status.idle":"2024-12-06T12:06:40.492248Z","shell.execute_reply.started":"2024-12-06T12:06:35.143085Z","shell.execute_reply":"2024-12-06T12:06:40.490673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Test Data\ntest = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")\ntest","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T10:57:40.992905Z","iopub.execute_input":"2024-12-06T10:57:40.993417Z","iopub.status.idle":"2024-12-06T10:57:45.160543Z","shell.execute_reply.started":"2024-12-06T10:57:40.993372Z","shell.execute_reply":"2024-12-06T10:57:45.159075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Submission Data\nsubmission = pd.read_csv(\"/kaggle/input/playground-series-s4e12/sample_submission.csv\")\nsubmission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T10:57:56.317017Z","iopub.execute_input":"2024-12-06T10:57:56.317544Z","iopub.status.idle":"2024-12-06T10:57:56.515432Z","shell.execute_reply.started":"2024-12-06T10:57:56.317497Z","shell.execute_reply":"2024-12-06T10:57:56.514047Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T11:19:35.112242Z","iopub.execute_input":"2024-12-06T11:19:35.112922Z","iopub.status.idle":"2024-12-06T11:19:35.832081Z","shell.execute_reply.started":"2024-12-06T11:19:35.112867Z","shell.execute_reply":"2024-12-06T11:19:35.830941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Collect columns which have NaN\ntrain_nan_col = train.columns[train.isnull().any()]\ntrain_nan_col","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:06:49.162221Z","iopub.execute_input":"2024-12-06T12:06:49.162726Z","iopub.status.idle":"2024-12-06T12:06:49.812257Z","shell.execute_reply.started":"2024-12-06T12:06:49.162676Z","shell.execute_reply":"2024-12-06T12:06:49.810773Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### They need to fill NaN with some values.\n### Of them, they are categorical features.\n### It is necessary to change their types to numerical ones.\n### And then, decide how to fill NaN.","metadata":{}},{"cell_type":"code","source":"# Change categorical features to numerical ones\nle = LabelEncoder()\ncat_cols = []\nfor col in train.columns:\n    if train[col].dtypes == \"object\":\n        cat_cols.append(col)\n\nfor col in cat_cols:\n    train[col] = le.fit_transform(train[col])\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:06:53.793282Z","iopub.execute_input":"2024-12-06T12:06:53.793772Z","iopub.status.idle":"2024-12-06T12:06:58.060801Z","shell.execute_reply.started":"2024-12-06T12:06:53.793723Z","shell.execute_reply":"2024-12-06T12:06:58.059696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Histogram of train_nan_cols\nplt.figure(figsize = (20, 15))\nfor i, col in enumerate(train_nan_col, 1):\n    plt.subplot(3, 4, i)\n    sns.histplot(x = train[col])\n    plt.title(f\"Histogram of {col} Data\")\n    plt.plot()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T11:34:01.722939Z","iopub.execute_input":"2024-12-06T11:34:01.723507Z","iopub.status.idle":"2024-12-06T11:34:12.911807Z","shell.execute_reply.started":"2024-12-06T11:34:01.723459Z","shell.execute_reply":"2024-12-06T11:34:12.91Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fill nan of \"Marital Status\" & \"Customer Feedback\" with fewest value\ntrain[\"Marital Status\"].fillna(3, inplace = True)\ntrain[\"Customer Feedback\"].fillna(3, inplace = True)\n\n# Fill nan of others with random sampling\ntrain_nan_cols = ['Age','Annual Income','Number of Dependents','Occupation','Health Score','Previous Claims','Vehicle Age','Credit Score','Insurance Duration']\nfor col in train_nan_cols:\n    nan_indices = train[train[col].isnull()].index\n    if len(nan_indices) > 0:\n        non_nan_values = train[col].dropna().sample(len(nan_indices), replace=True)\n        train.loc[nan_indices, col] = non_nan_values.values\n\ntrain.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:14:36.05498Z","iopub.execute_input":"2024-12-06T12:14:36.055492Z","iopub.status.idle":"2024-12-06T12:14:36.547255Z","shell.execute_reply.started":"2024-12-06T12:14:36.055452Z","shell.execute_reply":"2024-12-06T12:14:36.545824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Show all feature with target\nplt.figure(figsize = (20, 25))\nfor i, col in enumerate(train.columns[1:], 1):\n    plt.subplot(5, 4, i)\n    sns.histplot(x = train[col])\n    plt.title(f\"Histogram of {col} Data\")\n    plt.plot()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T11:57:34.136025Z","iopub.execute_input":"2024-12-06T11:57:34.13648Z","iopub.status.idle":"2024-12-06T11:57:50.210469Z","shell.execute_reply.started":"2024-12-06T11:57:34.136444Z","shell.execute_reply":"2024-12-06T11:57:50.209134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig1 = train.pivot_table(index = \"Marital Status\", columns = \"Number of Dependents\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig1.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:16:37.646082Z","iopub.execute_input":"2024-12-06T12:16:37.646607Z","iopub.status.idle":"2024-12-06T12:16:37.976874Z","shell.execute_reply.started":"2024-12-06T12:16:37.646555Z","shell.execute_reply":"2024-12-06T12:16:37.975689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig2 = train.pivot_table(index = \"Marital Status\", columns = \"Education Level\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig2.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:17:14.031211Z","iopub.execute_input":"2024-12-06T12:17:14.031684Z","iopub.status.idle":"2024-12-06T12:17:14.419224Z","shell.execute_reply.started":"2024-12-06T12:17:14.031639Z","shell.execute_reply":"2024-12-06T12:17:14.417995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig3 = train.pivot_table(index = \"Marital Status\", columns = \"Occupation\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig3.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:17:41.824118Z","iopub.execute_input":"2024-12-06T12:17:41.824604Z","iopub.status.idle":"2024-12-06T12:17:42.152333Z","shell.execute_reply.started":"2024-12-06T12:17:41.824558Z","shell.execute_reply":"2024-12-06T12:17:42.150944Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig4 = train.pivot_table(index = \"Marital Status\", columns = \"Location\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig4.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:19:35.041829Z","iopub.execute_input":"2024-12-06T12:19:35.042366Z","iopub.status.idle":"2024-12-06T12:19:35.368022Z","shell.execute_reply.started":"2024-12-06T12:19:35.042324Z","shell.execute_reply":"2024-12-06T12:19:35.366477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig5 = train.pivot_table(index = \"Marital Status\", columns = \"Policy Type\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig5.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:19:54.552047Z","iopub.execute_input":"2024-12-06T12:19:54.552571Z","iopub.status.idle":"2024-12-06T12:19:54.92798Z","shell.execute_reply.started":"2024-12-06T12:19:54.552517Z","shell.execute_reply":"2024-12-06T12:19:54.926756Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig6 = train.pivot_table(index = \"Marital Status\", columns = \"Previous Claims\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig6.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:20:54.636181Z","iopub.execute_input":"2024-12-06T12:20:54.636753Z","iopub.status.idle":"2024-12-06T12:20:55.125771Z","shell.execute_reply.started":"2024-12-06T12:20:54.636707Z","shell.execute_reply":"2024-12-06T12:20:55.12454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig7 = train.pivot_table(index = \"Marital Status\", columns = \"Vehicle Age\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig7.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:20:56.512652Z","iopub.execute_input":"2024-12-06T12:20:56.513151Z","iopub.status.idle":"2024-12-06T12:20:57.142059Z","shell.execute_reply.started":"2024-12-06T12:20:56.513106Z","shell.execute_reply":"2024-12-06T12:20:57.140819Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig8 = train.pivot_table(index = \"Marital Status\", columns = \"Insurance Duration\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig8.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:22:07.362736Z","iopub.execute_input":"2024-12-06T12:22:07.363244Z","iopub.status.idle":"2024-12-06T12:22:07.736845Z","shell.execute_reply.started":"2024-12-06T12:22:07.363193Z","shell.execute_reply":"2024-12-06T12:22:07.735724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig9 = train.pivot_table(index = \"Marital Status\", columns = \"Customer Feedback\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig9.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:22:10.192153Z","iopub.execute_input":"2024-12-06T12:22:10.192636Z","iopub.status.idle":"2024-12-06T12:22:10.483511Z","shell.execute_reply.started":"2024-12-06T12:22:10.192594Z","shell.execute_reply":"2024-12-06T12:22:10.482026Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig10 = train.pivot_table(index = \"Marital Status\", columns = \"Smoking Status\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig10.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:23:17.870047Z","iopub.execute_input":"2024-12-06T12:23:17.870539Z","iopub.status.idle":"2024-12-06T12:23:18.139024Z","shell.execute_reply.started":"2024-12-06T12:23:17.870495Z","shell.execute_reply":"2024-12-06T12:23:18.13744Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig11 = train.pivot_table(index = \"Marital Status\", columns = \"Exercise Frequency\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig11.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:23:32.42928Z","iopub.execute_input":"2024-12-06T12:23:32.429739Z","iopub.status.idle":"2024-12-06T12:23:32.718569Z","shell.execute_reply.started":"2024-12-06T12:23:32.4297Z","shell.execute_reply":"2024-12-06T12:23:32.717319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig12 = train.pivot_table(index = \"Marital Status\", columns = \"Property Type\", values = \"Premium Amount\").plot.bar(rot = 0)\nfig12.legend(loc = \"best\", bbox_to_anchor = (1,1))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:23:35.22072Z","iopub.execute_input":"2024-12-06T12:23:35.221231Z","iopub.status.idle":"2024-12-06T12:23:35.498617Z","shell.execute_reply.started":"2024-12-06T12:23:35.221188Z","shell.execute_reply":"2024-12-06T12:23:35.497087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Correlation of this dataset\ntrain_corr = train.corr()\nplt.figure(figsize = (12, 12))\nsns.heatmap(train_corr, fmt = \".2f\", annot = True, cmap = \"RdPu\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:27:56.829213Z","iopub.execute_input":"2024-12-06T12:27:56.829657Z","iopub.status.idle":"2024-12-06T12:28:00.394589Z","shell.execute_reply.started":"2024-12-06T12:27:56.829621Z","shell.execute_reply":"2024-12-06T12:28:00.393151Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Prepare ML Model","metadata":{}},{"cell_type":"code","source":"# Split dataset with train & test\nX = train.iloc[:, :-1]\ny = train.iloc[:, -1]\nX_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-06T12:31:01.560735Z","iopub.execute_input":"2024-12-06T12:31:01.562121Z","iopub.status.idle":"2024-12-06T12:31:02.413895Z","shell.execute_reply.started":"2024-12-06T12:31:01.562061Z","shell.execute_reply":"2024-12-06T12:31:02.412542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Standardization of X_train & X_test\nsc = StandardScaler()\nX_train = sc.fit_transform(X_train)\nX_test = sc.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:32:15.386394Z","iopub.execute_input":"2024-12-06T12:32:15.386929Z","iopub.status.idle":"2024-12-06T12:32:15.851157Z","shell.execute_reply.started":"2024-12-06T12:32:15.386887Z","shell.execute_reply":"2024-12-06T12:32:15.849983Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Build ML Model - XGBRegressor","metadata":{}},{"cell_type":"code","source":"train_scores = []\ntest_scores = []\nfor i in range(2, 10):\n    xgbr = XGBRegressor(n_estimators = i*40, max_depth = i, max_features = i*0.2)\n    xgbr.fit(X_train, y_train)\n\n    train_scores.append(xgbr.score(X_train, y_train))\n    test_scores.append(xgbr.score(X_test, y_test))\n\nsns.lineplot(train_scores, marker = \"*\", color = 'b', linestyle = \"-\")\nsns.lineplot(test_scores, marker = \"o\", color = 'r', linestyle = \"-.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T13:06:49.702379Z","iopub.execute_input":"2024-12-06T13:06:49.703015Z","iopub.status.idle":"2024-12-06T13:08:42.871701Z","shell.execute_reply.started":"2024-12-06T13:06:49.702927Z","shell.execute_reply":"2024-12-06T13:08:42.870464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# i = 9\nxgbr = XGBRegressor(n_estimators = 450, max_depth = 9, max_features = 1.35)\nxgbr.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:47:41.190077Z","iopub.execute_input":"2024-12-06T12:47:41.191529Z","iopub.status.idle":"2024-12-06T12:48:10.992324Z","shell.execute_reply.started":"2024-12-06T12:47:41.191469Z","shell.execute_reply":"2024-12-06T12:48:10.991107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prediction of Target(\"Premium Amount\")\nfor col in cat_cols:\n    test[col] = le.fit_transform(test[col])\ny_xgbr = xgbr.predict(test)\ny_xgbr","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:48:26.713232Z","iopub.execute_input":"2024-12-06T12:48:26.713741Z","iopub.status.idle":"2024-12-06T12:48:37.288343Z","shell.execute_reply.started":"2024-12-06T12:48:26.713692Z","shell.execute_reply":"2024-12-06T12:48:37.287021Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Mean Absolute Error btw y_xgbr & submission\nprint(\"MAE Score :\", mean_absolute_error(y_xgbr, submission.iloc[:, 1]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:49:10.090436Z","iopub.execute_input":"2024-12-06T12:49:10.091Z","iopub.status.idle":"2024-12-06T12:49:10.10732Z","shell.execute_reply.started":"2024-12-06T12:49:10.09094Z","shell.execute_reply":"2024-12-06T12:49:10.105459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}