{"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":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Introduction\n\nIf you take a look at distributions between the target `bg+1:00` and `bg-0:00`, we see some very strong similarities. The two distributions look very much the same. Let's take a look:","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore', category=DeprecationWarning)\nwarnings.filterwarnings('ignore', category=FutureWarning)\nwarnings.filterwarnings(\"ignore\", \"is_categorical_dtype\")\nwarnings.filterwarnings(\"ignore\", \"use_inf_as_na\")\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport gc\nimport plotly.express as px\nimport plotly.graph_objects as go\n\nfrom colorama import Fore\nfrom plotly.subplots import make_subplots\n\ntrain = pd.read_csv(\"../input/brist1d/train.csv\")\ntest = pd.read_csv(\"../input/brist1d/test.csv\")\nsubmission = pd.read_csv(\"../input/brist1d/sample_submission.csv\")\n\nsns.set_style('darkgrid')\n\nfig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 5))\n\n_ = sns.histplot(data=train, x=\"bg-0:00\", kde=True, ax=ax, label=\"bg-0:00\")\n_ = sns.histplot(data=train, x=\"bg+1:00\", kde=True, ax=ax, label=\"bg+1:00\")\n_ = ax.set_ylabel(\"\")\n_ = ax.set_xlabel(\"\")\n_ = ax.set_title(\"Blood Glucose Levels\", fontsize=15)\n_ = ax.legend(loc=\"upper right\")","metadata":{"execution":{"iopub.status.busy":"2024-10-24T20:42:16.684342Z","iopub.execute_input":"2024-10-24T20:42:16.685260Z","iopub.status.idle":"2024-10-24T20:42:38.336493Z","shell.execute_reply.started":"2024-10-24T20:42:16.685212Z","shell.execute_reply":"2024-10-24T20:42:38.335399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In fact, if we look at their statistical properties, we see something quite interesting:","metadata":{}},{"cell_type":"code","source":"bg_features = [\n    \"bg-0:00\", \"bg+1:00\",\n]\n\ntrain[bg_features].describe().T.style.bar(subset=['mean'], color='#7BCC70')\\\n    .background_gradient(subset=['std'], cmap='Reds')\\\n    .background_gradient(subset=['50%'], cmap='coolwarm')","metadata":{"execution":{"iopub.status.busy":"2024-10-24T20:43:09.148122Z","iopub.execute_input":"2024-10-24T20:43:09.148539Z","iopub.status.idle":"2024-10-24T20:43:09.381112Z","shell.execute_reply.started":"2024-10-24T20:43:09.148484Z","shell.execute_reply":"2024-10-24T20:43:09.379982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So we see their standard deviation, mean, min, max, and quantiles look very very similar. This suggests that our blood glucose at time 0:00 is very similar to +1:00. So, let's build a simple predictor! For this, we'll make it just the value at `bg-0:00`. If that value is missing, use the one at `bg-0:05`. If _that_ one is missing, then use `bg-0:10`, otherwise use the mean of `bg-0:00`. Let's do that, and then calculate what our $RMSE$ value would be.","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\ntrain[\"pred\"] = train[[\"bg-0:00\", \"bg-0:05\"]].apply(\n    lambda x: x[\"bg-0:00\"] if not np.isnan(x[\"bg-0:00\"]) else x[\"bg-0:05\"] if not np.isnan(x[\"bg-0:05\"]) else 8.277, \n    axis=1\n)\nrmse = mean_squared_error(train[\"bg+1:00\"], train[\"pred\"], squared=False)\nprint(f\"{Fore.RED}-->{Fore.RESET} RMSE = {Fore.GREEN}{rmse}{Fore.RESET}\")","metadata":{"execution":{"iopub.status.busy":"2024-10-24T20:54:30.190349Z","iopub.execute_input":"2024-10-24T20:54:30.191791Z","iopub.status.idle":"2024-10-24T20:54:33.640318Z","shell.execute_reply.started":"2024-10-24T20:54:30.191744Z","shell.execute_reply":"2024-10-24T20:54:33.639178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Interseting. Okay, let's create a sample submission using the same approach!","metadata":{}},{"cell_type":"code","source":"mean_value = test[\"bg-0:00\"].mean()\nsubmission[\"bg+1:00\"] = test[[\"bg-0:00\", \"bg-0:05\", \"bg-0:10\"]].apply(\n    lambda x: x[\"bg-0:00\"] if not np.isnan(x[\"bg-0:00\"]) else x[\"bg-0:05\"] if not np.isnan(x[\"bg-0:05\"]) else x[\"bg-0:10\"] if not np.isnan(x[\"bg-0:10\"]) else mean_value, \n    axis=1\n)\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-24T20:54:41.540575Z","iopub.execute_input":"2024-10-24T20:54:41.541257Z","iopub.status.idle":"2024-10-24T20:54:41.630367Z","shell.execute_reply.started":"2024-10-24T20:54:41.541198Z","shell.execute_reply":"2024-10-24T20:54:41.629406Z"},"trusted":true},"execution_count":null,"outputs":[]}]}