{"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"},{"sourceId":9178166,"sourceType":"datasetVersion","datasetId":5547076}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <p style=\"background-color: #4A5568; font-family: 'Courier New', monospace; font-weight: bold; color: #E2E8F0; font-size: 85%; text-align: center; border: 3px solid #A0AEC0; border-radius: 15px; padding: 15px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);\">🌟 📦 Import Packages 🌟</p>\n\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.preprocessing import LabelEncoder\nimport xgboost as xgb\nfrom sklearn.impute import SimpleImputer\nimport optuna","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:06.595397Z","iopub.execute_input":"2024-12-16T16:03:06.596197Z","iopub.status.idle":"2024-12-16T16:03:09.213455Z","shell.execute_reply.started":"2024-12-16T16:03:06.596155Z","shell.execute_reply":"2024-12-16T16:03:09.212503Z"}},"outputs":[],"execution_count":null},{"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')\n#original = pd.read_csv('/kaggle/input/insurance-premium-prediction/Insurance Premium Prediction Dataset.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:09.215064Z","iopub.execute_input":"2024-12-16T16:03:09.215457Z","iopub.status.idle":"2024-12-16T16:03:17.785587Z","shell.execute_reply.started":"2024-12-16T16:03:09.215431Z","shell.execute_reply":"2024-12-16T16:03:17.784816Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #4A5568; font-family: 'Courier New', monospace; font-weight: bold; color: #E2E8F0; font-size: 85%; text-align: center; border: 3px solid #A0AEC0; border-radius: 15px; padding: 15px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);\">🌟 🔍 Data Cleaning & Scaling 🌟</p>","metadata":{}},{"cell_type":"code","source":"#train = pd.concat([train1, original], ignore_index=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:17.786666Z","iopub.execute_input":"2024-12-16T16:03:17.787345Z","iopub.status.idle":"2024-12-16T16:03:17.791232Z","shell.execute_reply.started":"2024-12-16T16:03:17.787301Z","shell.execute_reply":"2024-12-16T16:03:17.790414Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:17.793029Z","iopub.execute_input":"2024-12-16T16:03:17.793286Z","iopub.status.idle":"2024-12-16T16:03:17.832674Z","shell.execute_reply.started":"2024-12-16T16:03:17.793262Z","shell.execute_reply":"2024-12-16T16:03:17.831799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:17.833783Z","iopub.execute_input":"2024-12-16T16:03:17.834146Z","iopub.status.idle":"2024-12-16T16:03:18.390063Z","shell.execute_reply.started":"2024-12-16T16:03:17.834109Z","shell.execute_reply":"2024-12-16T16:03:18.389108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:18.391053Z","iopub.execute_input":"2024-12-16T16:03:18.391327Z","iopub.status.idle":"2024-12-16T16:03:18.970866Z","shell.execute_reply.started":"2024-12-16T16:03:18.391302Z","shell.execute_reply":"2024-12-16T16:03:18.969842Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Duplicated Rows in Train Data:\",train.duplicated().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:18.971824Z","iopub.execute_input":"2024-12-16T16:03:18.972095Z","iopub.status.idle":"2024-12-16T16:03:20.375614Z","shell.execute_reply.started":"2024-12-16T16:03:18.97207Z","shell.execute_reply":"2024-12-16T16:03:20.374725Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Number of Rows:\",train.shape[0])\nprint(\"-\"*50)\nprint(\"Number of Column:\",train.shape[1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:20.377043Z","iopub.execute_input":"2024-12-16T16:03:20.377592Z","iopub.status.idle":"2024-12-16T16:03:20.382595Z","shell.execute_reply.started":"2024-12-16T16:03:20.37755Z","shell.execute_reply":"2024-12-16T16:03:20.38167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:20.38362Z","iopub.execute_input":"2024-12-16T16:03:20.383889Z","iopub.status.idle":"2024-12-16T16:03:20.928948Z","shell.execute_reply.started":"2024-12-16T16:03:20.383863Z","shell.execute_reply":"2024-12-16T16:03:20.928024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in train.columns:\n    print(f\"Unique values in column '{col}':\")\n    print(train[col].unique())\n    print()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:20.931599Z","iopub.execute_input":"2024-12-16T16:03:20.931868Z","iopub.status.idle":"2024-12-16T16:03:21.839822Z","shell.execute_reply.started":"2024-12-16T16:03:20.931842Z","shell.execute_reply":"2024-12-16T16:03:21.838863Z"}},"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-16T16:03:21.841076Z","iopub.execute_input":"2024-12-16T16:03:21.841722Z","iopub.status.idle":"2024-12-16T16:03:22.459123Z","shell.execute_reply.started":"2024-12-16T16:03:21.841681Z","shell.execute_reply":"2024-12-16T16:03:22.458188Z"}},"outputs":[],"execution_count":null},{"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-16T16:03:22.460206Z","iopub.execute_input":"2024-12-16T16:03:22.460483Z","iopub.status.idle":"2024-12-16T16:03:22.886297Z","shell.execute_reply.started":"2024-12-16T16:03:22.460458Z","shell.execute_reply":"2024-12-16T16:03:22.885568Z"}},"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-16T16:03:22.887296Z","iopub.execute_input":"2024-12-16T16:03:22.887566Z","iopub.status.idle":"2024-12-16T16:03:22.891822Z","shell.execute_reply.started":"2024-12-16T16:03:22.887541Z","shell.execute_reply":"2024-12-16T16:03:22.890927Z"}},"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-16T16:03:22.893051Z","iopub.execute_input":"2024-12-16T16:03:22.893393Z","iopub.status.idle":"2024-12-16T16:03:22.902145Z","shell.execute_reply.started":"2024-12-16T16:03:22.893358Z","shell.execute_reply":"2024-12-16T16:03:22.901285Z"}},"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-16T16:03:22.903375Z","iopub.execute_input":"2024-12-16T16:03:22.903989Z","iopub.status.idle":"2024-12-16T16:03:24.828272Z","shell.execute_reply.started":"2024-12-16T16:03:22.903921Z","shell.execute_reply":"2024-12-16T16:03:24.827371Z"}},"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-16T16:03:24.829371Z","iopub.execute_input":"2024-12-16T16:03:24.829608Z","iopub.status.idle":"2024-12-16T16:03:25.974598Z","shell.execute_reply.started":"2024-12-16T16:03:24.829585Z","shell.execute_reply":"2024-12-16T16:03:25.973905Z"}},"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])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:25.975518Z","iopub.execute_input":"2024-12-16T16:03:25.97574Z","iopub.status.idle":"2024-12-16T16:03:26.252643Z","shell.execute_reply.started":"2024-12-16T16:03:25.975718Z","shell.execute_reply":"2024-12-16T16:03:26.251961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:26.25358Z","iopub.execute_input":"2024-12-16T16:03:26.253841Z","iopub.status.idle":"2024-12-16T16:03:26.610562Z","shell.execute_reply.started":"2024-12-16T16:03:26.253815Z","shell.execute_reply":"2024-12-16T16:03:26.609545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.drop(columns=['Policy Start Date'], inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:26.6116Z","iopub.execute_input":"2024-12-16T16:03:26.611853Z","iopub.status.idle":"2024-12-16T16:03:26.772647Z","shell.execute_reply.started":"2024-12-16T16:03:26.611829Z","shell.execute_reply":"2024-12-16T16:03:26.771683Z"}},"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-16T16:03:26.774119Z","iopub.execute_input":"2024-12-16T16:03:26.774488Z","iopub.status.idle":"2024-12-16T16:03:27.929214Z","shell.execute_reply.started":"2024-12-16T16:03:26.774446Z","shell.execute_reply":"2024-12-16T16:03:27.928278Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #4A5568; font-family: 'Courier New', monospace; font-weight: bold; color: #E2E8F0; font-size: 85%; text-align: center; border: 3px solid #A0AEC0; border-radius: 15px; padding: 15px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);\">🌟 ✂️ Train-Test Split 🌟</p>","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:27.930309Z","iopub.execute_input":"2024-12-16T16:03:27.930579Z","iopub.status.idle":"2024-12-16T16:03:27.950688Z","shell.execute_reply.started":"2024-12-16T16:03:27.930554Z","shell.execute_reply":"2024-12-16T16:03:27.949807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(columns='Premium Amount')\ny = train['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:27.951833Z","iopub.execute_input":"2024-12-16T16:03:27.952692Z","iopub.status.idle":"2024-12-16T16:03:28.077688Z","shell.execute_reply.started":"2024-12-16T16:03:27.952653Z","shell.execute_reply":"2024-12-16T16:03:28.077019Z"}},"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-16T16:03:28.078719Z","iopub.execute_input":"2024-12-16T16:03:28.079081Z","iopub.status.idle":"2024-12-16T16:03:28.082968Z","shell.execute_reply.started":"2024-12-16T16:03:28.079045Z","shell.execute_reply":"2024-12-16T16:03:28.082187Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #4A5568; font-family: 'Courier New', monospace; font-weight: bold; color: #E2E8F0; font-size: 85%; text-align: center; border: 3px solid #A0AEC0; border-radius: 15px; padding: 15px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);\">🌟 🤖 Model Training 🌟</p>","metadata":{}},{"cell_type":"code","source":"best_params = {\n    'tree_method': 'gpu_hist',\n    'predictor': 'gpu_predictor',\n    'objective': 'reg:squarederror',\n    'learning_rate': 0.034233185364831505,\n    'max_depth': 9,\n    'subsample': 0.9382984186200222,\n    'colsample_bytree': 0.9982251168881568,\n    'reg_alpha': 0.1350559178504231,\n    'reg_lambda': 0.5535752137125746,\n    'n_estimators': 804\n}\n\nmodel = xgb.XGBRegressor(**best_params)\nmodel.fit(X, y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:03:28.084078Z","iopub.execute_input":"2024-12-16T16:03:28.08433Z","iopub.status.idle":"2024-12-16T16:03:28.093789Z","shell.execute_reply.started":"2024-12-16T16:03:28.084306Z","shell.execute_reply":"2024-12-16T16:03:28.092924Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #4A5568; font-family: 'Courier New', monospace; font-weight: bold; color: #E2E8F0; font-size: 85%; text-align: center; border: 3px solid #A0AEC0; border-radius: 15px; padding: 15px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);\">🌟 📤 Submission Ready! 🌟</p>","metadata":{}},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:06:47.219659Z","iopub.execute_input":"2024-12-16T16:06:47.219919Z","iopub.status.idle":"2024-12-16T16:06:47.241056Z","shell.execute_reply.started":"2024-12-16T16:06:47.219893Z","shell.execute_reply":"2024-12-16T16:06:47.240169Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_pred = model.predict(test)\ntest_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:06:47.242125Z","iopub.execute_input":"2024-12-16T16:06:47.242382Z","iopub.status.idle":"2024-12-16T16:06:47.554964Z","shell.execute_reply.started":"2024-12-16T16:06:47.242358Z","shell.execute_reply":"2024-12-16T16:06:47.553645Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission['Premium Amount'] = test_pred\n\nsubmission = pd.DataFrame({\n    'id': test['id'],  \n    'Premium Amount': test_pred\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:06:47.555809Z","iopub.status.idle":"2024-12-16T16:06:47.556151Z","shell.execute_reply.started":"2024-12-16T16:06:47.555986Z","shell.execute_reply":"2024-12-16T16:06:47.556012Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(submission)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:06:47.557294Z","iopub.status.idle":"2024-12-16T16:06:47.557557Z","shell.execute_reply.started":"2024-12-16T16:06:47.557427Z","shell.execute_reply":"2024-12-16T16:06:47.557441Z"}},"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-16T16:06:47.559088Z","iopub.status.idle":"2024-12-16T16:06:47.559365Z","shell.execute_reply.started":"2024-12-16T16:06:47.559232Z","shell.execute_reply":"2024-12-16T16:06:47.559246Z"}},"outputs":[],"execution_count":null}]}