{"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"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🚀 Import Package 🚀</p>","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom catboost import CatBoostRegressor\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.preprocessing import LabelEncoder\nimport lightgbm as lgb\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-03T13:39:07.725753Z","iopub.execute_input":"2024-12-03T13:39:07.726099Z","iopub.status.idle":"2024-12-03T13:39:07.730847Z","shell.execute_reply.started":"2024-12-03T13:39:07.726069Z","shell.execute_reply":"2024-12-03T13:39:07.730022Z"}},"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')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:07.732169Z","iopub.execute_input":"2024-12-03T13:39:07.732558Z","iopub.status.idle":"2024-12-03T13:39:13.488172Z","shell.execute_reply.started":"2024-12-03T13:39:07.732506Z","shell.execute_reply":"2024-12-03T13:39:13.487503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:13.489805Z","iopub.execute_input":"2024-12-03T13:39:13.490158Z","iopub.status.idle":"2024-12-03T13:39:13.51032Z","shell.execute_reply.started":"2024-12-03T13:39:13.490122Z","shell.execute_reply":"2024-12-03T13:39:13.509448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:13.511472Z","iopub.execute_input":"2024-12-03T13:39:13.511747Z","iopub.status.idle":"2024-12-03T13:39:14.055545Z","shell.execute_reply.started":"2024-12-03T13:39:13.51172Z","shell.execute_reply":"2024-12-03T13:39:14.054671Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:14.05729Z","iopub.execute_input":"2024-12-03T13:39:14.057567Z","iopub.status.idle":"2024-12-03T13:39:14.611583Z","shell.execute_reply.started":"2024-12-03T13:39:14.057542Z","shell.execute_reply":"2024-12-03T13:39:14.610641Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🚀 Data Cleaning 🚀</p>","metadata":{}},{"cell_type":"code","source":"mapping_dict = {\n    'Smoking Status': {'No': 0, 'Yes': 1},\n    'Exercise Frequency': {'Weekly': 1, 'Monthly': 2, 'Daily': 3, 'Rarely': 4},\n    'Property Type': {'House': 1, 'Apartment': 2, 'Condo': 3},\n    'Location': {'Urban': 1, 'Rural': 2, 'Suburban': 3},\n    'Policy Type': {'Premium': 1, 'Comprehensive': 2, 'Basic': 3},\n    'Education Level': {\"Bachelor's\": 1, \"Master's\": 2, 'High School': 3, 'PhD': 4},\n    'Gender': {'Female': 0, 'Male': 1}\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:14.612541Z","iopub.execute_input":"2024-12-03T13:39:14.612787Z","iopub.status.idle":"2024-12-03T13:39:14.617921Z","shell.execute_reply.started":"2024-12-03T13:39:14.612754Z","shell.execute_reply":"2024-12-03T13:39:14.616976Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column, mapping in mapping_dict.items():\n    if column in train.columns:\n        train[column] = train[column].map(mapping)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:14.619091Z","iopub.execute_input":"2024-12-03T13:39:14.619356Z","iopub.status.idle":"2024-12-03T13:39:15.053935Z","shell.execute_reply.started":"2024-12-03T13:39:14.619331Z","shell.execute_reply":"2024-12-03T13:39:15.053262Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column, mapping in mapping_dict.items():\n    if column in test.columns:\n        test[column] = test[column].map(mapping)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:15.055254Z","iopub.execute_input":"2024-12-03T13:39:15.055587Z","iopub.status.idle":"2024-12-03T13:39:15.346925Z","shell.execute_reply.started":"2024-12-03T13:39:15.05555Z","shell.execute_reply":"2024-12-03T13:39:15.34626Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_cols = train.select_dtypes(include=['float64']).columns\nimputer = SimpleImputer(strategy='median')\ntrain[numerical_cols] = imputer.fit_transform(train[numerical_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:15.347965Z","iopub.execute_input":"2024-12-03T13:39:15.348328Z","iopub.status.idle":"2024-12-03T13:39:16.663669Z","shell.execute_reply.started":"2024-12-03T13:39:15.348282Z","shell.execute_reply":"2024-12-03T13:39:16.662952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_cols = test.select_dtypes(include=['float64']).columns\nimputer = SimpleImputer(strategy='median')\ntest[numerical_cols] = imputer.fit_transform(test[numerical_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:16.664877Z","iopub.execute_input":"2024-12-03T13:39:16.665211Z","iopub.status.idle":"2024-12-03T13:39:17.41493Z","shell.execute_reply.started":"2024-12-03T13:39:16.665176Z","shell.execute_reply":"2024-12-03T13:39:17.414241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_cols = train.select_dtypes(include=['object']).columns\nfor col in categorical_cols:\n    train[col] = train[col].fillna(train[col].mode()[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:17.41862Z","iopub.execute_input":"2024-12-03T13:39:17.419269Z","iopub.status.idle":"2024-12-03T13:39:18.410468Z","shell.execute_reply.started":"2024-12-03T13:39:17.419211Z","shell.execute_reply":"2024-12-03T13:39:18.409516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_cols = test.select_dtypes(include=['object']).columns\nfor col in categorical_cols:\n    test[col] = test[col].fillna(test[col].mode()[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:18.411447Z","iopub.execute_input":"2024-12-03T13:39:18.411716Z","iopub.status.idle":"2024-12-03T13:39:19.067467Z","shell.execute_reply.started":"2024-12-03T13:39:18.411691Z","shell.execute_reply":"2024-12-03T13:39:19.066538Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_encoder = LabelEncoder()\n\ncategorical_columns = train.select_dtypes(include=['object']).columns  \n\nfor col in categorical_columns:\n    train[col] = label_encoder.fit_transform(train[col])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:19.068654Z","iopub.execute_input":"2024-12-03T13:39:19.068919Z","iopub.status.idle":"2024-12-03T13:39:20.566372Z","shell.execute_reply.started":"2024-12-03T13:39:19.068894Z","shell.execute_reply":"2024-12-03T13:39:20.565454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"le = LabelEncoder()\n\ncategorical_columns = test.select_dtypes(include=['object']).columns  \n\nfor col in categorical_columns:\n    test[col] = le.fit_transform(test[col])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:20.567647Z","iopub.execute_input":"2024-12-03T13:39:20.568398Z","iopub.status.idle":"2024-12-03T13:39:21.657897Z","shell.execute_reply.started":"2024-12-03T13:39:20.568359Z","shell.execute_reply":"2024-12-03T13:39:21.657183Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🚀 Split Dataset 🚀</p>","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:21.659051Z","iopub.execute_input":"2024-12-03T13:39:21.659646Z","iopub.status.idle":"2024-12-03T13:39:21.681738Z","shell.execute_reply.started":"2024-12-03T13:39:21.65961Z","shell.execute_reply":"2024-12-03T13:39:21.680959Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(columns=['Premium Amount', 'Policy Start Date'])\ny = train['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:21.682714Z","iopub.execute_input":"2024-12-03T13:39:21.682952Z","iopub.status.idle":"2024-12-03T13:39:21.781458Z","shell.execute_reply.started":"2024-12-03T13:39:21.682929Z","shell.execute_reply":"2024-12-03T13:39:21.780523Z"}},"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-03T13:39:21.782682Z","iopub.execute_input":"2024-12-03T13:39:21.783395Z","iopub.status.idle":"2024-12-03T13:39:22.122141Z","shell.execute_reply.started":"2024-12-03T13:39:21.783364Z","shell.execute_reply":"2024-12-03T13:39:22.121109Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🚀 Model Training 🚀</p>","metadata":{}},{"cell_type":"code","source":"model = CatBoostRegressor(\n    iterations=3000,\n    learning_rate=0.05,\n    depth=6,\n    eval_metric=\"RMSE\",\n    random_seed=42,\n    verbose=200,\n    task_type='GPU',\n    l2_leaf_reg=0.7,\n)\nmodel.fit(X, y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:22.123407Z","iopub.execute_input":"2024-12-03T13:39:22.1237Z","iopub.status.idle":"2024-12-03T13:39:46.052874Z","shell.execute_reply.started":"2024-12-03T13:39:22.123673Z","shell.execute_reply":"2024-12-03T13:39:46.052102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#y_pred = model.predict(X_test)\n\n# Calculate the RMSLE\n#rmsle = np.sqrt(mean_squared_log_error(y_test, y_pred))\n\n#print(f'RMSLE: {rmsle}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:46.054147Z","iopub.execute_input":"2024-12-03T13:39:46.054581Z","iopub.status.idle":"2024-12-03T13:39:46.058988Z","shell.execute_reply.started":"2024-12-03T13:39:46.054544Z","shell.execute_reply":"2024-12-03T13:39:46.057988Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #2D3748; font-family: 'Verdana', sans-serif; font-weight: bold; color: #F7FAFC; font-size: 70%; text-align: center; border: 2px solid #CBD5E0; border-radius: 12px; padding: 12px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);\">🚀 Submission 🚀</p>","metadata":{}},{"cell_type":"code","source":"test_pred = model.predict(test)\ntest_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:46.060002Z","iopub.execute_input":"2024-12-03T13:39:46.060277Z","iopub.status.idle":"2024-12-03T13:39:47.2586Z","shell.execute_reply.started":"2024-12-03T13:39:46.060232Z","shell.execute_reply":"2024-12-03T13:39:47.257689Z"}},"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-03T13:39:47.259526Z","iopub.execute_input":"2024-12-03T13:39:47.259757Z","iopub.status.idle":"2024-12-03T13:39:47.267833Z","shell.execute_reply.started":"2024-12-03T13:39:47.259735Z","shell.execute_reply":"2024-12-03T13:39:47.267119Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(submission)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:39:47.268721Z","iopub.execute_input":"2024-12-03T13:39:47.269067Z","iopub.status.idle":"2024-12-03T13:39:47.281079Z","shell.execute_reply.started":"2024-12-03T13:39:47.269023Z","shell.execute_reply":"2024-12-03T13:39:47.280336Z"}},"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-03T13:39:47.281944Z","iopub.execute_input":"2024-12-03T13:39:47.282183Z","iopub.status.idle":"2024-12-03T13:39:48.650819Z","shell.execute_reply.started":"2024-12-03T13:39:47.282159Z","shell.execute_reply":"2024-12-03T13:39:48.649927Z"}},"outputs":[],"execution_count":null}]}