{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30822,"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},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom xgboost import XGBRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T05:26:57.05014Z","iopub.execute_input":"2024-12-24T05:26:57.050406Z","iopub.status.idle":"2024-12-24T05:26:58.6738Z","shell.execute_reply.started":"2024-12-24T05:26:57.050371Z","shell.execute_reply":"2024-12-24T05:26:58.672707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")\ndata.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T05:29:33.860988Z","iopub.execute_input":"2024-12-24T05:29:33.861331Z","iopub.status.idle":"2024-12-24T05:29:44.790091Z","shell.execute_reply.started":"2024-12-24T05:29:33.861305Z","shell.execute_reply":"2024-12-24T05:29:44.789128Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Drop irrelevant columns\ndata_cleaned = data.drop(columns=['id', 'Policy Start Date'])\ntest_data_cleaned = test_data.drop(columns=['id', 'Policy Start Date'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T05:30:13.696672Z","iopub.execute_input":"2024-12-24T05:30:13.697036Z","iopub.status.idle":"2024-12-24T05:30:13.951736Z","shell.execute_reply.started":"2024-12-24T05:30:13.697006Z","shell.execute_reply":"2024-12-24T05:30:13.950754Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Split features and target in training data\nX = data_cleaned.drop(columns=['Premium Amount'])\ny = data_cleaned['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T05:30:39.575927Z","iopub.execute_input":"2024-12-24T05:30:39.576312Z","iopub.status.idle":"2024-12-24T05:30:39.741359Z","shell.execute_reply.started":"2024-12-24T05:30:39.576284Z","shell.execute_reply":"2024-12-24T05:30:39.74021Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Encode categorical variables in training and test data\ncategorical_cols = X.select_dtypes(include=['object']).columns\n\nencoders = {}\nfor col in categorical_cols:\n    encoders[col] = LabelEncoder().fit(X[col])\n    X[col] = encoders[col].transform(X[col])\n    if col in test_data_cleaned.columns:\n        test_data_cleaned[col] = encoders[col].transform(test_data_cleaned[col])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T05:31:31.921573Z","iopub.execute_input":"2024-12-24T05:31:31.921911Z","iopub.status.idle":"2024-12-24T05:31:35.529626Z","shell.execute_reply.started":"2024-12-24T05:31:31.921885Z","shell.execute_reply":"2024-12-24T05:31:35.528804Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Split training data into train and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Train XGBoost model\nxgb_model = XGBRegressor(n_estimators=50, random_state=42, verbosity=0)\nxgb_model.fit(X_train, y_train)\n\n# Predict on validation and test data\nxgb_val_pred = xgb_model.predict(X_val)\nxgb_test_pred = xgb_model.predict(test_data_cleaned)\n\n# Clip predictions to ensure no negatives\nxgb_val_pred = np.clip(xgb_val_pred, a_min=0, a_max=None)\nxgb_test_pred = np.clip(xgb_test_pred, a_min=0, a_max=None)\n\n# Calculate RMSLE for validation set\nfrom sklearn.metrics import mean_squared_log_error\n\ndef rmsle(y_true, y_pred):\n    return np.sqrt(mean_squared_log_error(y_true, y_pred))\n\nxgb_rmsle_val = rmsle(y_val, xgb_val_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T05:33:03.166234Z","iopub.execute_input":"2024-12-24T05:33:03.166584Z","iopub.status.idle":"2024-12-24T05:33:08.414837Z","shell.execute_reply.started":"2024-12-24T05:33:03.166558Z","shell.execute_reply":"2024-12-24T05:33:08.413907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_rmsle_val","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T05:33:31.015787Z","iopub.execute_input":"2024-12-24T05:33:31.016197Z","iopub.status.idle":"2024-12-24T05:33:31.022444Z","shell.execute_reply.started":"2024-12-24T05:33:31.016168Z","shell.execute_reply":"2024-12-24T05:33:31.021509Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'id': test_data['id'],  \n    'Premium Amount': xgb_test_pred \n})\n\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T05:33:55.446278Z","iopub.execute_input":"2024-12-24T05:33:55.446613Z","iopub.status.idle":"2024-12-24T05:33:56.62559Z","shell.execute_reply.started":"2024-12-24T05:33:55.446586Z","shell.execute_reply":"2024-12-24T05:33:56.624804Z"}},"outputs":[],"execution_count":null}]}