{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":197.168553,"end_time":"2025-01-08T17:59:25.384468","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2025-01-08T17:56:08.215915","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"\n# Analysis and Lightgbm Model Project \n","metadata":{"papermill":{"duration":0.013164,"end_time":"2025-01-08T17:56:10.603022","exception":false,"start_time":"2025-01-08T17:56:10.589858","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nimport warnings\nfrom catboost import CatBoostRegressor\nfrom lightgbm import LGBMRegressor\nfrom sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor\nfrom sklearn.exceptions import ConvergenceWarning\nfrom sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet\nfrom sklearn.neighbors import KNeighborsRegressor\nfrom sklearn.svm import SVR\nfrom sklearn.tree import DecisionTreeRegressor\nfrom xgboost import XGBRegressor\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import train_test_split, cross_val_score,GridSearchCV\nfrom sklearn.preprocessing import StandardScaler\n\nwarnings.simplefilter(action='ignore', category=FutureWarning)\nwarnings.simplefilter(\"ignore\", category=ConvergenceWarning)\nwarnings.filterwarnings(\"ignore\")\n\n\npd.set_option('display.max_columns', None)\n#pd.set_option('display.max_rows', None)\npd.set_option('display.width', None)\npd.set_option('display.float_format', lambda x: '%.3f' % x)","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2025-01-18T12:57:06.244065Z","iopub.execute_input":"2025-01-18T12:57:06.24455Z","iopub.status.idle":"2025-01-18T12:57:14.308155Z","shell.execute_reply.started":"2025-01-18T12:57:06.244496Z","shell.execute_reply":"2025-01-18T12:57:14.307055Z"},"papermill":{"duration":5.810038,"end_time":"2025-01-08T17:56:16.425484","exception":false,"start_time":"2025-01-08T17:56:10.615446","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df=pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ntest_df=pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')\nsample_df=pd.read_csv('/kaggle/input/playground-series-s4e12/sample_submission.csv')\n\ndf = pd.concat([train_df, test_df], axis=0, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:14.309745Z","iopub.execute_input":"2025-01-18T12:57:14.310706Z","iopub.status.idle":"2025-01-18T12:57:25.612075Z","shell.execute_reply.started":"2025-01-18T12:57:14.31066Z","shell.execute_reply":"2025-01-18T12:57:25.610864Z"},"papermill":{"duration":11.572516,"end_time":"2025-01-08T17:56:28.007567","exception":false,"start_time":"2025-01-08T17:56:16.435051","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.columns = train_df.columns.str.lower().str.replace(' ', '_')\ntest_df.columns = test_df.columns.str.lower().str.replace(' ', '_')\nsample_df.columns = sample_df.columns.str.lower().str.replace(' ', '_')\n\ndf.columns = df.columns.str.lower().str.replace(' ', '_')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:57:25.614018Z","iopub.execute_input":"2025-01-18T12:57:25.614393Z","iopub.status.idle":"2025-01-18T12:57:25.621751Z","shell.execute_reply.started":"2025-01-18T12:57:25.614362Z","shell.execute_reply":"2025-01-18T12:57:25.620465Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:25.623778Z","iopub.execute_input":"2025-01-18T12:57:25.624435Z","iopub.status.idle":"2025-01-18T12:57:25.653022Z","shell.execute_reply.started":"2025-01-18T12:57:25.624401Z","shell.execute_reply":"2025-01-18T12:57:25.651875Z"},"papermill":{"duration":0.018401,"end_time":"2025-01-08T17:56:28.035474","exception":false,"start_time":"2025-01-08T17:56:28.017073","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:25.654336Z","iopub.execute_input":"2025-01-18T12:57:25.6547Z","iopub.status.idle":"2025-01-18T12:57:26.376346Z","shell.execute_reply.started":"2025-01-18T12:57:25.654659Z","shell.execute_reply":"2025-01-18T12:57:26.375288Z"},"papermill":{"duration":0.639963,"end_time":"2025-01-08T17:56:28.684684","exception":false,"start_time":"2025-01-08T17:56:28.044721","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:26.377431Z","iopub.execute_input":"2025-01-18T12:57:26.377808Z","iopub.status.idle":"2025-01-18T12:57:26.383821Z","shell.execute_reply.started":"2025-01-18T12:57:26.377771Z","shell.execute_reply":"2025-01-18T12:57:26.382833Z"},"papermill":{"duration":0.017125,"end_time":"2025-01-08T17:56:28.711697","exception":false,"start_time":"2025-01-08T17:56:28.694572","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:26.384946Z","iopub.execute_input":"2025-01-18T12:57:26.385224Z","iopub.status.idle":"2025-01-18T12:57:26.426484Z","shell.execute_reply.started":"2025-01-18T12:57:26.385201Z","shell.execute_reply":"2025-01-18T12:57:26.425609Z"},"papermill":{"duration":0.032553,"end_time":"2025-01-08T17:56:28.7538","exception":false,"start_time":"2025-01-08T17:56:28.721247","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['policy_start_date'] = pd.to_datetime(df['policy_start_date'])\n\n\ndf['year'] =df['policy_start_date'].dt.year\ndf['quarter'] = df['policy_start_date'].dt.quarter\ndf['month'] = df['policy_start_date'].dt.month\ndf['day'] = df['policy_start_date'].dt.day\ndf['day_of_week'] = df['policy_start_date'].dt.day_name()\ndf['week_of_year'] = df['policy_start_date'].dt.isocalendar().week\n\ndf['day_sin'] = np.sin(2 * np.pi * df['day'] / 365.0)\ndf['day_cos'] = np.cos(2 * np.pi * df['day'] / 365.0)\ndf['month_sin'] = np.sin(2 * np.pi * df['month'] / 12.0)\ndf['month_cos'] = np.cos(2 * np.pi * df['month'] / 12.0)\ndf['year_sin'] = np.sin(2 * np.pi * df['year'] / 7.0)\ndf['year_cos'] = np.cos(2 * np.pi * df['year'] / 7.0)\ndf['group']=(df['year']-2010)*48+df['month']*4+df['day']//7\n\n\ndf['quarter'] = df['quarter'].astype('str')\ndf['month'] = df['month'].astype('str')\ndf['day_of_week'] = df['day_of_week'].astype('str')\ndf['week_of_year'] = df['week_of_year'].astype('str')\n\n","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:26.428971Z","iopub.execute_input":"2025-01-18T12:57:26.429276Z","iopub.status.idle":"2025-01-18T12:57:30.335559Z","shell.execute_reply.started":"2025-01-18T12:57:26.42925Z","shell.execute_reply":"2025-01-18T12:57:30.334344Z"},"papermill":{"duration":4.723933,"end_time":"2025-01-08T17:56:33.487487","exception":false,"start_time":"2025-01-08T17:56:28.763554","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.drop('policy_start_date', axis=1)","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:30.337236Z","iopub.execute_input":"2025-01-18T12:57:30.337536Z","iopub.status.idle":"2025-01-18T12:57:30.891354Z","shell.execute_reply.started":"2025-01-18T12:57:30.337509Z","shell.execute_reply":"2025-01-18T12:57:30.890201Z"},"papermill":{"duration":0.609926,"end_time":"2025-01-08T17:56:34.10735","exception":false,"start_time":"2025-01-08T17:56:33.497424","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:30.892515Z","iopub.execute_input":"2025-01-18T12:57:30.892885Z","iopub.status.idle":"2025-01-18T12:57:30.916568Z","shell.execute_reply.started":"2025-01-18T12:57:30.892854Z","shell.execute_reply":"2025-01-18T12:57:30.915248Z"},"papermill":{"duration":0.033292,"end_time":"2025-01-08T17:56:34.196033","exception":false,"start_time":"2025-01-08T17:56:34.162741","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def check_df(dataframe):\n    print(\"------------- Shape -------------\")\n    print(dataframe.shape)\n    print(\"------------- Types -------------\")\n    print(dataframe.dtypes)\n    print(\"------------- Head -------------\")\n    print(dataframe.head(3))\n    print(\"------------- Tail -------------\")\n    print(dataframe.tail(3))\n    print(\"------------- NA -------------\")\n    print(dataframe.isnull().sum())\n    \n    \ncheck_df(df)","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:30.917725Z","iopub.execute_input":"2025-01-18T12:57:30.91808Z","iopub.status.idle":"2025-01-18T12:57:32.41117Z","shell.execute_reply.started":"2025-01-18T12:57:30.918051Z","shell.execute_reply":"2025-01-18T12:57:32.40991Z"},"papermill":{"duration":1.342973,"end_time":"2025-01-08T17:56:35.549246","exception":false,"start_time":"2025-01-08T17:56:34.206273","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def grab_col_names(dataframe, cat_th=10, car_th=20):\n    cat_cols = [col for col in dataframe.columns if dataframe[col].dtypes == \"O\"]\n\n    num_but_cat = [col for col in dataframe.columns if dataframe[col].nunique() < cat_th and\n                   dataframe[col].dtypes != \"O\"]\n\n    cat_but_car = [col for col in dataframe.columns if dataframe[col].nunique() > car_th and\n                   dataframe[col].dtypes == \"O\"]\n\n    cat_cols = cat_cols + num_but_cat\n    cat_cols = [col for col in cat_cols if col not in cat_but_car]\n\n    num_cols = [col for col in dataframe.columns if dataframe[col].dtypes != \"O\"]\n    num_cols = [col for col in num_cols if col not in num_but_cat]\n    num_cols = [col for col in num_cols if col not in ['premium_amount', 'id']]\n\n    print(f\"Observations: {dataframe.shape[0]}\")\n    print(f\"Variables: {dataframe.shape[1]}\")\n    print(f'cat_cols: {len(cat_cols)}')\n    print(f'num_cols: {len(num_cols)}')\n    print(f'cat_but_car: {len(cat_but_car)}')\n    print(f'num_but_cat: {len(num_but_cat)}')\n\n\n    return cat_cols, cat_but_car, num_cols\n\ncat_cols, cat_but_car, num_cols = grab_col_names(df)","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:32.412457Z","iopub.execute_input":"2025-01-18T12:57:32.412945Z","iopub.status.idle":"2025-01-18T12:57:36.578837Z","shell.execute_reply.started":"2025-01-18T12:57:32.412882Z","shell.execute_reply":"2025-01-18T12:57:36.57768Z"},"papermill":{"duration":3.864279,"end_time":"2025-01-08T17:56:39.424211","exception":false,"start_time":"2025-01-08T17:56:35.559932","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler = StandardScaler()\ndf[num_cols] = scaler.fit_transform(df[num_cols])\n\ndf['premium_amount'] = np.log1p(df['premium_amount'])","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:36.580002Z","iopub.execute_input":"2025-01-18T12:57:36.580299Z","iopub.status.idle":"2025-01-18T12:57:37.914256Z","shell.execute_reply.started":"2025-01-18T12:57:36.580273Z","shell.execute_reply":"2025-01-18T12:57:37.912868Z"},"papermill":{"duration":1.089513,"end_time":"2025-01-08T17:56:40.524284","exception":false,"start_time":"2025-01-08T17:56:39.434771","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def cat_summary(dataframe,col_name):\n  print(pd.DataFrame({col_name:dataframe[col_name].value_counts(),\n                      'Ratio':100*dataframe[col_name].value_counts()/len(dataframe)}))\n  print(\"----------------------------------\")\n\n\nfor col in cat_cols:\n  cat_summary(df,col)\n\n\ndef cat_summary(dataframe,col_name,plot=False):\n  print(pd.DataFrame({col_name:dataframe[col_name].value_counts(),\n                      'Ratio':100*dataframe[col_name].value_counts()/len(dataframe)}))\n  print(\"----------------------------------\")\n\n  if plot:\n    sns.countplot(data=dataframe,x=dataframe[col_name])\n    plt.show(block=True)\n\n\nfor col in cat_cols:\n  cat_summary(df,col,plot=True)\n\n\nfor col in cat_cols:\n    if df[col].dtypes ==\"bool\":\n        df[col] = df[col].astype(int)\n        cat_summary(df,col,plot=True)\n    else:\n        cat_summary(df,col,plot=True)","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:57:37.91518Z","iopub.execute_input":"2025-01-18T12:57:37.915502Z","iopub.status.idle":"2025-01-18T12:58:47.784561Z","shell.execute_reply.started":"2025-01-18T12:57:37.915473Z","shell.execute_reply":"2025-01-18T12:58:47.783383Z"},"papermill":{"duration":69.870769,"end_time":"2025-01-08T17:57:50.405705","exception":false,"start_time":"2025-01-08T17:56:40.534936","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndef num_summary(dataframe,numerical_col):\n    quantiles = [0.05, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 0.95, 0.99]\n    print(dataframe[numerical_col].describe(quantiles).T)\n    print(\"----------------------------------\")\n\n\nnum_summary(df,\"premium_amount\")\n\nfor col in num_cols:\n    num_summary(df,col)\n\ndef num_summary(dataframe,numerical_col,plot=False):\n    quantiles = [0.05, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 0.95, 0.99]\n    print(dataframe[numerical_col].describe(quantiles).T)\n\n    if plot:\n      sns.histplot(data=dataframe, x=numerical_col)\n      plt.show(block=True)\n\nfor col in num_cols:\n  num_summary(df,col,plot=True)","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:58:47.785726Z","iopub.execute_input":"2025-01-18T12:58:47.786074Z","iopub.status.idle":"2025-01-18T12:59:04.211325Z","shell.execute_reply.started":"2025-01-18T12:58:47.786044Z","shell.execute_reply":"2025-01-18T12:59:04.210195Z"},"papermill":{"duration":16.299747,"end_time":"2025-01-08T17:58:06.739134","exception":false,"start_time":"2025-01-08T17:57:50.439387","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def target_summary_with_cat(dataframe,target,categorical_col):\n  print(pd.DataFrame({'Target_Mean':dataframe.groupby(categorical_col,observed=True)[target].mean()}), end=\"\\n\\n\\n\")\n  print(\"----------------------------------\")\n  \n\nfor col in cat_cols:\n  target_summary_with_cat(df,'premium_amount',col)","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:59:04.212613Z","iopub.execute_input":"2025-01-18T12:59:04.213038Z","iopub.status.idle":"2025-01-18T12:59:06.377112Z","shell.execute_reply.started":"2025-01-18T12:59:04.212997Z","shell.execute_reply":"2025-01-18T12:59:06.376097Z"},"papermill":{"duration":2.064011,"end_time":"2025-01-08T17:58:08.852759","exception":false,"start_time":"2025-01-08T17:58:06.788748","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def target_summary_with_num(dataframe,target,numerical_col):\n  print(dataframe.groupby(target).agg({numerical_col:'mean'}), end=\"\\n\\n\\n\")\n  print(\"----------------------------------\")\n\nfor col in num_cols:\n  target_summary_with_num(df,'premium_amount',col)","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:59:06.377911Z","iopub.execute_input":"2025-01-18T12:59:06.37821Z","iopub.status.idle":"2025-01-18T12:59:06.907452Z","shell.execute_reply.started":"2025-01-18T12:59:06.378185Z","shell.execute_reply":"2025-01-18T12:59:06.906253Z"},"papermill":{"duration":0.544045,"end_time":"2025-01-08T17:58:09.440086","exception":false,"start_time":"2025-01-08T17:58:08.896041","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#fig = px.histogram(df, x='premium_amount', nbins=100, title='Premium Amount Distribution')\n#fig.show()","metadata":{"execution":{"iopub.status.busy":"2025-01-18T12:59:06.908484Z","iopub.execute_input":"2025-01-18T12:59:06.908862Z","iopub.status.idle":"2025-01-18T12:59:06.912828Z","shell.execute_reply.started":"2025-01-18T12:59:06.908826Z","shell.execute_reply":"2025-01-18T12:59:06.911765Z"},"papermill":{"duration":0.367513,"end_time":"2025-01-08T17:58:09.852172","exception":false,"start_time":"2025-01-08T17:58:09.484659","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#df['log_premium_amount'] = np.log1p(df['premium_amount'])\n\n#fig = px.histogram(df, x='log_premium_amount', nbins=50, title='Log Transformed Premium Amount Distribution')\n\n#fig.show()\n","metadata":{"papermill":{"duration":0.319534,"end_time":"2025-01-08T17:58:10.21524","exception":false,"start_time":"2025-01-08T17:58:09.895706","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:06.913946Z","iopub.execute_input":"2025-01-18T12:59:06.914233Z","iopub.status.idle":"2025-01-18T12:59:06.932524Z","shell.execute_reply.started":"2025-01-18T12:59:06.914208Z","shell.execute_reply":"2025-01-18T12:59:06.931529Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"##  Feature Engineering","metadata":{"execution":{"iopub.execute_input":"2025-01-08T17:58:10.3038Z","iopub.status.busy":"2025-01-08T17:58:10.303348Z","iopub.status.idle":"2025-01-08T17:58:10.308186Z","shell.execute_reply":"2025-01-08T17:58:10.306934Z"},"papermill":{"duration":0.051658,"end_time":"2025-01-08T17:58:10.310512","exception":false,"start_time":"2025-01-08T17:58:10.258854","status":"completed"},"tags":[]}},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"papermill":{"duration":1.372288,"end_time":"2025-01-08T17:58:11.727262","exception":false,"start_time":"2025-01-08T17:58:10.354974","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:06.933585Z","iopub.execute_input":"2025-01-18T12:59:06.933882Z","iopub.status.idle":"2025-01-18T12:59:08.409146Z","shell.execute_reply.started":"2025-01-18T12:59:06.933845Z","shell.execute_reply":"2025-01-18T12:59:08.40805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def missing_values_table(dataframe, na_name=False):\n    na_columns = [col for col in dataframe.columns if dataframe[col].isnull().sum() > 0]\n\n    n_miss = dataframe[na_columns].isnull().sum().sort_values(ascending=False)\n\n    ratio = (dataframe[na_columns].isnull().sum() / dataframe.shape[0] * 100).sort_values(ascending=False)\n\n    missing_df = pd.concat([n_miss, np.round(ratio, 2)], axis=1, keys=['n_miss', 'ratio'])\n\n    print(missing_df, end=\"\\n\")\n\n    if na_name:\n        return na_columns\n\nmissing_values_table(df)","metadata":{"papermill":{"duration":2.170756,"end_time":"2025-01-08T17:58:13.942422","exception":false,"start_time":"2025-01-08T17:58:11.771666","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:08.41017Z","iopub.execute_input":"2025-01-18T12:59:08.410441Z","iopub.status.idle":"2025-01-18T12:59:10.799113Z","shell.execute_reply.started":"2025-01-18T12:59:08.410418Z","shell.execute_reply":"2025-01-18T12:59:10.797973Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def quick_missing_imp(data, num_method=\"median\", cat_length=20, target=\"premium_amount\"):\n    variables_with_na = [col for col in data.columns if data[col].isnull().sum() > 0]  \n\n    temp_target = data[target]\n\n    print(\"# BEFORE\")\n    print(data[variables_with_na].isnull().sum(), \"\\n\\n\")  \n\n    data = data.apply(lambda x: x.fillna(x.mode()[0]) if (x.dtype == \"O\" and len(x.unique()) <= cat_length) else x, axis=0)\n\n    if num_method == \"mean\":\n        data = data.apply(lambda x: x.fillna(x.mean()) if x.dtype != \"O\" else x, axis=0)\n    elif num_method == \"median\":\n        data = data.apply(lambda x: x.fillna(x.median()) if x.dtype != \"O\" else x, axis=0)\n\n    data[target] = temp_target\n\n    print(\"# AFTER \\n Imputation method is 'MODE' for categorical variables!\")\n    print(\" Imputation method is '\" + num_method.upper() + \"' for numeric variables! \\n\")\n    print(data[variables_with_na].isnull().sum(), \"\\n\\n\")\n\n    return data\n\n\ndf = quick_missing_imp(df, num_method=\"median\", cat_length=17)","metadata":{"papermill":{"duration":11.989636,"end_time":"2025-01-08T17:58:25.976339","exception":false,"start_time":"2025-01-08T17:58:13.986703","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:10.800319Z","iopub.execute_input":"2025-01-18T12:59:10.800617Z","iopub.status.idle":"2025-01-18T12:59:22.884113Z","shell.execute_reply.started":"2025-01-18T12:59:10.800592Z","shell.execute_reply":"2025-01-18T12:59:22.88272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"papermill":{"duration":1.419096,"end_time":"2025-01-08T17:58:27.440064","exception":false,"start_time":"2025-01-08T17:58:26.020968","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:22.890187Z","iopub.execute_input":"2025-01-18T12:59:22.89052Z","iopub.status.idle":"2025-01-18T12:59:24.338317Z","shell.execute_reply.started":"2025-01-18T12:59:22.890473Z","shell.execute_reply":"2025-01-18T12:59:24.337047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def outlier_thresholds(dataframe, variable, low_quantile=0.10, up_quantile=0.90):\n    quantile_one = dataframe[variable].quantile(low_quantile)\n    quantile_three = dataframe[variable].quantile(up_quantile)\n    interquantile_range = quantile_three - quantile_one\n    up_limit = quantile_three + 1.5 * interquantile_range\n    low_limit = quantile_one - 1.5 * interquantile_range\n    return low_limit, up_limit\n","metadata":{"papermill":{"duration":0.052184,"end_time":"2025-01-08T17:58:27.536505","exception":false,"start_time":"2025-01-08T17:58:27.484321","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:24.340667Z","iopub.execute_input":"2025-01-18T12:59:24.341049Z","iopub.status.idle":"2025-01-18T12:59:24.346247Z","shell.execute_reply.started":"2025-01-18T12:59:24.34102Z","shell.execute_reply":"2025-01-18T12:59:24.345057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def check_outlier(dataframe, col_name):\n    low_limit, up_limit = outlier_thresholds(dataframe, col_name)\n    if dataframe[(dataframe[col_name] > up_limit) | (dataframe[col_name] < low_limit)].any(axis=None):\n        return True\n    else:\n        return False\n\n\nfor col in num_cols:\n    if col != \"premium_amount\":\n      print(col, check_outlier(df, col))","metadata":{"papermill":{"duration":0.959378,"end_time":"2025-01-08T17:58:28.54023","exception":false,"start_time":"2025-01-08T17:58:27.580852","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:24.347314Z","iopub.execute_input":"2025-01-18T12:59:24.347684Z","iopub.status.idle":"2025-01-18T12:59:25.438222Z","shell.execute_reply.started":"2025-01-18T12:59:24.347656Z","shell.execute_reply":"2025-01-18T12:59:25.437075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def replace_with_thresholds(dataframe, variable):\n    low_limit, up_limit = outlier_thresholds(dataframe, variable)\n    dataframe.loc[(dataframe[variable] < low_limit), variable] = low_limit\n    dataframe.loc[(dataframe[variable] > up_limit), variable] = up_limit\n\n\nfor col in num_cols:\n    if col != \"premium_amount\":\n        replace_with_thresholds(df,col)","metadata":{"papermill":{"duration":0.953969,"end_time":"2025-01-08T17:58:29.539281","exception":false,"start_time":"2025-01-08T17:58:28.585312","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:25.439383Z","iopub.execute_input":"2025-01-18T12:59:25.439873Z","iopub.status.idle":"2025-01-18T12:59:26.571376Z","shell.execute_reply.started":"2025-01-18T12:59:25.439836Z","shell.execute_reply":"2025-01-18T12:59:26.570288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in num_cols:\n    if col != \"premium_amount\":\n      print(col, check_outlier(df, col))","metadata":{"papermill":{"duration":0.9454,"end_time":"2025-01-08T17:58:30.529434","exception":false,"start_time":"2025-01-08T17:58:29.584034","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:26.572203Z","iopub.execute_input":"2025-01-18T12:59:26.572533Z","iopub.status.idle":"2025-01-18T12:59:27.638444Z","shell.execute_reply.started":"2025-01-18T12:59:26.572497Z","shell.execute_reply":"2025-01-18T12:59:27.637208Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_encoders = {col: LabelEncoder() for col in cat_cols}\n\n\nfor col in cat_cols:\n    le = label_encoders[col]\n    le.fit(df[col])\n    df[col] = le.transform(df[col])","metadata":{"papermill":{"duration":5.655079,"end_time":"2025-01-08T17:58:36.22921","exception":false,"start_time":"2025-01-08T17:58:30.574131","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:27.639395Z","iopub.execute_input":"2025-01-18T12:59:27.639718Z","iopub.status.idle":"2025-01-18T12:59:34.227211Z","shell.execute_reply.started":"2025-01-18T12:59:27.639692Z","shell.execute_reply":"2025-01-18T12:59:34.225859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"papermill":{"duration":0.067083,"end_time":"2025-01-08T17:58:36.340691","exception":false,"start_time":"2025-01-08T17:58:36.273608","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:34.228248Z","iopub.execute_input":"2025-01-18T12:59:34.228544Z","iopub.status.idle":"2025-01-18T12:59:34.248727Z","shell.execute_reply.started":"2025-01-18T12:59:34.228516Z","shell.execute_reply":"2025-01-18T12:59:34.247655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['week_of_year'] = df['week_of_year'].astype(int)","metadata":{"papermill":{"duration":0.226256,"end_time":"2025-01-08T17:58:36.61195","exception":false,"start_time":"2025-01-08T17:58:36.385694","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:34.250024Z","iopub.execute_input":"2025-01-18T12:59:34.250417Z","iopub.status.idle":"2025-01-18T12:59:34.445101Z","shell.execute_reply.started":"2025-01-18T12:59:34.250375Z","shell.execute_reply":"2025-01-18T12:59:34.444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from lightgbm import LGBMClassifier","metadata":{"papermill":{"duration":0.067958,"end_time":"2025-01-08T17:58:36.7252","exception":false,"start_time":"2025-01-08T17:58:36.657242","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:34.446274Z","iopub.execute_input":"2025-01-18T12:59:34.44675Z","iopub.status.idle":"2025-01-18T12:59:34.45726Z","shell.execute_reply.started":"2025-01-18T12:59:34.446706Z","shell.execute_reply":"2025-01-18T12:59:34.45611Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape, test_df.shape","metadata":{"papermill":{"duration":0.055751,"end_time":"2025-01-08T17:58:36.838659","exception":false,"start_time":"2025-01-08T17:58:36.782908","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:34.458507Z","iopub.execute_input":"2025-01-18T12:59:34.458963Z","iopub.status.idle":"2025-01-18T12:59:34.48191Z","shell.execute_reply.started":"2025-01-18T12:59:34.458889Z","shell.execute_reply":"2025-01-18T12:59:34.48077Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = df[df['premium_amount'].notna()].copy()\ntest_df = df[df['premium_amount'].isna()].copy()","metadata":{"papermill":{"duration":1.011187,"end_time":"2025-01-08T17:58:37.896545","exception":false,"start_time":"2025-01-08T17:58:36.885358","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:34.483335Z","iopub.execute_input":"2025-01-18T12:59:34.483794Z","iopub.status.idle":"2025-01-18T12:59:35.356167Z","shell.execute_reply.started":"2025-01-18T12:59:34.483747Z","shell.execute_reply":"2025-01-18T12:59:35.354913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape, test_df.shape","metadata":{"papermill":{"duration":0.056081,"end_time":"2025-01-08T17:58:37.998941","exception":false,"start_time":"2025-01-08T17:58:37.94286","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:35.357338Z","iopub.execute_input":"2025-01-18T12:59:35.357726Z","iopub.status.idle":"2025-01-18T12:59:35.364052Z","shell.execute_reply.started":"2025-01-18T12:59:35.357689Z","shell.execute_reply":"2025-01-18T12:59:35.362959Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.drop('premium_amount', axis=1, inplace=True)","metadata":{"papermill":{"duration":0.106332,"end_time":"2025-01-08T17:58:38.151032","exception":false,"start_time":"2025-01-08T17:58:38.0447","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:35.365151Z","iopub.execute_input":"2025-01-18T12:59:35.365415Z","iopub.status.idle":"2025-01-18T12:59:35.426489Z","shell.execute_reply.started":"2025-01-18T12:59:35.365392Z","shell.execute_reply":"2025-01-18T12:59:35.425325Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape, test_df.shape","metadata":{"papermill":{"duration":0.055693,"end_time":"2025-01-08T17:58:38.31359","exception":false,"start_time":"2025-01-08T17:58:38.257897","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:35.427559Z","iopub.execute_input":"2025-01-18T12:59:35.427875Z","iopub.status.idle":"2025-01-18T12:59:35.434233Z","shell.execute_reply.started":"2025-01-18T12:59:35.427848Z","shell.execute_reply":"2025-01-18T12:59:35.433025Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_df.drop(['premium_amount'], axis=1)\ny = train_df['premium_amount']","metadata":{"papermill":{"duration":0.155421,"end_time":"2025-01-08T17:58:38.515281","exception":false,"start_time":"2025-01-08T17:58:38.35986","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:35.435417Z","iopub.execute_input":"2025-01-18T12:59:35.4358Z","iopub.status.idle":"2025-01-18T12:59:35.565793Z","shell.execute_reply.started":"2025-01-18T12:59:35.43576Z","shell.execute_reply":"2025-01-18T12:59:35.564737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)","metadata":{"papermill":{"duration":0.558355,"end_time":"2025-01-08T17:58:39.120084","exception":false,"start_time":"2025-01-08T17:58:38.561729","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:35.56703Z","iopub.execute_input":"2025-01-18T12:59:35.567425Z","iopub.status.idle":"2025-01-18T12:59:36.093139Z","shell.execute_reply.started":"2025-01-18T12:59:35.567387Z","shell.execute_reply":"2025-01-18T12:59:36.092239Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import mean_absolute_percentage_error","metadata":{"papermill":{"duration":0.052184,"end_time":"2025-01-08T17:58:39.218957","exception":false,"start_time":"2025-01-08T17:58:39.166773","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:36.093849Z","iopub.execute_input":"2025-01-18T12:59:36.094121Z","iopub.status.idle":"2025-01-18T12:59:36.098634Z","shell.execute_reply.started":"2025-01-18T12:59:36.094098Z","shell.execute_reply":"2025-01-18T12:59:36.097607Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndef mape(y_true, y_pred):\n    return mean_absolute_percentage_error(y_true, y_pred)","metadata":{"papermill":{"duration":0.053162,"end_time":"2025-01-08T17:58:39.317763","exception":false,"start_time":"2025-01-08T17:58:39.264601","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:36.099741Z","iopub.execute_input":"2025-01-18T12:59:36.100096Z","iopub.status.idle":"2025-01-18T12:59:36.119997Z","shell.execute_reply.started":"2025-01-18T12:59:36.100059Z","shell.execute_reply":"2025-01-18T12:59:36.118597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nlgbm_params = {\n    'num_leaves': 71,\n    'learning_rate': 0.05412467152424433,\n    'n_estimators': 595,\n    'max_depth': 12,\n    'min_data_in_leaf': 97,\n    'bagging_fraction': 0.5200288825838669,\n    'feature_fraction': 0.9881738491942492,\n    'n_jobs': -1,\n    'verbose': -1\n}","metadata":{"papermill":{"duration":0.052274,"end_time":"2025-01-08T17:58:39.415404","exception":false,"start_time":"2025-01-08T17:58:39.36313","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:36.121053Z","iopub.execute_input":"2025-01-18T12:59:36.121347Z","iopub.status.idle":"2025-01-18T12:59:36.139698Z","shell.execute_reply.started":"2025-01-18T12:59:36.121324Z","shell.execute_reply":"2025-01-18T12:59:36.13833Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgbm_model = LGBMRegressor(**lgbm_params)\nlgbm_model.fit(X_train, y_train)","metadata":{"papermill":{"duration":30.361232,"end_time":"2025-01-08T17:59:09.822083","exception":false,"start_time":"2025-01-08T17:58:39.460851","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T12:59:36.140725Z","iopub.execute_input":"2025-01-18T12:59:36.141084Z","iopub.status.idle":"2025-01-18T13:00:02.464668Z","shell.execute_reply.started":"2025-01-18T12:59:36.141046Z","shell.execute_reply":"2025-01-18T13:00:02.463857Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_preds = lgbm_model.predict(X_test)","metadata":{"papermill":{"duration":2.598485,"end_time":"2025-01-08T17:59:12.465583","exception":false,"start_time":"2025-01-08T17:59:09.867098","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:00:02.465562Z","iopub.execute_input":"2025-01-18T13:00:02.465863Z","iopub.status.idle":"2025-01-18T13:00:06.691649Z","shell.execute_reply.started":"2025-01-18T13:00:02.465837Z","shell.execute_reply":"2025-01-18T13:00:06.688168Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgbm_mape = mean_absolute_percentage_error(y_test, y_preds)\nprint(f\"LightGBM MAPE: {lgbm_mape:.4f}\")","metadata":{"papermill":{"duration":0.056227,"end_time":"2025-01-08T17:59:12.567834","exception":false,"start_time":"2025-01-08T17:59:12.511607","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:00:06.69266Z","iopub.execute_input":"2025-01-18T13:00:06.693096Z","iopub.status.idle":"2025-01-18T13:00:06.71545Z","shell.execute_reply.started":"2025-01-18T13:00:06.693057Z","shell.execute_reply":"2025-01-18T13:00:06.714384Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preds = lgbm_model.predict(test_df)","metadata":{"papermill":{"duration":9.177245,"end_time":"2025-01-08T17:59:21.792275","exception":false,"start_time":"2025-01-08T17:59:12.61503","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:00:06.716293Z","iopub.execute_input":"2025-01-18T13:00:06.716661Z","iopub.status.idle":"2025-01-18T13:00:18.417355Z","shell.execute_reply.started":"2025-01-18T13:00:06.716627Z","shell.execute_reply":"2025-01-18T13:00:18.416415Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nsubmission = pd.DataFrame({\n    'id': test_df['id'], \n    'premium_amount': test_preds \n})","metadata":{"papermill":{"duration":0.057851,"end_time":"2025-01-08T17:59:21.902696","exception":false,"start_time":"2025-01-08T17:59:21.844845","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:00:18.418391Z","iopub.execute_input":"2025-01-18T13:00:18.418991Z","iopub.status.idle":"2025-01-18T13:00:18.426932Z","shell.execute_reply.started":"2025-01-18T13:00:18.418954Z","shell.execute_reply":"2025-01-18T13:00:18.42599Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_filename = \"submission.csv\"\nsubmission.to_csv(submission_filename, index=False)\nprint(f\"Submission file saved as {submission_filename}\")","metadata":{"papermill":{"duration":1.6513,"end_time":"2025-01-08T17:59:23.610774","exception":false,"start_time":"2025-01-08T17:59:21.959474","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:00:18.428079Z","iopub.execute_input":"2025-01-18T13:00:18.42881Z","iopub.status.idle":"2025-01-18T13:00:19.980333Z","shell.execute_reply.started":"2025-01-18T13:00:18.428775Z","shell.execute_reply":"2025-01-18T13:00:19.97919Z"}},"outputs":[],"execution_count":null}]}