{"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":82611,"databundleVersionId":9553358,"sourceType":"competition"}],"dockerImageVersionId":30775,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Table of Contents\n* [Basic EDA](#eda)\n* [Target vs Features](#target_vs_features)\n* [Time Series Plots](#ts)\n* [Data Preprocessing](#dp)\n* [Model](#model)","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport time\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport os\nimport re","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-25T07:23:53.425490Z","iopub.execute_input":"2024-11-25T07:23:53.425880Z","iopub.status.idle":"2024-11-25T07:23:57.088962Z","shell.execute_reply.started":"2024-11-25T07:23:53.425840Z","shell.execute_reply":"2024-11-25T07:23:57.087587Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# configs\npd.set_option('display.max_columns', None) # we want to display all columns in this notebook\npd.set_option('display.max_rows', 100) # increase rows to be displayed\npd.set_option('display.max_colwidth', None) # show full cell contents\n\n# random seed\nmy_random_seed = 111\n\n# aesthetics\ndefault_color_1 = 'darkblue'\ndefault_color_2 = 'darkred'\n\n# Ignore all warnings for this notebook\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:23:57.090958Z","iopub.execute_input":"2024-11-25T07:23:57.091463Z","iopub.status.idle":"2024-11-25T07:23:57.098789Z","shell.execute_reply.started":"2024-11-25T07:23:57.091428Z","shell.execute_reply":"2024-11-25T07:23:57.097508Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load data\nt1 = time.time()\ndf_train = pd.read_csv('/kaggle/input/brist1d/train.csv', low_memory=False)\ndf_test = pd.read_csv('/kaggle/input/brist1d/test.csv', low_memory=False)\ndf_sub = pd.read_csv('/kaggle/input/brist1d/sample_submission.csv', low_memory=False)\n\n# Output the time of loading data\nt2 = time.time()\nprint('Elapsed time [s]:', np.round(t2-t1,4))","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:23:57.100152Z","iopub.execute_input":"2024-11-25T07:23:57.100667Z","iopub.status.idle":"2024-11-25T07:24:19.989368Z","shell.execute_reply.started":"2024-11-25T07:23:57.100605Z","shell.execute_reply":"2024-11-25T07:24:19.988153Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='eda'></a>\n# EDA","metadata":{}},{"cell_type":"code","source":"# Date conversion - Make sure the time column is in the format of %H%M%S\ndf_train.time = pd.to_datetime(df_train.time, format='%H:%M:%S')\ndf_test.time = pd.to_datetime(df_test.time, format='%H:%M:%S')","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:19.992923Z","iopub.execute_input":"2024-11-25T07:24:19.993420Z","iopub.status.idle":"2024-11-25T07:24:20.047090Z","shell.execute_reply.started":"2024-11-25T07:24:19.993381Z","shell.execute_reply":"2024-11-25T07:24:20.045995Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# show structure of train dataset\ndf_train.info(show_counts=True, verbose=True)","metadata":{"_kg_hide-output":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-25T07:24:20.048360Z","iopub.execute_input":"2024-11-25T07:24:20.048709Z","iopub.status.idle":"2024-11-25T07:24:20.678656Z","shell.execute_reply.started":"2024-11-25T07:24:20.048676Z","shell.execute_reply":"2024-11-25T07:24:20.676871Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### 💡 We can see that there are a lot of missing values!","metadata":{}},{"cell_type":"code","source":"# show structure of test dataset\ndf_test.info(show_counts=True, verbose=True)","metadata":{"_kg_hide-output":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-25T07:24:20.680876Z","iopub.execute_input":"2024-11-25T07:24:20.681599Z","iopub.status.idle":"2024-11-25T07:24:20.742217Z","shell.execute_reply.started":"2024-11-25T07:24:20.681550Z","shell.execute_reply":"2024-11-25T07:24:20.740908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# time range - training\ndf_train.time.describe()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:20.743834Z","iopub.execute_input":"2024-11-25T07:24:20.744259Z","iopub.status.idle":"2024-11-25T07:24:20.764829Z","shell.execute_reply.started":"2024-11-25T07:24:20.744210Z","shell.execute_reply":"2024-11-25T07:24:20.763635Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# time range - test\ndf_test.time.describe()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:20.766107Z","iopub.execute_input":"2024-11-25T07:24:20.766657Z","iopub.status.idle":"2024-11-25T07:24:20.780162Z","shell.execute_reply.started":"2024-11-25T07:24:20.766621Z","shell.execute_reply":"2024-11-25T07:24:20.778880Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# patient ids - training\ncount_train = df_train.p_num.value_counts().sort_index()\ncount_train","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:20.781741Z","iopub.execute_input":"2024-11-25T07:24:20.782053Z","iopub.status.idle":"2024-11-25T07:24:20.814192Z","shell.execute_reply.started":"2024-11-25T07:24:20.782022Z","shell.execute_reply":"2024-11-25T07:24:20.813148Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# patient ids - test\ncount_test = df_test.p_num.value_counts().sort_index()\ncount_test","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:20.818871Z","iopub.execute_input":"2024-11-25T07:24:20.819426Z","iopub.status.idle":"2024-11-25T07:24:20.838676Z","shell.execute_reply.started":"2024-11-25T07:24:20.819387Z","shell.execute_reply":"2024-11-25T07:24:20.837564Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# check which points in time are available in the test set\ndf_test.time.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:20.840303Z","iopub.execute_input":"2024-11-25T07:24:20.840789Z","iopub.status.idle":"2024-11-25T07:24:20.862708Z","shell.execute_reply.started":"2024-11-25T07:24:20.840741Z","shell.execute_reply":"2024-11-25T07:24:20.861183Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### 💡 We have 288 different points in time in the test set. This means that every possible point in time is also available in the test set, because we have 12 values per hour - having 5 minute intervals - and this adds up to 24 x 12 = 288 different values.","metadata":{}},{"cell_type":"code","source":"# extract hour\ndf_train['hour'] = df_train.time.apply(lambda x : x.hour)\ndf_test['hour'] = df_test.time.apply(lambda x : x.hour)\n\n# and show plot\nplt.figure(figsize=(12,4))\ndf_train.hour.value_counts().sort_index().plot(kind='bar', color=default_color_1)\nplt.title('Distribution of hour - Training')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:20.864646Z","iopub.execute_input":"2024-11-25T07:24:20.865100Z","iopub.status.idle":"2024-11-25T07:24:21.700523Z","shell.execute_reply.started":"2024-11-25T07:24:20.865053Z","shell.execute_reply":"2024-11-25T07:24:21.698670Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# convert time into minutes (start 0:00)\n# df_train['time_min'] = df_train.hour * 60 + df_train.time.apply(lambda x : x.minute)\n# df_test['time_min'] = df_test.hour * 60 + df_test.time.apply(lambda x : x.minute)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:21.701908Z","iopub.execute_input":"2024-11-25T07:24:21.702230Z","iopub.status.idle":"2024-11-25T07:24:21.709746Z","shell.execute_reply.started":"2024-11-25T07:24:21.702199Z","shell.execute_reply":"2024-11-25T07:24:21.707879Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# define target and predictors\ntarget = 'bg+1:00'\n\n# predictors = df_test.columns.tolist()\n# predictors.remove('id')\n# predictors.remove('time')\n# predictors.remove('time_min')","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:21.711276Z","iopub.execute_input":"2024-11-25T07:24:21.711698Z","iopub.status.idle":"2024-11-25T07:24:21.737984Z","shell.execute_reply.started":"2024-11-25T07:24:21.711663Z","shell.execute_reply":"2024-11-25T07:24:21.736574Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot target distribution\nplt.figure(figsize=(12,4))\ndf_train[target].plot(kind='hist', bins=50,\n                      color=default_color_2)\nplt.title(target)\nplt.grid()\nplt.show()\n\n# basic stats\nprint(df_train[target].describe())","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:21.740105Z","iopub.execute_input":"2024-11-25T07:24:21.740536Z","iopub.status.idle":"2024-11-25T07:24:22.128772Z","shell.execute_reply.started":"2024-11-25T07:24:21.740500Z","shell.execute_reply":"2024-11-25T07:24:22.127716Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='target_vs_features'></a>\n# Target vs Features","metadata":{}},{"cell_type":"code","source":"# plot target distribution by hour\nplt.figure(figsize=(14,6))\nsns.violinplot(data=df_train, x='hour', y=target)\nplt.title('Target by hour')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:22.129848Z","iopub.execute_input":"2024-11-25T07:24:22.130164Z","iopub.status.idle":"2024-11-25T07:24:23.465874Z","shell.execute_reply.started":"2024-11-25T07:24:22.130132Z","shell.execute_reply":"2024-11-25T07:24:23.464670Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### 💡 We see a dependency between time and the target values.","metadata":{}},{"cell_type":"code","source":"# plot target distribution by patient\nplt.figure(figsize=(12,5))\nsns.violinplot(data=df_train, x='p_num', y=target)\nplt.title('Target by patient')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:23.467251Z","iopub.execute_input":"2024-11-25T07:24:23.467608Z","iopub.status.idle":"2024-11-25T07:24:24.722873Z","shell.execute_reply.started":"2024-11-25T07:24:23.467574Z","shell.execute_reply":"2024-11-25T07:24:24.721772Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### 💡 We can also observe that the target distributions differ significantly by patient.","metadata":{}},{"cell_type":"code","source":"# pick only a subset of numerical features\nsome_features = ['bg-0:00', 'bg-1:00', 'bg-2:00',\n                 'insulin-0:00', 'insulin-1:00', 'insulin-2:00',\n                 'carbs-0:00', 'carbs-1:00', 'carbs-2:00',\n                 'hr-0:00', 'hr-1:00', 'hr-2:00',\n                 'cals-0:00', 'cals-1:00', 'cals-2:00',\n                 'steps-0:00', 'steps-1:00', 'steps-2:00',\n                 ]","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:24.724056Z","iopub.execute_input":"2024-11-25T07:24:24.724390Z","iopub.status.idle":"2024-11-25T07:24:24.730791Z","shell.execute_reply.started":"2024-11-25T07:24:24.724358Z","shell.execute_reply":"2024-11-25T07:24:24.729240Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# scatter plots of target vs selected features\nfor f in some_features:\n    plt.scatter(df_train[f], df_train[target], color=default_color_2,\n                alpha=0.1, s=2)\n    plt.title('Target vs ' + f)\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:24.732629Z","iopub.execute_input":"2024-11-25T07:24:24.733099Z","iopub.status.idle":"2024-11-25T07:24:30.979943Z","shell.execute_reply.started":"2024-11-25T07:24:24.733050Z","shell.execute_reply":"2024-11-25T07:24:30.978722Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='Data Preprocessing'></a>\n# Data Preprocessing","metadata":{}},{"cell_type":"markdown","source":"## Data Preprocessing - Training Dataset","metadata":{}},{"cell_type":"code","source":"# Ensure that each bg value has valid data when calculating\nbg_columns = [col for col in df_train.columns if col.startswith('bg-')]\ndf_train[bg_columns] = df_train[bg_columns].fillna(method='ffill', axis=1)\ndf_train[bg_columns] = df_train[bg_columns].fillna(method='bfill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:30.981637Z","iopub.execute_input":"2024-11-25T07:24:30.981978Z","iopub.status.idle":"2024-11-25T07:24:31.536197Z","shell.execute_reply.started":"2024-11-25T07:24:30.981948Z","shell.execute_reply":"2024-11-25T07:24:31.535023Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fill all insulin columns forward or backward with valid data\ninsulin_columns = [col for col in df_train.columns if col.startswith('insulin-')]\ndf_train[insulin_columns] = df_train[insulin_columns].fillna(method='ffill', axis=1)\ndf_train[insulin_columns] = df_train[insulin_columns].fillna(method='bfill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:31.537579Z","iopub.execute_input":"2024-11-25T07:24:31.537918Z","iopub.status.idle":"2024-11-25T07:24:32.104870Z","shell.execute_reply.started":"2024-11-25T07:24:31.537885Z","shell.execute_reply":"2024-11-25T07:24:32.103757Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fill all cals columns forward or backward with valid data\ncals_columns = [col for col in df_train.columns if col.startswith('cals-')]\ndf_train[cals_columns] = df_train[cals_columns].fillna(method='ffill', axis=1)\ndf_train[cals_columns] = df_train[cals_columns].fillna(method='bfill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:32.106245Z","iopub.execute_input":"2024-11-25T07:24:32.106730Z","iopub.status.idle":"2024-11-25T07:24:32.687271Z","shell.execute_reply.started":"2024-11-25T07:24:32.106673Z","shell.execute_reply":"2024-11-25T07:24:32.686068Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fill all step columns forward or backward with valid data\nsteps_columns = [col for col in df_train.columns if col.startswith('steps-')]\ndf_train[steps_columns] = df_train[steps_columns].fillna(method='ffill', axis=1)\ndf_train[steps_columns] = df_train[steps_columns].fillna(method='bfill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:32.689254Z","iopub.execute_input":"2024-11-25T07:24:32.689704Z","iopub.status.idle":"2024-11-25T07:24:33.283977Z","shell.execute_reply.started":"2024-11-25T07:24:32.689669Z","shell.execute_reply":"2024-11-25T07:24:33.282622Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fill all hr columns forward or backward with valid data\nhr_columns = [col for col in df_train.columns if col.startswith('hr-')]\ndf_train[hr_columns] = df_train[hr_columns].fillna(method='ffill', axis=1)\ndf_train[hr_columns] = df_train[hr_columns].fillna(method='bfill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:33.286448Z","iopub.execute_input":"2024-11-25T07:24:33.286857Z","iopub.status.idle":"2024-11-25T07:24:33.874378Z","shell.execute_reply.started":"2024-11-25T07:24:33.286818Z","shell.execute_reply":"2024-11-25T07:24:33.872624Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Sample data\ndata = {'activity': ['Indoor climbing','Run','Strength training','Swim','Bike','Dancing','Stairclimber','Spinning','Walking','HIIT','Outdoor Bike','Walk','Aerobic Workout','Tennis','Workout','Hike','Zumba','Sport','Yoga','Swimming','Weights','Running']}\ndf = pd.DataFrame(data)\n\n# Create a dictionary that maps each unique activity to a number\nunique_activities = {activity: i+1 for i, activity in enumerate(df['activity'].dropna().unique())}\nprint(\"Activity Mapping Dictionary:\", unique_activities)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:33.875795Z","iopub.execute_input":"2024-11-25T07:24:33.876160Z","iopub.status.idle":"2024-11-25T07:24:33.885407Z","shell.execute_reply.started":"2024-11-25T07:24:33.876124Z","shell.execute_reply":"2024-11-25T07:24:33.884251Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create a dictionary of participants\ndata = {'participant': ['p01','p02','p03','p04','p05','p06','p07','p08','p09','p10','p11','p12','p13','p14','p15']}\ndf = pd.DataFrame(data)\n\n# Create a dictionary that maps each unique activity to a number\nunique_participant = {participant: i+1 for i, participant in enumerate(df['participant'].dropna().unique())}\nprint(\"Activity Mapping Dictionary:\", unique_participant)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:33.887023Z","iopub.execute_input":"2024-11-25T07:24:33.887456Z","iopub.status.idle":"2024-11-25T07:24:33.904936Z","shell.execute_reply.started":"2024-11-25T07:24:33.887423Z","shell.execute_reply":"2024-11-25T07:24:33.903876Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Map the activity type value to the unique value defined in the dictionary, and handle the case of null values\nfor col in df_train.columns:\n    if 'activity' in col:\n        # Assign values based on the dictionary and set -1 for empty values\n        df_train[col] = df_train[col].apply(lambda x: unique_activities.get(x, -1) if x != '' else -1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:33.906210Z","iopub.execute_input":"2024-11-25T07:24:33.906577Z","iopub.status.idle":"2024-11-25T07:24:39.389978Z","shell.execute_reply.started":"2024-11-25T07:24:33.906535Z","shell.execute_reply":"2024-11-25T07:24:39.388909Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Map the p_num type value to the unique value defined in the dictionary, and handle the case of null values\nfor col in df_train.columns:\n    if 'p_num' == col:\n        # Assign values based on the dictionary and set -1 for empty values\n        df_train[col] = df_train[col].apply(lambda x: unique_participant.get(x, -1) if pd.notna(x) and x != '' else -1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:39.395547Z","iopub.execute_input":"2024-11-25T07:24:39.395907Z","iopub.status.idle":"2024-11-25T07:24:39.594321Z","shell.execute_reply.started":"2024-11-25T07:24:39.395872Z","shell.execute_reply":"2024-11-25T07:24:39.593165Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fill the empty values in the corresponding data frame with -1\ndf_train =df_train.fillna(-1)\n\n# Check our train data\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:39.595828Z","iopub.execute_input":"2024-11-25T07:24:39.596489Z","iopub.status.idle":"2024-11-25T07:24:40.867249Z","shell.execute_reply.started":"2024-11-25T07:24:39.596442Z","shell.execute_reply":"2024-11-25T07:24:40.865969Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# df_train =df_train.fillna(-1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:40.868578Z","iopub.execute_input":"2024-11-25T07:24:40.868888Z","iopub.status.idle":"2024-11-25T07:24:40.873676Z","shell.execute_reply.started":"2024-11-25T07:24:40.868858Z","shell.execute_reply":"2024-11-25T07:24:40.872483Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"In the EDA part, we will find that this periodicity cannot be well expressed using simple numbers (such as the hour values ​​0 and 23), so in order to express the periodicity of time, we convert time into two-dimensional sin and cos values.","metadata":{}},{"cell_type":"code","source":"# Extract components - Extract the hour in the %H%M%S date format\ndf_train['hour'] = pd.to_datetime(df_train['time'], format='%H:%M:%S').dt.hour\n\n# df_train['minute'] = pd.to_datetime(df_train['time'], format='%H:%M:%S').dt.minute\n# df_train['second'] = pd.to_datetime(df_train['time'], format='%H:%M:%S').dt.second","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:40.875189Z","iopub.execute_input":"2024-11-25T07:24:40.876002Z","iopub.status.idle":"2024-11-25T07:24:40.909558Z","shell.execute_reply.started":"2024-11-25T07:24:40.875944Z","shell.execute_reply":"2024-11-25T07:24:40.908158Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Calculate the sin and cos values of hour\ndf_train['hour_sin'] = np.sin(2 * np.pi * df_train['hour'] / 24)\ndf_train['hour_cos'] = np.cos(2 * np.pi * df_train['hour'] / 24)\n\n# Similarly, you can create cyclical features for minutes or seconds if needed\n# df_train['minute_sin'] = np.sin(2 * np.pi * df_train['minute'] / 60)\n# df_train['minute_cos'] = np.cos(2 * np.pi * df_train['minute'] / 60)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:40.911035Z","iopub.execute_input":"2024-11-25T07:24:40.911390Z","iopub.status.idle":"2024-11-25T07:24:40.928362Z","shell.execute_reply.started":"2024-11-25T07:24:40.911341Z","shell.execute_reply":"2024-11-25T07:24:40.926579Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Remove the time column from the data frame\ndf_train = df_train.drop(['time'],axis=1)\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:40.929898Z","iopub.execute_input":"2024-11-25T07:24:40.930233Z","iopub.status.idle":"2024-11-25T07:24:41.632092Z","shell.execute_reply.started":"2024-11-25T07:24:40.930192Z","shell.execute_reply":"2024-11-25T07:24:41.629983Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data Preprocessing - Test Dataset\n\nNext, the test set is also processed.","metadata":{}},{"cell_type":"code","source":"# Ensure that each bg value has valid data when calculating\ndf_test[bg_columns] = df_test[bg_columns].fillna(method='ffill', axis=1)\ndf_test[bg_columns] = df_test[bg_columns].fillna(method='bfill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:41.633689Z","iopub.execute_input":"2024-11-25T07:24:41.634098Z","iopub.status.idle":"2024-11-25T07:24:41.668635Z","shell.execute_reply.started":"2024-11-25T07:24:41.634052Z","shell.execute_reply":"2024-11-25T07:24:41.667656Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fill all insulin columns forward or backward with valid data\ndf_test[insulin_columns] = df_test[insulin_columns].fillna(method='ffill', axis=1)\ndf_test[insulin_columns] = df_test[insulin_columns].fillna(method='bfill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:41.669980Z","iopub.execute_input":"2024-11-25T07:24:41.670417Z","iopub.status.idle":"2024-11-25T07:24:41.709142Z","shell.execute_reply.started":"2024-11-25T07:24:41.670373Z","shell.execute_reply":"2024-11-25T07:24:41.707921Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fill all hr columns forward or backward with valid data\ndf_test[hr_columns] = df_test[hr_columns].fillna(method='ffill', axis=1)\ndf_test[hr_columns] = df_test[hr_columns].fillna(method='bfill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:41.710883Z","iopub.execute_input":"2024-11-25T07:24:41.711347Z","iopub.status.idle":"2024-11-25T07:24:41.747066Z","shell.execute_reply.started":"2024-11-25T07:24:41.711276Z","shell.execute_reply":"2024-11-25T07:24:41.745719Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fill all cals columns forward or backward with valid data\ndf_test[cals_columns] = df_test[cals_columns].fillna(method='ffill', axis=1)\ndf_test[cals_columns] = df_test[cals_columns].fillna(method='bfill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:41.749100Z","iopub.execute_input":"2024-11-25T07:24:41.749622Z","iopub.status.idle":"2024-11-25T07:24:41.782326Z","shell.execute_reply.started":"2024-11-25T07:24:41.749543Z","shell.execute_reply":"2024-11-25T07:24:41.781188Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fill all steps columns forward or backward with valid data\ndf_test[steps_columns] = df_test[steps_columns].fillna(method='ffill', axis=1)\ndf_test[steps_columns] = df_test[steps_columns].fillna(method='bfill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:41.783565Z","iopub.execute_input":"2024-11-25T07:24:41.783898Z","iopub.status.idle":"2024-11-25T07:24:41.822609Z","shell.execute_reply.started":"2024-11-25T07:24:41.783867Z","shell.execute_reply":"2024-11-25T07:24:41.821476Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Map the activity type value to the unique value defined in the dictionary, and handle the case of null values\nfor col in df_train.columns:\n    if 'activity' in col:\n        # Assign values based on the dictionary and set -1 for empty values\n        df_test[col] = df_test[col].apply(lambda x: unique_activities.get(x, -1) if x != '' else -1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:41.823920Z","iopub.execute_input":"2024-11-25T07:24:41.824350Z","iopub.status.idle":"2024-11-25T07:24:41.977258Z","shell.execute_reply.started":"2024-11-25T07:24:41.824304Z","shell.execute_reply":"2024-11-25T07:24:41.976211Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Map the p_num type value to the unique value defined in the dictionary, and handle the case of null values\nfor col in df_train.columns:\n    if 'p_num'== col:\n        # Assign values based on the dictionary and set -1 for empty values\n        df_test[col] = df_test[col].apply(lambda x: unique_participant.get(x, -1) if x != '' else -1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:41.978631Z","iopub.execute_input":"2024-11-25T07:24:41.978947Z","iopub.status.idle":"2024-11-25T07:24:41.989175Z","shell.execute_reply.started":"2024-11-25T07:24:41.978916Z","shell.execute_reply":"2024-11-25T07:24:41.987163Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Fill the empty values in the corresponding data frame with -1\ndf_test =df_test.fillna(-1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:41.991001Z","iopub.execute_input":"2024-11-25T07:24:41.991415Z","iopub.status.idle":"2024-11-25T07:24:42.038764Z","shell.execute_reply.started":"2024-11-25T07:24:41.991381Z","shell.execute_reply":"2024-11-25T07:24:42.037692Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extract components - Extract the hour in the %H%M%S date format\ndf_test['hour'] = pd.to_datetime(df_test['time'], format='%H:%M:%S').dt.hour\ndf_test['minute'] = pd.to_datetime(df_test['time'], format='%H:%M:%S').dt.minute\ndf_test['second'] = pd.to_datetime(df_test['time'], format='%H:%M:%S').dt.second","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:42.039980Z","iopub.execute_input":"2024-11-25T07:24:42.040320Z","iopub.status.idle":"2024-11-25T07:24:42.065518Z","shell.execute_reply.started":"2024-11-25T07:24:42.040257Z","shell.execute_reply":"2024-11-25T07:24:42.063088Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Calculate the sin and cos values of hour\ndf_test['hour_sin'] = np.sin(2 * np.pi * df_test['hour'] / 24)\ndf_test['hour_cos'] = np.cos(2 * np.pi * df_test['hour'] / 24)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:42.067486Z","iopub.execute_input":"2024-11-25T07:24:42.068237Z","iopub.status.idle":"2024-11-25T07:24:42.084900Z","shell.execute_reply.started":"2024-11-25T07:24:42.068182Z","shell.execute_reply":"2024-11-25T07:24:42.083786Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_sub['id'] = df_test['id']","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:42.086062Z","iopub.execute_input":"2024-11-25T07:24:42.086363Z","iopub.status.idle":"2024-11-25T07:24:42.099693Z","shell.execute_reply.started":"2024-11-25T07:24:42.086334Z","shell.execute_reply":"2024-11-25T07:24:42.098643Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test = df_test.drop(['hour','minute','second','id','time'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:42.100795Z","iopub.execute_input":"2024-11-25T07:24:42.101100Z","iopub.status.idle":"2024-11-25T07:24:42.130186Z","shell.execute_reply.started":"2024-11-25T07:24:42.101072Z","shell.execute_reply":"2024-11-25T07:24:42.129312Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='model'></a>\n# Model","metadata":{}},{"cell_type":"code","source":"# Check the features of the training set\nprint(df_train.columns)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:42.131458Z","iopub.execute_input":"2024-11-25T07:24:42.131803Z","iopub.status.idle":"2024-11-25T07:24:42.137437Z","shell.execute_reply.started":"2024-11-25T07:24:42.131762Z","shell.execute_reply":"2024-11-25T07:24:42.136299Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define the features we select\n\n# The trainning dataset feature we select\nselected_features = ['bg-0:00', 'bg-1:00', 'bg-2:00',\n                     'insulin-0:00', 'insulin-1:00', 'insulin-2:00',\n#                      'carbs-0:00', 'carbs-1:00', 'carbs-2:00',\n                     'hr-0:00', 'hr-1:00', 'hr-2:00',\n                     'cals-0:00', 'cals-1:00', 'cals-2:00',\n                     'steps-0:00', 'steps-1:00', 'steps-2:00',\n                     'hour_sin', 'hour_cos','bg+1:00']\n\n# The features of the test set should correspond to those of the training set.\nselected_features_1 = ['bg-0:00', 'bg-1:00', 'bg-2:00',\n                     'insulin-0:00', 'insulin-1:00', 'insulin-2:00',\n#                      'carbs-0:00', 'carbs-1:00', 'carbs-2:00',\n                     'hr-0:00', 'hr-1:00', 'hr-2:00',\n                     'cals-0:00', 'cals-1:00', 'cals-2:00',\n                     'steps-0:00', 'steps-1:00', 'steps-2:00',\n                     'hour_sin', 'hour_cos']\n\n# Create a new DataFrame with only the selected features from df_train\ndf_selected_train = df_train[selected_features]\n\n# Similarly, create a new DataFrame for df_test\ndf_selected_test = df_test[selected_features_1]\n","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:42.138773Z","iopub.execute_input":"2024-11-25T07:24:42.139052Z","iopub.status.idle":"2024-11-25T07:24:42.178347Z","shell.execute_reply.started":"2024-11-25T07:24:42.139025Z","shell.execute_reply":"2024-11-25T07:24:42.177185Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from xgboost import XGBRegressor\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import mean_squared_error\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\n\n# Initialize list to store models and predictions\nmodels = []\ntest_predictions = []\n\n# Define the custom scoring function (Root Mean Squared Error)\ndef rmse(y_true, y_pred):\n    return np.sqrt(mean_squared_error(y_true, y_pred))\n\n# Use KFold for cross-validation\nkf = KFold(n_splits=5, shuffle=True, random_state=42)\n\n# Features and target from training data (Make sure to remove 'bg+1:00' only from X)\nX = df_selected_train.drop('bg+1:00', axis=1)\ny = df_train['bg+1:00']\n\n# Ensure the test set only has the same features as the training set\nX_test = df_test[X.columns]  # Use the same columns as in training data\n\n# Train and store models, and make predictions\nfor train_index, val_index in kf.split(X):\n    X_train, X_val = X.iloc[train_index], X.iloc[val_index]\n    y_train, y_val = y.iloc[train_index], y.iloc[val_index]\n    \n    # Train the model on the training set\n    model = XGBRegressor(n_estimators=100, learning_rate=0.1, random_state=42)\n    model.fit(X_train, y_train)\n    \n    # Make predictions on the test set (use X_test which has the same features as X)\n    y_test_pred = model.predict(X_test)\n    test_predictions.append(y_test_pred)\n\n# Take the mean of predictions from all 5 models\nfinal_predictions = np.mean(test_predictions, axis=0)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:42.179671Z","iopub.execute_input":"2024-11-25T07:24:42.180001Z","iopub.status.idle":"2024-11-25T07:24:47.417886Z","shell.execute_reply.started":"2024-11-25T07:24:42.179970Z","shell.execute_reply":"2024-11-25T07:24:47.417017Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_sub['bg+1:00'] = final_predictions","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:47.418838Z","iopub.execute_input":"2024-11-25T07:24:47.419155Z","iopub.status.idle":"2024-11-25T07:24:47.427373Z","shell.execute_reply.started":"2024-11-25T07:24:47.419123Z","shell.execute_reply":"2024-11-25T07:24:47.426233Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_sub.head(20)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:47.428576Z","iopub.execute_input":"2024-11-25T07:24:47.431667Z","iopub.status.idle":"2024-11-25T07:24:47.457631Z","shell.execute_reply.started":"2024-11-25T07:24:47.431607Z","shell.execute_reply":"2024-11-25T07:24:47.456510Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import xgboost as xgb\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.metrics import make_scorer, mean_squared_error, mean_absolute_error, mean_squared_log_error\n\n# Custom scoring indicators\nrmse_scorer = make_scorer(mean_squared_error, squared=False)\nmse_scorer = make_scorer(mean_squared_error, squared=True)\nmae_scorer = make_scorer(mean_absolute_error)\nrmsle_scorer = make_scorer(lambda y_true, y_pred: np.sqrt(mean_squared_log_error(y_true, np.clip(y_pred, 0, None))))\n\n# Evaluate the model using cross validation\nscores_train = cross_val_score(model, X_train, y_train, cv=5, scoring=rmse_scorer)\nmse_scores_train = cross_val_score(model, X_train, y_train, cv=5, scoring=mse_scorer)\nmae_scores_train = cross_val_score(model, X_train, y_train, cv=5, scoring=mae_scorer)\nrmsle_scores_train = cross_val_score(model, X_train, y_train, cv=5, scoring=rmsle_scorer)\n\n# Create a DataFrame to store the cross validation results\ncv_results_df = pd.DataFrame({\n    'Fold': range(1, 6),\n    'RMSE': scores_train,\n    'MSE': mse_scores_train,\n    'MAE': mae_scores_train,\n    'RMSLE': rmsle_scores_train\n})\n\n# Calculate the average of the cross-validation results and add them to the DataFrame\ncv_results_df.loc['Average'] = cv_results_df.mean()\n\n# Make predictions on the training set and calculate indicators\ny_train_pred = model.predict(X_train)\nrmse_train = mean_squared_error(y_train, y_train_pred, squared=False)\nmse_train = mean_squared_error(y_train, y_train_pred, squared=True)\nmae_train = mean_absolute_error(y_train, y_train_pred)\nrmsle_train = np.sqrt(mean_squared_log_error(y_train, np.clip(y_train_pred, 0, None)))\nmean_residual_deviance = np.mean((y_train - y_train_pred) ** 2)\n\n# Create a DataFrame to store the metrics on the training set\ntrain_results_df = pd.DataFrame({\n    'Metric': ['RMSE', 'MSE', 'MAE', 'RMSLE', 'MRD'], # MRD:Mean Residual Deviance\n    'Value': [rmse_train, mse_train, mae_train, rmsle_train, mean_residual_deviance]\n})\n\n# Print cross validation results\nprint(\"Cross-Validation Results:\")\nprint(cv_results_df)\n\n# Print the indicators on the training set\nprint(\"\\nTraining Set Results:\")\nprint(train_results_df)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:24:47.459508Z","iopub.execute_input":"2024-11-25T07:24:47.459993Z","iopub.status.idle":"2024-11-25T07:25:04.628030Z","shell.execute_reply.started":"2024-11-25T07:24:47.459958Z","shell.execute_reply":"2024-11-25T07:25:04.626945Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Draw a line chart\nplt.figure(figsize=(12, 6))\n\n# Plot cross validation results\nplt.subplot(1, 2, 1)\nplt.plot(cv_results_df.index[:-1], cv_results_df['RMSE'][:-1], marker='o', label='RMSE')\nplt.plot(cv_results_df.index[:-1], cv_results_df['MSE'][:-1], marker='o', label='MSE')\nplt.plot(cv_results_df.index[:-1], cv_results_df['MAE'][:-1], marker='o', label='MAE')\nplt.plot(cv_results_df.index[:-1], cv_results_df['RMSLE'][:-1], marker='o', label='RMSLE')\n# plt.axhline(y=cv_results_df.loc['Average', 'RMSE'], color='r', linestyle='--', label='Avg RMSE')\n# plt.axhline(y=cv_results_df.loc['Average', 'MSE'], color='g', linestyle='--', label='Avg MSE')\n# plt.axhline(y=cv_results_df.loc['Average', 'MAE'], color='b', linestyle='--', label='Avg MAE')\n# plt.axhline(y=cv_results_df.loc['Average', 'RMSLE'], color='y', linestyle='--', label='Avg RMSLE')\nplt.title('Cross-Validation Results')\nplt.xlabel('Fold')\nplt.ylabel('Value')\nplt.legend()\n\n# Plot indicators on the training set\nplt.subplot(1, 2, 2)\nplt.bar(train_results_df['Metric'], train_results_df['Value'], color=['r', 'g', 'b', 'y', 'c'])\nplt.title('Training Set Results')\nplt.xlabel('Metric')\nplt.ylabel('Value')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:25:04.629229Z","iopub.execute_input":"2024-11-25T07:25:04.629577Z","iopub.status.idle":"2024-11-25T07:25:05.239542Z","shell.execute_reply.started":"2024-11-25T07:25:04.629546Z","shell.execute_reply":"2024-11-25T07:25:05.238351Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict training set results\ny_train_pred = model.predict(X_train)\n\n# Draw a histogram of the training set prediction results\nplt.figure(figsize=(10, 4))\nplt.hist(y_train_pred, bins=25, color='lightcoral')\nplt.title('Predictions on Trainset')\nplt.grid(True)\nplt.xlabel('Predicted Values')\nplt.ylabel('Frequency')\nplt.show()\n\n# Output basic statistics of the training set prediction results\ny_train_pred_df = pd.DataFrame(y_train_pred, columns=['Predictions'])\nprint(y_train_pred_df.describe())\n","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:25:05.240841Z","iopub.execute_input":"2024-11-25T07:25:05.241179Z","iopub.status.idle":"2024-11-25T07:25:05.689801Z","shell.execute_reply.started":"2024-11-25T07:25:05.241142Z","shell.execute_reply":"2024-11-25T07:25:05.688630Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Calculate the residual of the training set (true value minus predicted value)\ndf_train['residual'] = y_train - y_train_pred\n\n# Draw the residual graph of the training set\nplt.figure(figsize=(14, 3))\nplt.scatter(df_train.index, df_train['residual'], color='darkblue', \n            alpha=0.25, s=1)\nplt.title('Residuals - Training')\nplt.grid()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:25:05.691573Z","iopub.execute_input":"2024-11-25T07:25:05.692008Z","iopub.status.idle":"2024-11-25T07:25:06.082556Z","shell.execute_reply.started":"2024-11-25T07:25:05.691958Z","shell.execute_reply":"2024-11-25T07:25:06.081331Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(14,3))\n# Filter the first 1000 records\ndf_sample = df_train.head(1000)\n\n# Draw a scatter plot\nplt.scatter(df_sample.index, df_sample.residual, color=default_color_1, \n            alpha=0.25, s=1)\nplt.title('Residuals - Training (First 1000 samples)')\nplt.grid()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:25:06.084203Z","iopub.execute_input":"2024-11-25T07:25:06.084682Z","iopub.status.idle":"2024-11-25T07:25:06.319509Z","shell.execute_reply.started":"2024-11-25T07:25:06.084635Z","shell.execute_reply":"2024-11-25T07:25:06.318353Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Select the features to analyze, for example 'bg-0:00'\nf = 'bg-0:00'\n\n# Draw a scatter plot of features and residuals\nplt.figure(figsize=(14, 3))\nplt.scatter(df_train[f], df_train['residual'], color='darkblue', \n            alpha=0.25, s=1)\nplt.xlabel(f)\nplt.ylabel('Residual')\nplt.title(f'Residuals vs {f} - Training')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:25:06.320745Z","iopub.execute_input":"2024-11-25T07:25:06.321055Z","iopub.status.idle":"2024-11-25T07:25:06.628478Z","shell.execute_reply.started":"2024-11-25T07:25:06.321026Z","shell.execute_reply":"2024-11-25T07:25:06.627356Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Select the features to analyze, for example 'bg-0:00'\nf = 'bg-0:00'\n\n# Filter the first 1000 records\ndf_sample = df_train.head(1000)\n\n# Draw a scatter plot of features and residuals for the selected 1000 rows\nplt.figure(figsize=(14, 3))\nplt.scatter(df_sample[f], df_sample['residual'], color='darkblue', \n            alpha=0.25, s=1)\nplt.xlabel(f)\nplt.ylabel('Residual')\nplt.title(f'Residuals vs {f} - Training (First 1000 samples)')\nplt.grid()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:25:06.629773Z","iopub.execute_input":"2024-11-25T07:25:06.630090Z","iopub.status.idle":"2024-11-25T07:25:06.886190Z","shell.execute_reply.started":"2024-11-25T07:25:06.630058Z","shell.execute_reply":"2024-11-25T07:25:06.884848Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Use the previous prediction results final_predictions\npred_test = final_predictions  # Assuming final_predictions is the result from your test set\n\n# Draw a histogram of the prediction results\nplt.figure(figsize=(10, 4))\nplt.hist(pred_test, bins=25, color='skyblue')  # You can replace 'skyblue' with your default_color_2\nplt.title('Predictions on Testset')\nplt.grid(True)\nplt.xlabel('Predicted Values')\nplt.ylabel('Frequency')\nplt.show()\n\n# Print basic statistics of prediction results\npred_test_df = pd.DataFrame(pred_test, columns=['Predictions'])\nprint(pred_test_df.describe())\n","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:25:06.887928Z","iopub.execute_input":"2024-11-25T07:25:06.888431Z","iopub.status.idle":"2024-11-25T07:25:07.212691Z","shell.execute_reply.started":"2024-11-25T07:25:06.888379Z","shell.execute_reply":"2024-11-25T07:25:07.211045Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_sub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T07:25:07.213989Z","iopub.execute_input":"2024-11-25T07:25:07.214303Z","iopub.status.idle":"2024-11-25T07:25:07.229701Z","shell.execute_reply.started":"2024-11-25T07:25:07.214252Z","shell.execute_reply":"2024-11-25T07:25:07.228313Z"},"trusted":true},"outputs":[],"execution_count":null}]}