{"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"},{"sourceId":9178166,"sourceType":"datasetVersion","datasetId":5547076}],"dockerImageVersionId":30786,"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-03T00:26:45.496045Z","iopub.execute_input":"2024-12-03T00:26:45.496606Z","iopub.status.idle":"2024-12-03T00:26:46.619017Z","shell.execute_reply.started":"2024-12-03T00:26:45.496556Z","shell.execute_reply":"2024-12-03T00:26:46.617741Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Loading Data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:26:46.621123Z","iopub.execute_input":"2024-12-03T00:26:46.621693Z","iopub.status.idle":"2024-12-03T00:26:53.032829Z","shell.execute_reply.started":"2024-12-03T00:26:46.621645Z","shell.execute_reply":"2024-12-03T00:26:53.031733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:26:53.034126Z","iopub.execute_input":"2024-12-03T00:26:53.034481Z","iopub.status.idle":"2024-12-03T00:26:56.86702Z","shell.execute_reply.started":"2024-12-03T00:26:53.034449Z","shell.execute_reply":"2024-12-03T00:26:56.865947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"original = pd.read_csv(\"/kaggle/input/insurance-premium-prediction/Insurance Premium Prediction Dataset.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:26:56.86878Z","iopub.execute_input":"2024-12-03T00:26:56.869131Z","iopub.status.idle":"2024-12-03T00:26:58.257528Z","shell.execute_reply.started":"2024-12-03T00:26:56.869099Z","shell.execute_reply":"2024-12-03T00:26:58.256413Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"original.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:26:58.258942Z","iopub.execute_input":"2024-12-03T00:26:58.259393Z","iopub.status.idle":"2024-12-03T00:26:58.290675Z","shell.execute_reply.started":"2024-12-03T00:26:58.259322Z","shell.execute_reply":"2024-12-03T00:26:58.289616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:26:58.292016Z","iopub.execute_input":"2024-12-03T00:26:58.292473Z","iopub.status.idle":"2024-12-03T00:26:58.322037Z","shell.execute_reply.started":"2024-12-03T00:26:58.292428Z","shell.execute_reply":"2024-12-03T00:26:58.320963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.drop(columns=['id'], inplace=True)\ntrain = pd.concat([train, original], ignore_index=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:26:58.323582Z","iopub.execute_input":"2024-12-03T00:26:58.324055Z","iopub.status.idle":"2024-12-03T00:26:58.738673Z","shell.execute_reply.started":"2024-12-03T00:26:58.323966Z","shell.execute_reply":"2024-12-03T00:26:58.737696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T16:09:59.789113Z","iopub.execute_input":"2024-12-02T16:09:59.789689Z","iopub.status.idle":"2024-12-02T16:09:59.839092Z","shell.execute_reply.started":"2024-12-02T16:09:59.789621Z","shell.execute_reply":"2024-12-02T16:09:59.837369Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Analysis","metadata":{}},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:27:11.731482Z","iopub.execute_input":"2024-12-03T00:27:11.731832Z","iopub.status.idle":"2024-12-03T00:27:12.520417Z","shell.execute_reply.started":"2024-12-03T00:27:11.731802Z","shell.execute_reply":"2024-12-03T00:27:12.519435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:27:19.082141Z","iopub.execute_input":"2024-12-03T00:27:19.08251Z","iopub.status.idle":"2024-12-03T00:27:19.899609Z","shell.execute_reply.started":"2024-12-03T00:27:19.082477Z","shell.execute_reply":"2024-12-03T00:27:19.898425Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in train.columns:\n    print(train[col].value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T16:12:24.477622Z","iopub.execute_input":"2024-12-02T16:12:24.478003Z","iopub.status.idle":"2024-12-02T16:12:26.924979Z","shell.execute_reply.started":"2024-12-02T16:12:24.477967Z","shell.execute_reply":"2024-12-02T16:12:26.923585Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"train['Policy Start Date']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T16:14:28.136085Z","iopub.execute_input":"2024-12-02T16:14:28.137584Z","iopub.status.idle":"2024-12-02T16:14:28.14862Z","shell.execute_reply.started":"2024-12-02T16:14:28.137526Z","shell.execute_reply":"2024-12-02T16:14:28.146737Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Removing the time from the Policy Start Date","metadata":{}},{"cell_type":"code","source":"train_date = []\n\nfor s in train['Policy Start Date']:\n    train_date.append(s.split()[0])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:27:41.821585Z","iopub.execute_input":"2024-12-03T00:27:41.822105Z","iopub.status.idle":"2024-12-03T00:27:42.426757Z","shell.execute_reply.started":"2024-12-03T00:27:41.822066Z","shell.execute_reply":"2024-12-03T00:27:42.425766Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_date = []\n\nfor s in test['Policy Start Date']:\n    test_date.append(s.split()[0])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:27:45.471261Z","iopub.execute_input":"2024-12-03T00:27:45.47172Z","iopub.status.idle":"2024-12-03T00:27:45.80569Z","shell.execute_reply.started":"2024-12-03T00:27:45.471684Z","shell.execute_reply":"2024-12-03T00:27:45.804809Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_date[0:5]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T16:15:36.741527Z","iopub.execute_input":"2024-12-02T16:15:36.741973Z","iopub.status.idle":"2024-12-02T16:15:36.752024Z","shell.execute_reply.started":"2024-12-02T16:15:36.741933Z","shell.execute_reply":"2024-12-02T16:15:36.749887Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Making a new column for start date","metadata":{}},{"cell_type":"code","source":"train['Start Date'] = train_date\ntest['Start Date'] = test_date","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:29:46.991301Z","iopub.execute_input":"2024-12-03T00:29:46.991729Z","iopub.status.idle":"2024-12-03T00:29:47.102467Z","shell.execute_reply.started":"2024-12-03T00:29:46.991695Z","shell.execute_reply":"2024-12-03T00:29:47.10158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.drop('Policy Start Date',axis=1,inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:29:49.219264Z","iopub.execute_input":"2024-12-03T00:29:49.219711Z","iopub.status.idle":"2024-12-03T00:29:49.532571Z","shell.execute_reply.started":"2024-12-03T00:29:49.219675Z","shell.execute_reply":"2024-12-03T00:29:49.531449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.drop('Policy Start Date',axis=1,inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:29:51.143046Z","iopub.execute_input":"2024-12-03T00:29:51.143995Z","iopub.status.idle":"2024-12-03T00:29:51.296101Z","shell.execute_reply.started":"2024-12-03T00:29:51.143954Z","shell.execute_reply":"2024-12-03T00:29:51.294837Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols = [col for col in train.select_dtypes('object')]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:29:53.897016Z","iopub.execute_input":"2024-12-03T00:29:53.897399Z","iopub.status.idle":"2024-12-03T00:29:54.619659Z","shell.execute_reply.started":"2024-12-03T00:29:53.897362Z","shell.execute_reply":"2024-12-03T00:29:54.6188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols = [col for col in train.select_dtypes('number')]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:29:56.801492Z","iopub.execute_input":"2024-12-03T00:29:56.801918Z","iopub.status.idle":"2024-12-03T00:29:56.91386Z","shell.execute_reply.started":"2024-12-03T00:29:56.801885Z","shell.execute_reply":"2024-12-03T00:29:56.912988Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Imputing categorical columns","metadata":{}},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer\n\nimputer = SimpleImputer(strategy='most_frequent')\n\ntrain[cat_cols] = imputer.fit_transform(train[cat_cols])\ntest[cat_cols] = imputer.transform(test[cat_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:30:01.870217Z","iopub.execute_input":"2024-12-03T00:30:01.870638Z","iopub.status.idle":"2024-12-03T00:30:07.369098Z","shell.execute_reply.started":"2024-12-03T00:30:01.870603Z","shell.execute_reply":"2024-12-03T00:30:07.368044Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:39:03.698287Z","iopub.execute_input":"2024-12-03T00:39:03.698767Z","iopub.status.idle":"2024-12-03T00:39:04.517952Z","shell.execute_reply.started":"2024-12-03T00:39:03.69873Z","shell.execute_reply":"2024-12-03T00:39:04.516761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:39:09.404516Z","iopub.execute_input":"2024-12-03T00:39:09.405031Z","iopub.status.idle":"2024-12-03T00:39:09.412585Z","shell.execute_reply.started":"2024-12-03T00:39:09.404995Z","shell.execute_reply":"2024-12-03T00:39:09.411423Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Filling the null values in numerical columns with mean of the respective column","metadata":{}},{"cell_type":"code","source":"for col in num_cols:\n    curr_mean = train[col].mean() \n    train[col] = train[col].fillna(curr_mean)\n    if col!='Premium Amount':\n        test[col] = test[col].fillna(curr_mean)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:39:16.360531Z","iopub.execute_input":"2024-12-03T00:39:16.360935Z","iopub.status.idle":"2024-12-03T00:39:16.568642Z","shell.execute_reply.started":"2024-12-03T00:39:16.360899Z","shell.execute_reply":"2024-12-03T00:39:16.567631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T16:27:34.279703Z","iopub.execute_input":"2024-12-02T16:27:34.280812Z","iopub.status.idle":"2024-12-02T16:27:35.081062Z","shell.execute_reply.started":"2024-12-02T16:27:34.280759Z","shell.execute_reply":"2024-12-02T16:27:35.079795Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Encoding the categorical column","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:39:33.77007Z","iopub.execute_input":"2024-12-03T00:39:33.770838Z","iopub.status.idle":"2024-12-03T00:39:33.775183Z","shell.execute_reply.started":"2024-12-03T00:39:33.770799Z","shell.execute_reply":"2024-12-03T00:39:33.774152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoder = OrdinalEncoder(handle_unknown='use_encoded_value',unknown_value=-1)\ntrain[cat_cols] = encoder.fit_transform(train[cat_cols])\ntest[cat_cols] = encoder.transform(test[cat_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:40:47.129696Z","iopub.execute_input":"2024-12-03T00:40:47.130111Z","iopub.status.idle":"2024-12-03T00:40:53.415321Z","shell.execute_reply.started":"2024-12-03T00:40:47.130077Z","shell.execute_reply":"2024-12-03T00:40:53.414212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:40:57.644488Z","iopub.execute_input":"2024-12-03T00:40:57.6449Z","iopub.status.idle":"2024-12-03T00:40:57.681916Z","shell.execute_reply.started":"2024-12-03T00:40:57.644867Z","shell.execute_reply":"2024-12-03T00:40:57.68063Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Splitting Data","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import *\nimport xgboost as xgb\nfrom lightgbm import LGBMRegressor\nimport optuna\nimport lightgbm as lgb","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:54:05.532646Z","iopub.execute_input":"2024-12-03T00:54:05.533281Z","iopub.status.idle":"2024-12-03T00:54:05.539395Z","shell.execute_reply.started":"2024-12-03T00:54:05.533249Z","shell.execute_reply":"2024-12-03T00:54:05.53791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(['Premium Amount'],axis=1)\ny = train['Premium Amount']\nx_train,x_cv,y_train,y_cv = train_test_split(X,y,train_size=0.8)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T00:55:26.979257Z","iopub.execute_input":"2024-12-03T00:55:26.979674Z","iopub.status.idle":"2024-12-03T00:55:27.727531Z","shell.execute_reply.started":"2024-12-03T00:55:26.979638Z","shell.execute_reply":"2024-12-03T00:55:27.72656Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Training","metadata":{}},{"cell_type":"code","source":"# # Define Optuna optimization function\n# def objective(trial):\n#     # Define parameter search space\n#     param = {\n#         \"objective\": \"regression\",\n#         \"metric\": \"rmse\",\n#         \"boosting_type\": trial.suggest_categorical(\"boosting_type\", [\"gbdt\", \"dart\"]),\n#         \"num_leaves\": trial.suggest_int(\"num_leaves\", 200, 512),\n#         \"learning_rate\": trial.suggest_loguniform(\"learning_rate\", 1e-4, 1e-1),\n#         \"feature_fraction\": trial.suggest_uniform(\"feature_fraction\", 0.6, 1.0),\n#         \"bagging_fraction\": trial.suggest_uniform(\"bagging_fraction\", 0.6, 1.0),\n#         \"bagging_freq\": trial.suggest_int(\"bagging_freq\", 5, 12),\n#         \"min_data_in_leaf\": trial.suggest_int(\"min_data_in_leaf\", 20, 100),\n#         \"max_depth\": trial.suggest_int(\"max_depth\", -1, 16),  # -1 means no limit\n#         \"lambda_l1\": trial.suggest_loguniform(\"lambda_l1\", 1e-4, 10.0),\n#         \"lambda_l2\": trial.suggest_loguniform(\"lambda_l2\", 1e-4, 10.0),\n#         # \"device_type\": \"gpu\",  # Enable GPU support\n#         \"seed\" : 42\n\n#     }\n\n#     # Create a LightGBM dataset\n#     dtrain = lgb.Dataset(x_train, label=y_train)\n#     dval = lgb.Dataset(x_cv, label=y_cv, reference=dtrain)\n\n#     # Train LightGBM model\n#     model = lgb.train(\n#         param,\n#         dtrain,\n#         valid_sets=[dval],\n#     )\n\n#     # Predict on validation set\n#     y_val_pred = model.predict(x_cv)\n    \n#     # Compute RMSLE using sklearn's root_mean_squared_log_error\n#     rmsle = mean_squared_log_error(y_cv, y_val_pred)\n#     return rmsle\n\n# # Run Optuna study\n# study = optuna.create_study(direction=\"minimize\")\n# study.optimize(objective, n_trials=5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:49:50.597154Z","iopub.execute_input":"2024-12-03T01:49:50.597573Z","iopub.status.idle":"2024-12-03T01:52:15.163744Z","shell.execute_reply.started":"2024-12-03T01:49:50.597537Z","shell.execute_reply":"2024-12-03T01:52:15.162598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# study.best_params","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:53:15.94415Z","iopub.execute_input":"2024-12-03T01:53:15.945216Z","iopub.status.idle":"2024-12-03T01:53:15.952091Z","shell.execute_reply.started":"2024-12-03T01:53:15.945173Z","shell.execute_reply":"2024-12-03T01:53:15.950885Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"best_params = {\n    'boosting_type': 'dart',\n    'num_leaves': 384,\n    'learning_rate': 0.024680120465142227,\n    'feature_fraction': 0.9883068358315126,\n    'bagging_fraction': 0.7201712704805496,\n    'bagging_freq': 7,\n    'min_data_in_leaf': 50,\n    'max_depth': 15,\n    'lambda_l1': 0.0011290211269753322,\n    'lambda_l2': 3.056310541294088,\n    'seed': 42\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:55:59.59713Z","iopub.execute_input":"2024-12-03T01:55:59.597616Z","iopub.status.idle":"2024-12-03T01:55:59.603294Z","shell.execute_reply.started":"2024-12-03T01:55:59.597571Z","shell.execute_reply":"2024-12-03T01:55:59.602207Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = lgb.train(\n    best_params,\n    lgb.Dataset(x_train, label=y_train),\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:56:02.640045Z","iopub.execute_input":"2024-12-03T01:56:02.640467Z","iopub.status.idle":"2024-12-03T01:56:52.350635Z","shell.execute_reply.started":"2024-12-03T01:56:02.640428Z","shell.execute_reply":"2024-12-03T01:56:52.349392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(x_cv)\nprint('Root mean squared log error: ',mean_squared_log_error(y_pred,y_cv))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:56:56.43455Z","iopub.execute_input":"2024-12-03T01:56:56.43498Z","iopub.status.idle":"2024-12-03T01:56:57.375827Z","shell.execute_reply.started":"2024-12-03T01:56:56.434945Z","shell.execute_reply":"2024-12-03T01:56:57.374603Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predictions","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test.drop('id',axis=1))\n\n# Prepare submission file\nsubmission = pd.DataFrame({'id': test['id'], 'Premium Amount': predictions})\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:59:07.645542Z","iopub.execute_input":"2024-12-03T01:59:07.64605Z","iopub.status.idle":"2024-12-03T01:59:11.870507Z","shell.execute_reply.started":"2024-12-03T01:59:07.646004Z","shell.execute_reply":"2024-12-03T01:59:11.869152Z"}},"outputs":[],"execution_count":null}]}