{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":82611,"databundleVersionId":9553358,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install lightgbm --install-option=--gpu","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shap# 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\nimport lightgbm as lgb\nimport shap\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error\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-11-26T20:32:06.113325Z","iopub.execute_input":"2024-11-26T20:32:06.114314Z","iopub.status.idle":"2024-11-26T20:32:06.119292Z","shell.execute_reply.started":"2024-11-26T20:32:06.114277Z","shell.execute_reply":"2024-11-26T20:32:06.118266Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# activity key, value pair\ndo_hyperparameter_tuning = False\nactivitis_dict = {\"Indoor climbing\" : 1, \"Run\" : 2, \"Strength training\" : 3, \"Swim\" : 4, \"Bike\" : 5, \"Dancing\" : 6, \n             \"Stairclimber\" : 7, \"Spinning\" : 8, \"Walking\" : 9, \"HIIT\" : 10, \"Outdoor Bike\" : 11, \"Walk\" : 12, \"Aerobic Workout\" : 13,\n             \"Tennis\" : 14, \"Workout\" : 15, \"Hike\" : 16, \"Zumba\": 17, \"Sport\" : 18, \"Yoga\" : 19, \"Swimming\" : 20, \"Weights\" : 21,\n             \"Running\" : 22, \"Cycling\" : 23, \"CoreTraining\" : 24, \"-1\" : -1}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:32:08.644634Z","iopub.execute_input":"2024-11-26T20:32:08.644935Z","iopub.status.idle":"2024-11-26T20:32:08.650127Z","shell.execute_reply.started":"2024-11-26T20:32:08.644911Z","shell.execute_reply":"2024-11-26T20:32:08.649185Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"class Explore_Dataset_and_Post_Processing:\n    def __init__(self, train_csv_path):\n        assert train_csv_path is not None, \"train_csv_path cannot be none\"\n        self.train_csv_path = train_csv_path\n        self.train_df = pd.read_csv(self.train_csv_path)\n        self.column_prefixes = ['bg', 'insulin', 'carbs', 'hr', 'steps', 'cals', 'activity']\n    \n    # some general functions\n    def get_shape(self):\n        return self.train_df.shape\n    \n    def get_df_description(self):\n        return self.train_df.describe()\n    \n    def get_column_wise_null(self):\n        \"\"\"\n        return the number of values that are null\n        \"\"\"\n        return self.train_df.isnull().sum()\n    \n    def get_percentage_of_missing_values(self): \n        \"\"\"\n        returns the pecentage of missing values\n        \"\"\"\n        # getting the percentage of missing values\n        missing_values = self.train_df.isnull().sum()\n        total_rows = self.train_df.shape[0]\n        missing_percentage = (missing_values / total_rows) * 100\n\n        results_df = pd.DataFrame({'Column': missing_values.index,\n                                  'Missing Values': missing_values.values,\n                                  'Missing Percentage (%)': missing_percentage.values})\n\n        results_df = results_df.sort_values(by='Missing Percentage (%)', ascending=False)\n        return results_df\n    \n    def pp_convert_datetime(self):\n        \"\"\"\n        converts the time column to date and time\n        \"\"\"\n        assert ptypes.is_datetime64_any_dtype(self.train_df[\"time\"]) is not True, \"DateTime is already converted\" \n        self.train_df.time = pd.to_datetime(self.train_df.time, format='%H:%M:%S')\n        return self\n        \n    def pp_replace_null_values(self):\n        \"\"\"\n        replace all null values with 0\n        \"\"\"\n        # check if nan values are present\n        has_nan = self.train_df.isnull().values.any()\n        if not has_nan:\n            print(\"No existing null values in the dataframe.\")\n        else:\n            self.train_df = self.train_df.fillna(0)\n        return self\n    \n    def pp_replace_null_with_fill_forward(self):\n        \"\"\"\n        Replacing the Nan with values with fill forward for bg, insulin and heartrate columns\n        Reference : https://www.kaggle.com/code/vineetgupta2023/key-insights-from-xgboost-score-2-200\n        \"\"\"\n        # blood glucose\n        bg_columns = [col for col in self.train_df.columns if col.startswith('bg-')]\n        self.train_df[bg_columns] = self.train_df[bg_columns].fillna(method='ffill', axis=1)\n        self.train_df[bg_columns] = self.train_df[bg_columns].fillna(method='bfill', axis=1)\n        \n        # insulin\n        insulin_columns = [col for col in self.train_df.columns if col.startswith('insulin-')]\n        self.train_df[insulin_columns] = self.train_df[insulin_columns].fillna(method='ffill', axis=1)\n        self.train_df[insulin_columns] = self.train_df[insulin_columns].fillna(method='bfill', axis=1)\n        \n        # heart rate\n        hr_columns = [col for col in self.train_df if col.startswith('hr-')]\n        self.train_df[hr_columns] = self.train_df[hr_columns].fillna(method='ffill', axis=1)\n        self.train_df[hr_columns] = self.train_df[hr_columns].fillna(method='bfill', axis=1)\n        return self\n\n    def pp_fill_Nan_with_negative_one(self):\n        \"\"\"\n        function to fill the \n        \"\"\"\n        self.train_df = self.train_df.fillna(-1)\n        return self\n    \n    def pp_drop_id_pnum_column(self):\n        self.train_df = self.train_df.drop([\"id\", \"p_num\", \"time\"], axis=1)\n        return self\n        \n    def pp_map_activities_to_number(self, activity_dict):\n        \"\"\"\n        Fuction to map the activity names to integers\n        \"\"\"\n        # get all the columns with name activity\n        try:\n            activity_columns = list(self.train_df.filter(like='activity').columns)\n            #self.train_df[activity_columns] = self.train_df[activity_columns].replace(activitis_dict)\n            for col in activity_columns:\n                self.train_df[col] = self.train_df[col].apply(lambda x: activitis_dict.get(x, -1) if x != '' else -1)\n        except AssertionError as e:\n            print(f\"AssertionError caught: {e}\")\n        return self\n            \n    def pp_replace_nan_with_mean(self):\n        \"\"\"\n        Function to replace the nan values with mean\n        \"\"\"\n        # get the numerical columns in the dataset\n        numerical_columns = self.train_df.select_dtypes(include = ['number']).columns\n        \n        # now replace for every column with nan values the  mean of that column\n        self.train_df[numerical_columns] = self.train_df[numerical_columns].apply(lambda col: col.fillna(col.mean()))\n        return self\n        \n    def pp_normalize_numerical_values(self):\n        \"\"\"\n        Normlaize the numerical values. Here we do min ma normalization\n        \"\"\"\n        # get the numerical columns in the dataset\n        numerical_columns = list(self.train_df.select_dtypes(include = ['number']).columns)\n        for column in numerical_columns:\n            self.train_df[column] = (self.train_df[column] - self.train_df[column].min()) / (self.train_df[column].max() - self.train_df[column].min())\n            \n        return self\n        \n    def pp_impute_numeical_columns(self):\n        numerical_columns = list(self.train_df.select_dtypes(include = ['number']).columns)\n        try:\n            numerical_columns.remove('bg+1:00')\n        except:\n            pass\n        imputer = SimpleImputer(strategy='mean')\n        self.train_df[numerical_columns] = imputer.fit_transform(self.train_df[numerical_columns])\n        scaler = StandardScaler()\n        self.train_df[numerical_columns] = scaler.fit_transform(self.train_df[numerical_columns])\n        return self\n        \n    def pp_encode_categorical_columns(self):\n        categorical_cols = [col for col in self.train_df.columns if 'activity' in col]\n        for col in categorical_cols:\n            self.train_df[col] = self.train_df[col].fillna('None')\n            le = LabelEncoder()\n            self.train_df[col] = le.fit_transform(self.train_df[col])\n        return self\n\n    def pp_drop_columns_with_missing_values(self, missing_df,pecentage):\n        \"\"\"\n        missing_value_df : o/p of the function obtained from \"get_percentage_of_missing_values\"\n        percentage : column with missing values >= pecentage is dropped for training and testing\n        \"\"\"\n        # list of columns with missing values  > 90\n        filtered_columns = missing_df[missing_df['Missing Percentage (%)'] > pecentage]['Column'].tolist()\n        # now drop all these columns\n        self.train_df = self.train_df.drop(filtered_columns, axis = 1)\n        return self\n\n    def pp_carbs_by_doing_hourly_average(self):\n        \"\"\"\n        In this we do the average of the carbs columns.\n        -We average the value of carbs every hour\n        -Hence instead of 12 columns per hour (5 mins interval column), we get 1 column\n        -As we know there is an enormous amount of NaN values in the carbs column, this will atleast help in\n        reducing the number of NaN values\n        \"\"\"\n        # get all the carbs columns \n        carbs_columns = [col for col in self.train_df.columns if col.startswith('carbs-')]\n        hours = ['5', '4', '3', '2', '1', '0']\n        for h in hours:\n             carb_hour_columns = [name for name in carbs_columns if name.startswith(\"carbs-\" + h)]\n             self.train_df[\"average-\"+h+\"-carbs\"] = self.train_df[carb_hour_columns].mean(axis=1)\n        return self\n\n    def pp_steps_by_doing_hourly_average(self):\n        \"\"\"\n        Here we tahe the average of the steps column\n        from my EDA, I have seen second biggest missing value column is steps\n        Hence averaging steps can be also used to reduce dimensionality\n        \"\"\"\n        # get all the steps columns\n        steps_columns = [col for col in self.train_df.columns if col.startswith('steps-')] \n        hours = ['5', '4', '3', '2', '1', '0']\n        for h in hours:\n             step_hour_column = [name for name in steps_columns if name.startswith(\"steps-\" + h)]\n             self.train_df[\"average-\"+h+\"-steps\"] = self.train_df[step_hour_column].mean(axis=1)\n        return self\n\n    def pp_drop_a_list_of_columns_if_needed(self, prefix):\n        \"\"\"\n        This function will help in dropping a list of columns  \n        which we might not be interested in pitting into our training data.\n        It will drop all the columns with that specific prefix\n        \"\"\"\n        assert prefix in self.column_prefixes, \"Please provide the right prefix. The right prefixes are {}\".format(self.column_prefixes)\n        columns_to_drop = [col for col in self.train_df.columns if col.startswith(prefix + '-')]\n        self.train_df = self.train_df.drop(columns_to_drop, axis = 1)\n        return self\n\n    def pp_column_renaming(self):\n        \n        \"\"\"\n        In LightGBM the column names cannot have special characters and hence this is needed\n        \"\"\"\n        self.train_df.columns = self.train_df.columns.str.replace(r'[^\\w\\s]', '', regex=True)\n        return self\n\n    def pp_select_columns_as_features(self, column_list):\n        self.train_df = self.train_df[column_list]\n        return self\n        \n    def get_final_df(self):\n        return self.train_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:32:10.216470Z","iopub.execute_input":"2024-11-26T20:32:10.216768Z","iopub.status.idle":"2024-11-26T20:32:10.241394Z","shell.execute_reply.started":"2024-11-26T20:32:10.216744Z","shell.execute_reply":"2024-11-26T20:32:10.240564Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Dataset Creation","metadata":{}},{"cell_type":"code","source":"class DatasetCreation:\n    def __init__(self, train_df, activities_dict, train_ratio, test_ratio):\n        self.actvities_dict = activities_dict\n        \"\"\"\n        few assertions and checks needed to be done\n        \"\"\"\n        # check if all nun values are non eistent\n        if train_df.isnull().values.any():\n            raise ValueError(\"There should not be NaN in training data\")\n        \n        # assert that id and p_num is dropped\n        columns = list(train_df.columns)\n        assert \"id\" not in columns, \"There should not be the id column\"\n        assert \"p_num\" not in columns, \"There should not be the p_num column\"\n        \n        # check that all the activities column have dtype float64\n        activity_columns = list(train_df.filter(like='activity').columns)\n        for col in activity_columns:\n            for value in train_df[col]:\n                if value == 0:\n                    pass\n                elif value not in self.actvities_dict.values():\n                    raise AssertionError(f\"Value {value} in column {col} was not correctly mapped!\")\n        \n        # check if train and test ratio is float\n        assert isinstance(train_ratio, float), f\"Expected float, but got {type(x).__name__}\"\n        assert isinstance(test_ratio, float), f\"Expected float, but got {type(x).__name__}\"\n        \n        self.train_df = train_df\n        self.train_ratio = train_ratio\n        self.test_ratio = test_ratio\n        \n    def return_dataset(self):\n        train_X = self.train_df.drop(['bg+1:00'], axis = 1)\n        train_Y = self.train_df['bg+1:00']\n        X_train_split, X_val_split, y_train_split, y_val_split = train_test_split(train_X, train_Y, test_size=self.test_ratio, random_state=42)\n        \n        return X_train_split, X_val_split, y_train_split, y_val_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:32:15.045696Z","iopub.execute_input":"2024-11-26T20:32:15.046034Z","iopub.status.idle":"2024-11-26T20:32:15.053134Z","shell.execute_reply.started":"2024-11-26T20:32:15.045991Z","shell.execute_reply":"2024-11-26T20:32:15.052254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv_path = \"/kaggle/input/brist1d/train.csv\"\nobj_data_pipeline = Explore_Dataset_and_Post_Processing(train_csv_path)\n\n# Simple training Data creation pipeline\n\"\"\"\nHere the order of preprocessing is very important\n\"\"\"\n# get the percentage of missing values df\nmissing_values_df = obj_data_pipeline.get_percentage_of_missing_values()\nprint(missing_values_df.head())\n# preporcessing data\nprocessed_train_df = (obj_data_pipeline\n                     .pp_drop_id_pnum_column()\n                      # take the avergae of the carbs \n                     #.pp_carbs_by_doing_hourly_average()\n                     # drop the other carb columns \n                     .pp_drop_a_list_of_columns_if_needed(prefix='activity')\n                     #.pp_replace_null_with_fill_forward()\n                     .pp_replace_null_values()\n                     #.pp_map_activities_to_number(activitis_dict)\n                     #.pp_fill_Nan_with_negative_one()\n                     .pp_select_columns_as_features(['bg-0:00', 'bg-0:05', 'bg-0:15','bg+1:00'])\n                     .get_final_df())\n\nprint(\"[!] Number of columns in processed df : \", len(processed_train_df.columns))\nobj_data_create = DatasetCreation(processed_train_df, activities_dict=activitis_dict, train_ratio=0.8, test_ratio=0.2)\nx_train, x_val, y_train, y_val  = obj_data_create.return_dataset()\n\nx_train.columns = x_train.columns.str.replace(r'[^\\w\\s]', '', regex=True)\nx_val.columns = x_val.columns.str.replace(r'[^\\w\\s]', '', regex=True)\n\nprint(\"Type of X_train : \", type(x_train))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:32:17.638487Z","iopub.execute_input":"2024-11-26T20:32:17.638810Z","iopub.status.idle":"2024-11-26T20:32:30.199051Z","shell.execute_reply.started":"2024-11-26T20:32:17.638781Z","shell.execute_reply":"2024-11-26T20:32:30.197908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:32:33.112384Z","iopub.execute_input":"2024-11-26T20:32:33.112714Z","iopub.status.idle":"2024-11-26T20:32:33.138998Z","shell.execute_reply.started":"2024-11-26T20:32:33.112685Z","shell.execute_reply":"2024-11-26T20:32:33.138253Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Creating the LightGBM Model","metadata":{}},{"cell_type":"markdown","source":"### Using optuna to do hyper parameter tuning\nSome technical Notes\n1. This process of training over multiple random samples without replacement is called “bagging”. ","metadata":{}},{"cell_type":"markdown","source":"\ndef objective_lightgbm(trial):\n    start_time = time.time()\n    # Define the hyperparameter search space\n    params = {\n        \"objective\": \"regression\",\n        \"metric\": \"rmse\",\n        \"boosting_type\": \"gbdt\",\n        \"device_type\" : \"gpu\",\n        \"lambda_l1\": trial.suggest_loguniform(\"lambda_l1\", 1e-8, 10.0), #over-fitting parameter\n        \"lambda_l2\": trial.suggest_loguniform(\"lambda_l2\", 1e-8, 10.0), #over-fitting parameter\n        \"num_leaves\": trial.suggest_int(\"num_leaves\", 2, 256),#controlling complexity of tree model\n        \"feature_fraction\": trial.suggest_uniform(\"feature_fraction\", 0.4, 1.0),# decreasing the number of features to be considered\n        \"bagging_fraction\": trial.suggest_uniform(\"bagging_fraction\", 0.4, 1.0),# again draw samples of that much % of training data\n        \"bagging_freq\": trial.suggest_int(\"bagging_freq\", 1, 7),#how many iterations without resampling\n        \"min_child_samples\": trial.suggest_int(\"min_child_samples\", 5, 100),# minimum number of data in a leaf\n        \"learning_rate\": trial.suggest_loguniform(\"learning_rate\", 1e-4, 0.1),\n    }\n\n    # Create LightGBM dataset\n    train_data = lgb.Dataset(x_train, label=y_train)\n    valid_data = lgb.Dataset(x_val, label=y_val, reference=train_data)\n\n    # Train the model\n    model = lgb.train(\n        params,\n        train_data,\n        valid_sets=[train_data, valid_data],\n        num_boost_round=1000,\n        #early_stopping_rounds=20,\n        #verbose_eval=False,\n    )\n\n    # Predict and calculate RMSE\n    preds = model.predict(x_val, num_iteration=model.best_iteration)\n    rmse = np.sqrt(mean_squared_error(y_val, preds, squared=False))\n\n    # End timing\n    end_time = time.time()\n    elapsed_time = end_time - start_time\n    # Print elapsed time for the trial\n    print(f\"Trial {trial.number} finished in {elapsed_time:.2f} seconds.\")\n    return rmse\n","metadata":{"execution":{"iopub.status.busy":"2024-11-24T17:43:56.487638Z","iopub.execute_input":"2024-11-24T17:43:56.488013Z","iopub.status.idle":"2024-11-24T17:43:56.495582Z","shell.execute_reply.started":"2024-11-24T17:43:56.487981Z","shell.execute_reply":"2024-11-24T17:43:56.494662Z"}}},{"cell_type":"markdown","source":"# Create a study and optimize\nif do_hyperparameter_tuning:\n    import time\n    import optuna\n    study = optuna.create_study(direction=\"minimize\")\n    study.optimize(objective_lightgbm, n_trials=10)\nelse:\n    pass","metadata":{"execution":{"iopub.status.busy":"2024-11-24T17:44:00.665230Z","iopub.execute_input":"2024-11-24T17:44:00.665981Z","iopub.status.idle":"2024-11-24T17:44:00.670176Z","shell.execute_reply.started":"2024-11-24T17:44:00.665946Z","shell.execute_reply":"2024-11-24T17:44:00.669241Z"}}},{"cell_type":"code","source":"\nimport lightgbm\nprint(lightgbm.__version__)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T17:44:02.649562Z","iopub.execute_input":"2024-11-24T17:44:02.650479Z","iopub.status.idle":"2024-11-24T17:44:02.654896Z","shell.execute_reply.started":"2024-11-24T17:44:02.650439Z","shell.execute_reply":"2024-11-24T17:44:02.653952Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"train_data = lgb.Dataset(x_train, label=y_train)\ntest_data = lgb.Dataset(x_val, label=y_val, reference=train_data)","metadata":{"execution":{"iopub.status.busy":"2024-11-24T17:44:04.497788Z","iopub.execute_input":"2024-11-24T17:44:04.498452Z","iopub.status.idle":"2024-11-24T17:44:04.502674Z","shell.execute_reply.started":"2024-11-24T17:44:04.498414Z","shell.execute_reply":"2024-11-24T17:44:04.501636Z"}}},{"cell_type":"markdown","source":"params = {\n        \"objective\": \"regression\",\n        \"metric\": \"rmse\",\n        \"boosting_type\": \"gbdt\",\n        \"device_type\" : \"gpu\",\n        \"lambda_l1\": 0.026758431635345595, #over-fitting parameter\n        \"lambda_l2\": 1.1912416629585453e-08, #over-fitting parameter\n        \"num_leaves\": 144,#controlling complexity of tree model\n        \"feature_fraction\": 0.9818350824277666,# decreasing the number of features to be considered\n        \"bagging_fraction\": 0.7450061528836429,# again draw samples of that much % of training data\n        \"bagging_freq\": 2,#how many iterations without resampling\n        \"min_child_samples\": 30,# minimum number of data in a leaf\n        \"learning_rate\": 0.07174143281315025,\n    }\nmodel = lgb.train(params, train_data, valid_sets=[test_data], num_boost_round=50)","metadata":{"execution":{"iopub.status.busy":"2024-11-24T17:44:09.306499Z","iopub.execute_input":"2024-11-24T17:44:09.306838Z","iopub.status.idle":"2024-11-24T17:44:24.572236Z","shell.execute_reply.started":"2024-11-24T17:44:09.306807Z","shell.execute_reply":"2024-11-24T17:44:24.571180Z"}}},{"cell_type":"markdown","source":"### Having a check on which of the features contributes to the most","metadata":{}},{"cell_type":"code","source":"explainer = shap.Explainer(model)\nshap_values = explainer(x_val)\nshap.summary_plot(shap_values, x_val) ","metadata":{"execution":{"iopub.status.busy":"2024-11-24T17:44:28.385949Z","iopub.execute_input":"2024-11-24T17:44:28.386551Z","iopub.status.idle":"2024-11-24T17:45:35.238073Z","shell.execute_reply.started":"2024-11-24T17:44:28.386517Z","shell.execute_reply":"2024-11-24T17:45:35.237147Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"y_pred = model.predict(x_val, num_iteration=model.best_iteration)","metadata":{"execution":{"iopub.status.busy":"2024-11-24T17:45:40.700203Z","iopub.execute_input":"2024-11-24T17:45:40.700883Z","iopub.status.idle":"2024-11-24T17:45:40.898169Z","shell.execute_reply.started":"2024-11-24T17:45:40.700831Z","shell.execute_reply":"2024-11-24T17:45:40.897314Z"}}},{"cell_type":"markdown","source":"from sklearn.metrics import mean_squared_error\nfrom math import sqrt","metadata":{"execution":{"iopub.status.busy":"2024-11-24T17:45:42.404475Z","iopub.execute_input":"2024-11-24T17:45:42.405186Z","iopub.status.idle":"2024-11-24T17:45:42.409421Z","shell.execute_reply.started":"2024-11-24T17:45:42.405146Z","shell.execute_reply":"2024-11-24T17:45:42.408437Z"}}},{"cell_type":"markdown","source":"rmse = sqrt(mean_squared_error(y_val, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-11-24T17:45:43.395630Z","iopub.execute_input":"2024-11-24T17:45:43.396103Z","iopub.status.idle":"2024-11-24T17:45:43.401724Z","shell.execute_reply.started":"2024-11-24T17:45:43.396066Z","shell.execute_reply":"2024-11-24T17:45:43.400788Z"}}},{"cell_type":"code","source":"# Save the model\n#model.save_model('/kaggle/working/lgb_classifier.txt', num_iteration=model.best_iteration) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T17:45:46.586061Z","iopub.execute_input":"2024-11-24T17:45:46.586441Z","iopub.status.idle":"2024-11-24T17:45:46.605105Z","shell.execute_reply.started":"2024-11-24T17:45:46.586405Z","shell.execute_reply":"2024-11-24T17:45:46.604317Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Creating a CATBOOST MODEL","metadata":{}},{"cell_type":"code","source":"from catboost import CatBoostRegressor, Pool","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:32:57.472801Z","iopub.execute_input":"2024-11-26T20:32:57.473180Z","iopub.status.idle":"2024-11-26T20:32:57.477473Z","shell.execute_reply.started":"2024-11-26T20:32:57.473149Z","shell.execute_reply":"2024-11-26T20:32:57.476502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = CatBoostRegressor(\n    iterations=1000,    # Number of boosting iterations\n    learning_rate=0.1,  # Learning rate\n    depth=6,            # Depth of each tree\n    verbose=100         # Verbosity to monitor training\n)\nmodel.fit(x_train, y_train, eval_set=(x_val, y_val), use_best_model=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:34:07.748446Z","iopub.execute_input":"2024-11-26T20:34:07.748746Z","iopub.status.idle":"2024-11-26T20:36:07.183915Z","shell.execute_reply.started":"2024-11-26T20:34:07.748722Z","shell.execute_reply":"2024-11-26T20:36:07.183055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(x_val)\nrmse = np.sqrt(mean_squared_error(y_val, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:38:23.909137Z","iopub.execute_input":"2024-11-26T20:38:23.909494Z","iopub.status.idle":"2024-11-26T20:38:24.008251Z","shell.execute_reply.started":"2024-11-26T20:38:23.909464Z","shell.execute_reply":"2024-11-26T20:38:24.007468Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rmse","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:38:30.779232Z","iopub.execute_input":"2024-11-26T20:38:30.780232Z","iopub.status.idle":"2024-11-26T20:38:30.786247Z","shell.execute_reply.started":"2024-11-26T20:38:30.780187Z","shell.execute_reply":"2024-11-26T20:38:30.785208Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"explainer = shap.Explainer(model)\nshap_values = explainer(x_val)\nshap.summary_plot(shap_values, x_val) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T21:02:56.184992Z","iopub.execute_input":"2024-11-26T21:02:56.185829Z","iopub.status.idle":"2024-11-26T21:03:08.607232Z","shell.execute_reply.started":"2024-11-26T21:02:56.185795Z","shell.execute_reply":"2024-11-26T21:03:08.606428Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Creating the Submission","metadata":{}},{"cell_type":"code","source":"#test_csv = pd.read_csv(\"/kaggle/input/brist1d/test.csv\")\ntest_csv_path = \"/kaggle/input/brist1d/test.csv\"\nobj_data_pipeline = Explore_Dataset_and_Post_Processing(test_csv_path)\n\n# Simple training Data creation pipeline\n\"\"\"\nHere the order of preprocessing is very important\n\"\"\"\n# get the percentage of missing values df\nmissing_values_df = obj_data_pipeline.get_percentage_of_missing_values()\nprint(missing_values_df.head())\n# preporcessing data\nprocessed_test_df = (obj_data_pipeline\n                     .pp_drop_id_pnum_column()\n                      # take the avergae of the carbs \n                     #.pp_carbs_by_doing_hourly_average()\n                     # drop the other carb columns \n                     .pp_drop_a_list_of_columns_if_needed(prefix='activity')\n                     #.pp_replace_null_with_fill_forward()\n                     .pp_replace_null_values()\n                     #.pp_map_activities_to_number(activitis_dict)\n                     #.pp_fill_Nan_with_negative_one()\n                     #.pp_select_columns_as_features(['bg-0:00', 'bg-0:05', 'bg-0:15','bg+1:00'])\n                     .get_final_df())\nprocessed_test_df.columns = processed_test_df.columns.str.replace(r'[^\\w\\s]', '', regex=True)\npredictions = model.predict(processed_test_df)\nprint(predictions)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:57:50.415092Z","iopub.execute_input":"2024-11-26T20:57:50.415928Z","iopub.status.idle":"2024-11-26T20:57:50.614044Z","shell.execute_reply.started":"2024-11-26T20:57:50.415897Z","shell.execute_reply":"2024-11-26T20:57:50.613110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load submission \nsubmission_df = pd.read_csv(\"/kaggle/input/brist1d/sample_submission.csv\")\nsubmission_df['bg+1:00'] = predictions\n# Save the submission file\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T20:57:53.323090Z","iopub.execute_input":"2024-11-26T20:57:53.323433Z","iopub.status.idle":"2024-11-26T20:57:53.346356Z","shell.execute_reply.started":"2024-11-26T20:57:53.323405Z","shell.execute_reply":"2024-11-26T20:57:53.345691Z"}},"outputs":[],"execution_count":null}]}