{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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"},{"sourceId":199096122,"sourceType":"kernelVersion"}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"colab":{"provenance":[{"file_id":"1XOQskHK6k5XE08TYbT9l5bv7iU93_qHf","timestamp":1745112883045},{"file_id":"https://storage.googleapis.com/kaggle-colab-exported-notebooks/hassanabdulrazeq/predicting-future-blood-glucose-levels-from-body-s.8b757259-6f7d-435a-a6f4-f8babe6a5976.ipynb?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=gcp-kaggle-com%40kaggle-161607.iam.gserviceaccount.com/20250415/auto/storage/goog4_request&X-Goog-Date=20250415T215924Z&X-Goog-Expires=259200&X-Goog-SignedHeaders=host&X-Goog-Signature=2ab0f6b5d714be3af9fe4ce26ace153cd1b8163cf81daa7590acb01e10a8adcec3218a19393efee0d7ae0ca4f1075ab69cc4da6fe9b3b34106e4e765cdc9faf3c8300775d4bff322afc4c6702d57edcc5f6da2ecdb3018d3b0421d1b7ea4c84e2ae40d6178f468b5facf0a2f55583b606b5d8d27737717a1481503f04cc8d134eac0a0eb4898d89a2a2cb4c719e6d8159e08f4eb641076204356a1ada10fdc0e3356ebd277d7e587e432b9c1cb454a0e7f33784e144c792e12c5351c7db4dbb5eac274b4628ba96c0092f6ef814828549942ed4951555f245c86caacfd5b1d221798dd406b61a1b0192cf563a1a3eaee1ccd31bcc4a1988d479fdf4b6993d1ef","timestamp":1744758768561}]},"widgets":{"application/vnd.jupyter.widget-state+json":{"d4de18036e634151afa5715a763da73d":{"model_module":"@jupyter-widgets/controls","model_name":"VBoxModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"VBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"VBoxView","box_style":"","children":["IPY_MODEL_fcf89281a57d41899ad8e19802864384","IPY_MODEL_d4657021b15446b58ac72633caf9f9a0","IPY_MODEL_e329959803e0431ebac5c133deff8d38","IPY_MODEL_5e6dcb616c0044f98883dcdd74e84b28","IPY_MODEL_cad7eb9c95c24c48bdf98f4535929eb6"],"layout":"IPY_MODEL_017bdd491cd343b49788c4c0585203d6"}},"fcf89281a57d41899ad8e19802864384":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_977d266b1e174bddb46000842a2d50f6","placeholder":"​","style":"IPY_MODEL_360803a9eb0f4434871faa6f3092d4f8","value":"<center> <img\nsrc=https://www.kaggle.com/static/images/site-logo.png\nalt='Kaggle'> <br> Create an API token from <a\nhref=\"https://www.kaggle.com/settings/account\" target=\"_blank\">your Kaggle\nsettings page</a> and paste it below along with your Kaggle username. <br> </center>"}},"d4657021b15446b58ac72633caf9f9a0":{"model_module":"@jupyter-widgets/controls","model_name":"TextModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"TextModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"TextView","continuous_update":true,"description":"Username:","description_tooltip":null,"disabled":false,"layout":"IPY_MODEL_1a16ed895b96424b8f49cd5cc5d1f5f9","placeholder":"​","style":"IPY_MODEL_89cc1930c9cc40f9a1b43858f5feb80a","value":""}},"e329959803e0431ebac5c133deff8d38":{"model_module":"@jupyter-widgets/controls","model_name":"PasswordModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"PasswordModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"PasswordView","continuous_update":true,"description":"Token:","description_tooltip":null,"disabled":false,"layout":"IPY_MODEL_fe63dd7c573c408492e77a5d9e058699","placeholder":"​","style":"IPY_MODEL_9b318a1d96104063a9039dfd5106ae17","value":""}},"5e6dcb616c0044f98883dcdd74e84b28":{"model_module":"@jupyter-widgets/controls","model_name":"ButtonModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ButtonModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"ButtonView","button_style":"","description":"Login","disabled":false,"icon":"","layout":"IPY_MODEL_5761dcc6331846368061f6532babf445","style":"IPY_MODEL_54919feb162f44259286599edb0e0988","tooltip":""}},"cad7eb9c95c24c48bdf98f4535929eb6":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_049df65fc4e94fd786d499ec3a512a32","placeholder":"​","style":"IPY_MODEL_6cfe3c32855349a1964d0ea424bf8220","value":"\n<b>Thank You</b></center>"}},"017bdd491cd343b49788c4c0585203d6":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":"center","align_self":null,"border":null,"bottom":null,"display":"flex","flex":null,"flex_flow":"column","grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":"50%"}},"977d266b1e174bddb46000842a2d50f6":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"360803a9eb0f4434871faa6f3092d4f8":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"1a16ed895b96424b8f49cd5cc5d1f5f9":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"89cc1930c9cc40f9a1b43858f5feb80a":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"fe63dd7c573c408492e77a5d9e058699":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"9b318a1d96104063a9039dfd5106ae17":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"5761dcc6331846368061f6532babf445":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"54919feb162f44259286599edb0e0988":{"model_module":"@jupyter-widgets/controls","model_name":"ButtonStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ButtonStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","button_color":null,"font_weight":""}},"049df65fc4e94fd786d499ec3a512a32":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"6cfe3c32855349a1964d0ea424bf8220":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}}}}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 📈 Project Title: Predicting Future Blood Glucose Levels from Body Signals\n\n## 🧠 Problem Description\n\nThis project aims to **predict the blood glucose level one hour into the future** using body signal data collected from people with type 1 diabetes. These people used medical and wearable devices like:\n\n- Continuous Glucose Monitor (CGM)\n- Insulin Pump\n- Smartwatch\n\nThe idea is to use the last 6 hours of data to build a model that can forecast the glucose level 1 hour ahead. This is a **regression problem** because the output is a real number (glucose level in mmol/L).\n\n---\n\n## 📊 Dataset Description\n\nThe dataset contains time-series data collected from real patients. Each row represents a specific point in time and includes all signals from the **previous 6 hours**, split into 5-minute intervals.\n\n### ✅ Input Features Include:\n\n- `bg-X:XX`: blood glucose levels from the past\n- `hr-X:XX`: heart rate (beats per minute)\n- `steps-X:XX`: steps taken\n- `cals-X:XX`: calories burned\n- `insulin-X:XX`: insulin doses\n- `carbs-X:XX`: carbohydrate intake\n- `activity-X:XX`: activity label (like walking, sleeping...)\n\nEach \"X:XX\" refers to how far back in time the signal was recorded (e.g., `bg-2:35` = glucose 2h 35min ago).\n\n### 🎯 Target Variable:\n\n- `bg+1:00`: glucose value **1 hour in the future** (this is what we want to predict)\n\n---\n\n## 📁 Files Overview\n\n| File Name              | Description |\n|------------------------|-------------|\n| `train.csv`            | Main file for training. Includes all input features and the target (`bg+1:00`). |\n| `test.csv`             | File for testing the model. It does **not** include the target column. |\n| `sample_submission.csv`| Template to show how predictions should be submitted. |\n| `activities.txt`       | List of possible activity labels used in `activity-X:XX` columns. |\n\n---\n\n## ⚙️ Problem Type\n\nThis is a **regression task**, not classification.\n\n- Input = past signals (glucose, heart rate, insulin, activity...)\n- Output = real number (future glucose level)\n\n---\n\n## 🔍 Challenges in the Data\n\n- Some missing values (real-world medical data)\n- Different devices used by different patients\n- Participants in the test set are different from those in the train set\n\n---\n\n## 🚀 Goal\n\nBuild a machine learning model that can predict a person's glucose level **1 hour ahead** based on their recent body signals. This can help improve diabetes care and early intervention.\n\n","metadata":{"id":"faRhEURidvp8"}},{"cell_type":"markdown","source":"## 📌 Step 1: Importing Required Libraries\n\nIn this step, we import all the Python libraries we need to load the data, handle missing values, explore the dataset, and later build machine learning models.\n\n### 🧰 Libraries Used:\n\n- `pandas` → for loading and working with tabular data (CSV files)\n- `numpy` → for numerical operations and handling arrays\n- `matplotlib.pyplot` & `seaborn` → for data visualization\n- `sklearn` → for building and evaluating machine learning models\n- `warnings` → to ignore unnecessary warning messages during exploration\n\nMore\n","metadata":{"id":"Pljcldgldvp_"}},{"cell_type":"code","source":"# Data Handling\nimport pandas as pd\nimport numpy as np\n\n# Visualization\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Signal Processing\nfrom scipy.signal import butter, filtfilt\n\n# Machine Learning\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.preprocessing import MinMaxScaler, LabelEncoder\nfrom sklearn.metrics import mean_squared_error, r2_score\n\n# Warnings\nimport warnings\nwarnings.filterwarnings('ignore')\n","metadata":{"id":"Q60r-EzY15MV","executionInfo":{"status":"ok","timestamp":1745122643168,"user_tz":-120,"elapsed":139,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:03.679855Z","iopub.execute_input":"2025-04-25T17:54:03.680147Z","iopub.status.idle":"2025-04-25T17:54:07.388103Z","shell.execute_reply.started":"2025-04-25T17:54:03.680115Z","shell.execute_reply":"2025-04-25T17:54:07.387084Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### ✅ Step 1: Understand the Data\n\n- Load the dataset and check its shape and structure.\n- Explore the signal columns and target column.\n- Identify columns with missing values.\n- Check for all-zero signal rows or duplicate rows.\n","metadata":{"id":"I-QvYIJf2XSG"}},{"cell_type":"code","source":"# Load the dataset\ntrain_df = pd.read_csv('/kaggle/input/brist1d/train.csv')\n\ntrain_df","metadata":{"id":"gjnZ8Drs2R5N","executionInfo":{"status":"ok","timestamp":1745122661308,"user_tz":-120,"elapsed":18137,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"a0ae8e4e-c487-4cf7-a784-5cbaaba2b842","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:07.389808Z","iopub.execute_input":"2025-04-25T17:54:07.390389Z","iopub.status.idle":"2025-04-25T17:54:20.57091Z","shell.execute_reply.started":"2025-04-25T17:54:07.390363Z","shell.execute_reply":"2025-04-25T17:54:20.570151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Load the dataset\nsample_submission_df = pd.read_csv('/kaggle/input/brist1d/test.csv')\n\nsample_submission_df","metadata":{"id":"lPOvxLC5DZ-u","executionInfo":{"status":"ok","timestamp":1745122661349,"user_tz":-120,"elapsed":43,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"eb135d94-550c-4562-dc1f-ec1f35cc634a","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:20.571663Z","iopub.execute_input":"2025-04-25T17:54:20.571888Z","iopub.status.idle":"2025-04-25T17:54:20.826424Z","shell.execute_reply.started":"2025-04-25T17:54:20.571871Z","shell.execute_reply":"2025-04-25T17:54:20.825684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df=pd.read_csv('/kaggle/input/brist1d/sample_submission.csv')\ntest_df","metadata":{"id":"smtZn9GQD0ol","executionInfo":{"status":"ok","timestamp":1745122661646,"user_tz":-120,"elapsed":296,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"c2841b7c-835e-4ea0-c312-485b9aa6b1b9","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:20.827416Z","iopub.execute_input":"2025-04-25T17:54:20.827817Z","iopub.status.idle":"2025-04-25T17:54:20.845052Z","shell.execute_reply.started":"2025-04-25T17:54:20.827772Z","shell.execute_reply":"2025-04-25T17:54:20.84426Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Shape of the dataset (rows, columns)\ntrain_df.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:20.847252Z","iopub.execute_input":"2025-04-25T17:54:20.847553Z","iopub.status.idle":"2025-04-25T17:54:20.853311Z","shell.execute_reply.started":"2025-04-25T17:54:20.847532Z","shell.execute_reply":"2025-04-25T17:54:20.85258Z"},"id":"oN9mMPwCdvqE","executionInfo":{"status":"ok","timestamp":1745122661659,"user_tz":-120,"elapsed":10,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"466bd265-5332-4985-c8d3-a0bbb85afb91"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Display the first 5 rows\nprint(\"\\nFirst 5 rows of the dataset:\")\ntrain_df.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:20.854147Z","iopub.execute_input":"2025-04-25T17:54:20.854425Z","iopub.status.idle":"2025-04-25T17:54:20.884997Z","shell.execute_reply.started":"2025-04-25T17:54:20.854395Z","shell.execute_reply":"2025-04-25T17:54:20.884193Z"},"id":"QUt3ZgnYdvqF","executionInfo":{"status":"ok","timestamp":1745122661740,"user_tz":-120,"elapsed":79,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"3c2368b1-df94-46d2-80d6-74aee30fc4bf"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Shape of the dataset (rows, columns)\ntrain_df.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:20.886024Z","iopub.execute_input":"2025-04-25T17:54:20.886317Z","iopub.status.idle":"2025-04-25T17:54:20.899712Z","shell.execute_reply.started":"2025-04-25T17:54:20.886296Z","shell.execute_reply":"2025-04-25T17:54:20.898849Z"},"executionInfo":{"status":"ok","timestamp":1745122661753,"user_tz":-120,"elapsed":11,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"f985ad03-9a14-45bc-ea81-ecf7bfabb8ab","id":"8mwIDUTY3GnA"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Display the first 5 rows\nprint(\"\\nFirst 5 rows of the dataset:\")\ntrain_df.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:20.900659Z","iopub.execute_input":"2025-04-25T17:54:20.900952Z","iopub.status.idle":"2025-04-25T17:54:20.930258Z","shell.execute_reply.started":"2025-04-25T17:54:20.900928Z","shell.execute_reply":"2025-04-25T17:54:20.929528Z"},"executionInfo":{"status":"ok","timestamp":1745122661827,"user_tz":-120,"elapsed":73,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"0c671838-b67f-42f6-ecb7-492ffc9c5053","id":"ZvnTgAck3GnA"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"signal_cols = [\n    'hr-0:05',\n    'bg-0:05',\n    'steps-0:05',\n    'cals-0:05',\n    'insulin-0:05',\n    'cals-0:15'\n]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:20.931149Z","iopub.execute_input":"2025-04-25T17:54:20.931366Z","iopub.status.idle":"2025-04-25T17:54:20.935712Z","shell.execute_reply.started":"2025-04-25T17:54:20.93135Z","shell.execute_reply":"2025-04-25T17:54:20.93482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(signal_cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:20.936714Z","iopub.execute_input":"2025-04-25T17:54:20.937022Z","iopub.status.idle":"2025-04-25T17:54:20.953428Z","shell.execute_reply.started":"2025-04-25T17:54:20.936996Z","shell.execute_reply":"2025-04-25T17:54:20.952593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[:100]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:20.95442Z","iopub.execute_input":"2025-04-25T17:54:20.954754Z","iopub.status.idle":"2025-04-25T17:54:20.984291Z","shell.execute_reply.started":"2025-04-25T17:54:20.954728Z","shell.execute_reply":"2025-04-25T17:54:20.983367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_signals_slice(df, signal_cols, start=0, length=500, title=\"Signals Slice\"):\n    end = start + length\n    sliced_df = df[signal_cols].iloc[start:end]\n    \n    \n    n=len(signal_cols)\n    plt.figure(figsize=(20, n * 4))\n    for i, col in enumerate(signal_cols, 1):\n        plt.subplot(n ,2, i)\n        sns.lineplot(data=sliced_df[col], color='blue', estimator=None, lw=1, markers=True, dashes=False)\n    \n        plt.title(title)\n        plt.xlabel(\"Sample\")\n        plt.ylabel(\"Signal Value\")\n        plt.legend(loc='upper right', bbox_to_anchor=(1.15, 1))\n        plt.grid(True, linestyle='--', alpha=0.6)\n        plt.legend(loc='upper right', fontsize=12)\n        plt.tight_layout()\n   \n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:20.985309Z","iopub.execute_input":"2025-04-25T17:54:20.986283Z","iopub.status.idle":"2025-04-25T17:54:20.999817Z","shell.execute_reply.started":"2025-04-25T17:54:20.986258Z","shell.execute_reply":"2025-04-25T17:54:20.998843Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_signals_slice(train_df, signal_cols, start=0, length=500, title=\"Signals Overview\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:21.000594Z","iopub.execute_input":"2025-04-25T17:54:21.000878Z","iopub.status.idle":"2025-04-25T17:54:22.836332Z","shell.execute_reply.started":"2025-04-25T17:54:21.000857Z","shell.execute_reply":"2025-04-25T17:54:22.835384Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### ✅ Step 2: Handle Missing Values\n\n- Avoid using the mean to fill missing values in time-series signals.\n- Use **linear interpolation** to estimate missing values based on signal trends.\n- This maintains the natural shape of the signal over time.\n","metadata":{"id":"G5x8zYhidvqJ"}},{"cell_type":"code","source":"(train_df.isna().sum()/ len(train_df))*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:22.839983Z","iopub.execute_input":"2025-04-25T17:54:22.840266Z","iopub.status.idle":"2025-04-25T17:54:23.506178Z","shell.execute_reply.started":"2025-04-25T17:54:22.840245Z","shell.execute_reply":"2025-04-25T17:54:23.505252Z"},"id":"PT_rxrD8dvqJ","executionInfo":{"status":"ok","timestamp":1745122662300,"user_tz":-120,"elapsed":447,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"cccc79fe-a393-433e-a248-f28e6ee9f8b4"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.isna().mean()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:23.507364Z","iopub.execute_input":"2025-04-25T17:54:23.507716Z","iopub.status.idle":"2025-04-25T17:54:24.178212Z","shell.execute_reply.started":"2025-04-25T17:54:23.507688Z","shell.execute_reply":"2025-04-25T17:54:24.177498Z"},"id":"nqxQlw4_dvqK","executionInfo":{"status":"ok","timestamp":1745122662883,"user_tz":-120,"elapsed":580,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"8e743009-e3af-43f0-a85f-565537d5bf01"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataIsNall=train_df.columns[(train_df.isna().sum()/ len(train_df))>0.7]\ndataIsNall","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:24.179051Z","iopub.execute_input":"2025-04-25T17:54:24.179305Z","iopub.status.idle":"2025-04-25T17:54:24.855729Z","shell.execute_reply.started":"2025-04-25T17:54:24.179286Z","shell.execute_reply":"2025-04-25T17:54:24.853924Z"},"id":"RCs-XZvydvqK","executionInfo":{"status":"ok","timestamp":1745122663437,"user_tz":-120,"elapsed":574,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"d02459f1-8944-4f35-a6e4-94918ee32956"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data=train_df.drop(columns=dataIsNall)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:24.856816Z","iopub.execute_input":"2025-04-25T17:54:24.857148Z","iopub.status.idle":"2025-04-25T17:54:25.022199Z","shell.execute_reply.started":"2025-04-25T17:54:24.857121Z","shell.execute_reply":"2025-04-25T17:54:25.021302Z"},"id":"jEi4uuKGdvqL","executionInfo":{"status":"ok","timestamp":1745122663669,"user_tz":-120,"elapsed":203,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check which columns still have missing values\nstill_missing = data.columns[data.isna().sum() > 0]\nprint(f\"Columns with remaining missing values:\\n{still_missing}\")\n","metadata":{"id":"YfbHGP6Y3SOX","executionInfo":{"status":"ok","timestamp":1745122663828,"user_tz":-120,"elapsed":157,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"b2f816d0-b1c3-4f7f-dff6-dd4eb9bab131","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:25.023038Z","iopub.execute_input":"2025-04-25T17:54:25.023297Z","iopub.status.idle":"2025-04-25T17:54:25.283619Z","shell.execute_reply.started":"2025-04-25T17:54:25.02327Z","shell.execute_reply":"2025-04-25T17:54:25.282834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Select the signal (heart rate 5 minutes ago)\nsignal_col = 'hr-0:05'\n\n# Get the original signal (before cleaning)\noriginal_signal = train_df[signal_col].copy()\noriginal_signal","metadata":{"id":"b6_IB5LcGesA","executionInfo":{"status":"ok","timestamp":1745122663884,"user_tz":-120,"elapsed":27,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"5dbbe451-fd89-4f2e-997a-5376530f7630","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:25.284632Z","iopub.execute_input":"2025-04-25T17:54:25.284864Z","iopub.status.idle":"2025-04-25T17:54:25.293776Z","shell.execute_reply.started":"2025-04-25T17:54:25.284847Z","shell.execute_reply":"2025-04-25T17:54:25.292915Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Plot signal before interpolation\nplt.figure(figsize=(10, 4))\nplt.plot(original_signal, color='blue')\nplt.title(f\"{signal_col} - Before Interpolation\")\nplt.xlabel(\"Time\")\nplt.ylabel(\"Value\")\nplt.grid(True)\nplt.show()\n","metadata":{"id":"OJF54i5zGx1-","executionInfo":{"status":"ok","timestamp":1745122664447,"user_tz":-120,"elapsed":564,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"18da8835-27fa-4a6b-ba84-e2a9f7865a8b","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:25.294674Z","iopub.execute_input":"2025-04-25T17:54:25.294894Z","iopub.status.idle":"2025-04-25T17:54:25.598028Z","shell.execute_reply.started":"2025-04-25T17:54:25.294878Z","shell.execute_reply":"2025-04-25T17:54:25.596935Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Use interpolation for numeric columns with missing values\nfor col in still_missing:\n    if data[col].dtype != 'object':  # only numeric signals\n        data[col] = data[col].interpolate(method='linear', limit_direction='both')\n","metadata":{"id":"3oFMD35D4Bnm","executionInfo":{"status":"ok","timestamp":1745122719952,"user_tz":-120,"elapsed":5971,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:25.59902Z","iopub.execute_input":"2025-04-25T17:54:25.599285Z","iopub.status.idle":"2025-04-25T17:54:30.649509Z","shell.execute_reply.started":"2025-04-25T17:54:25.599255Z","shell.execute_reply":"2025-04-25T17:54:30.648507Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check total number of missing values now\nprint(\"Missing values after interpolation:\")\nprint(data.isna().sum().sum())  # should be 0\n","metadata":{"id":"cqNmdCll4GLJ","executionInfo":{"status":"ok","timestamp":1745122720207,"user_tz":-120,"elapsed":253,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"8fa40c1e-fa90-4a01-9cc7-454c7121d51d","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:30.650352Z","iopub.execute_input":"2025-04-25T17:54:30.650602Z","iopub.status.idle":"2025-04-25T17:54:30.936367Z","shell.execute_reply.started":"2025-04-25T17:54:30.650584Z","shell.execute_reply":"2025-04-25T17:54:30.935425Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:30.937403Z","iopub.execute_input":"2025-04-25T17:54:30.9377Z","iopub.status.idle":"2025-04-25T17:54:30.944595Z","shell.execute_reply.started":"2025-04-25T17:54:30.937679Z","shell.execute_reply":"2025-04-25T17:54:30.943397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_signals_slice(data, signal_cols, start=0, length=500, title=\"Signals After Cleaning\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:30.945704Z","iopub.execute_input":"2025-04-25T17:54:30.946104Z","iopub.status.idle":"2025-04-25T17:54:32.626617Z","shell.execute_reply.started":"2025-04-25T17:54:30.946071Z","shell.execute_reply":"2025-04-25T17:54:32.625395Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### ✅ Step 3: Remove Invalid Signal Rows\n\n- Some rows may have only zeros in signal columns (e.g., device was off).\n- These rows are likely invalid and should be removed from the data.\n\n","metadata":{"id":"TlHIsiM14Uh2"}},{"cell_type":"code","source":"# Get all signal columns (columns that start with hr-, bg-, steps-, cals-, insulin-, carbs-)\nsignal_cols = [col for col in data.columns if col.startswith(('hr-', 'bg-', 'steps-', 'cals-', 'insulin-', 'carbs-'))]\nsignal_cols","metadata":{"id":"T10WbtvM4dal","executionInfo":{"status":"ok","timestamp":1745122720209,"user_tz":-120,"elapsed":42,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"974aadc5-17c1-43a3-f16f-1f7b52b3eb7f","trusted":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:32.627677Z","iopub.execute_input":"2025-04-25T17:54:32.627942Z","iopub.status.idle":"2025-04-25T17:54:32.638028Z","shell.execute_reply.started":"2025-04-25T17:54:32.627922Z","shell.execute_reply":"2025-04-25T17:54:32.636787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Count how many signal values are zero in each row\nzero_counts_per_row = (data[signal_cols] == 0).sum(axis=1)\n","metadata":{"id":"bxuD1KMX4uh0","executionInfo":{"status":"ok","timestamp":1745122721200,"user_tz":-120,"elapsed":996,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:32.638993Z","iopub.execute_input":"2025-04-25T17:54:32.639512Z","iopub.status.idle":"2025-04-25T17:54:33.591026Z","shell.execute_reply.started":"2025-04-25T17:54:32.639482Z","shell.execute_reply":"2025-04-25T17:54:33.59Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Keep only rows where at least one signal is not zero\ndata = data\n\nprint(\"Rows after removing full-zero signals:\", len(data))\n","metadata":{"id":"HNNCRefT4y9F","executionInfo":{"status":"ok","timestamp":1745122721214,"user_tz":-120,"elapsed":12,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"387cc11b-4826-45a8-89af-fe1c0abefa87","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:33.592261Z","iopub.execute_input":"2025-04-25T17:54:33.592612Z","iopub.status.idle":"2025-04-25T17:54:33.597898Z","shell.execute_reply.started":"2025-04-25T17:54:33.592585Z","shell.execute_reply":"2025-04-25T17:54:33.596933Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# Pick a signal column to visualize\nsignal_col = 'hr-5:00'\n\n# Get the original signal before any filtering (from train_df)\noriginal_signal = train_df[signal_col]\n\n# Get the cleaned version (after handling missing and zero rows)\ncleaned_signal = data[signal_col]\n\n# Create side-by-side plots\nfig, axs = plt.subplots(1, 2, figsize=(14, 4), sharey=True)\n\n# Before cleaning\nsns.lineplot(data=original_signal[:500], ax=axs[0], color='blue')\naxs[0].set_title(f'{signal_col} - Before Cleaning')\naxs[0].set_xlabel('Time')\naxs[0].set_ylabel('Value')\naxs[0].grid(True)\n\n# After cleaning\nsns.lineplot(data=cleaned_signal[:500], ax=axs[1], color='green')\naxs[1].set_title(f'{signal_col} - After Cleaning')\naxs[1].set_xlabel('Time')\naxs[1].grid(True)\n\nplt.tight_layout()\nplt.show()\n","metadata":{"id":"8hpqEn0B5Juu","executionInfo":{"status":"ok","timestamp":1745122730410,"user_tz":-120,"elapsed":1388,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"cf71be4c-e990-40e9-e826-6f0cd18b2330","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:33.599015Z","iopub.execute_input":"2025-04-25T17:54:33.599665Z","iopub.status.idle":"2025-04-25T17:54:34.031978Z","shell.execute_reply.started":"2025-04-25T17:54:33.599642Z","shell.execute_reply":"2025-04-25T17:54:34.031122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Before cleaning:\", train_df.shape[0])\nprint(\"After cleaning :\", data.shape[0])\n","metadata":{"id":"CqEzD9og5pFN","executionInfo":{"status":"ok","timestamp":1745122730432,"user_tz":-120,"elapsed":19,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"3cfc1897-a828-47fe-eefb-35e6fa0482ed","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.033037Z","iopub.execute_input":"2025-04-25T17:54:34.033354Z","iopub.status.idle":"2025-04-25T17:54:34.038489Z","shell.execute_reply.started":"2025-04-25T17:54:34.033328Z","shell.execute_reply":"2025-04-25T17:54:34.037512Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# Pick a signal column to visualize\nsignal_col = 'hr-5:00'\n\n# Get the original signal before any filtering (from train_df)\noriginal_signal = train_df[signal_col]\n\n# Get the cleaned version (after handling missing and zero rows)\ncleaned_signal = data[signal_col]\n\n# Create side-by-side plots\nfig, axs = plt.subplots(1, 2, figsize=(14, 4), sharey=True)\n\n# Before cleaning\nsns.lineplot(data=original_signal[:500], ax=axs[0], color='blue')\naxs[0].set_title(f'{signal_col} - Before Cleaning')\naxs[0].set_xlabel('Time')\naxs[0].set_ylabel('Value')\naxs[0].grid(True)\n\n# After cleaning\nsns.lineplot(data=cleaned_signal[:500], ax=axs[1], color='green')\naxs[1].set_title(f'{signal_col} - After Cleaning')\naxs[1].set_xlabel('Time')\naxs[1].grid(True)\n\nplt.tight_layout()\nplt.show()\n","metadata":{"id":"u7LXM75u5yhX","executionInfo":{"status":"ok","timestamp":1745122733809,"user_tz":-120,"elapsed":841,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"3f893325-d16d-4d59-86ab-4429a4d827d7","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.039419Z","iopub.execute_input":"2025-04-25T17:54:34.03974Z","iopub.status.idle":"2025-04-25T17:54:34.488683Z","shell.execute_reply.started":"2025-04-25T17:54:34.039711Z","shell.execute_reply":"2025-04-25T17:54:34.48774Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n\n### ✅ Step 4: Convert Time Column\n\n- Convert the `time` column to datetime format.\n- This helps if we want to extract features like time of day, weekday, etc.\n\n","metadata":{"id":"11MhOC6n4acB"}},{"cell_type":"code","source":"# Convert the time column to datetime format\ndata['time'] = pd.to_datetime(data['time'], errors='coerce',format='%H:%M:%S')\n","metadata":{"id":"NRwX9bRAMto2","executionInfo":{"status":"ok","timestamp":1745122734910,"user_tz":-120,"elapsed":130,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.489725Z","iopub.execute_input":"2025-04-25T17:54:34.490047Z","iopub.status.idle":"2025-04-25T17:54:34.525803Z","shell.execute_reply.started":"2025-04-25T17:54:34.490027Z","shell.execute_reply":"2025-04-25T17:54:34.525063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Show first few values to confirm the format\nprint(data['time'].head())\n","metadata":{"id":"kwaqUm9iMxlI","executionInfo":{"status":"ok","timestamp":1745122736876,"user_tz":-120,"elapsed":27,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"774e0987-3927-4cac-b4f8-00a956de4d87","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.5266Z","iopub.execute_input":"2025-04-25T17:54:34.526867Z","iopub.status.idle":"2025-04-25T17:54:34.533031Z","shell.execute_reply.started":"2025-04-25T17:54:34.526846Z","shell.execute_reply":"2025-04-25T17:54:34.532214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Optional: create new columns from the datetime\ndata['hour'] = data['time'].dt.hour\ndata['day'] = data['time'].dt.day\ndata['weekday'] = data['time'].dt.weekday\n","metadata":{"id":"eVm2w04CM5g_","executionInfo":{"status":"ok","timestamp":1745122738403,"user_tz":-120,"elapsed":43,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.533995Z","iopub.execute_input":"2025-04-25T17:54:34.534312Z","iopub.status.idle":"2025-04-25T17:54:34.567978Z","shell.execute_reply.started":"2025-04-25T17:54:34.534284Z","shell.execute_reply":"2025-04-25T17:54:34.567287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"id":"YUufv_zhM7Km","executionInfo":{"status":"ok","timestamp":1745122740025,"user_tz":-120,"elapsed":69,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"8d397d94-044d-48cd-cf11-ac56524f9be7","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.568846Z","iopub.execute_input":"2025-04-25T17:54:34.569117Z","iopub.status.idle":"2025-04-25T17:54:34.594537Z","shell.execute_reply.started":"2025-04-25T17:54:34.569087Z","shell.execute_reply":"2025-04-25T17:54:34.593781Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n\n### ✅ Step 5: Filter and Smooth Signals\n\n- Body signals may contain noise or sharp spikes.\n- Apply a **low-pass filter** (e.g., Butterworth) to smooth the signals.\n- This step helps the model focus on real trends, not noise.\n\n","metadata":{"id":"FpPqqf-LMsnf"}},{"cell_type":"code","source":"# Define a Butterworth low-pass filter function\ndef butter_lowpass_filter(signal, cutoff=0.05, fs=1.0, order=3):\n    nyq = 0.5 * fs  # Nyquist Frequency\n    normal_cutoff = cutoff / nyq\n    b, a = butter(order, normal_cutoff, btype='low', analog=False)\n    return filtfilt(b, a, signal)\n","metadata":{"id":"lAXGnPrnNBEt","executionInfo":{"status":"ok","timestamp":1745122741760,"user_tz":-120,"elapsed":3,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.595422Z","iopub.execute_input":"2025-04-25T17:54:34.59569Z","iopub.status.idle":"2025-04-25T17:54:34.616092Z","shell.execute_reply.started":"2025-04-25T17:54:34.595671Z","shell.execute_reply":"2025-04-25T17:54:34.614867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Apply filter to a sample signal\nfiltered_hr = butter_lowpass_filter(data['hr-0:05'].fillna(method='ffill'))\n\n# Replace or create a new column\ndata['hr-0:05_filtered'] = filtered_hr\n","metadata":{"id":"E251TVZ4NZte","executionInfo":{"status":"ok","timestamp":1745122742997,"user_tz":-120,"elapsed":35,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.617074Z","iopub.execute_input":"2025-04-25T17:54:34.617477Z","iopub.status.idle":"2025-04-25T17:54:34.648523Z","shell.execute_reply.started":"2025-04-25T17:54:34.617423Z","shell.execute_reply":"2025-04-25T17:54:34.647482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nplt.figure(figsize=(14, 4))\nplt.plot(data['hr-0:05'][:500], label='Original', color='blue')\nplt.plot(data['hr-0:05_filtered'][:500], label='Filtered', color='green')\nplt.title(\"Heart Rate Signal - Before and After Filtering\")\nplt.xlabel(\"Time\")\nplt.ylabel(\"HR Value\")\nplt.legend()\nplt.grid(True)\nplt.show()\n","metadata":{"id":"HLgS3kIjNfkn","executionInfo":{"status":"ok","timestamp":1745122744639,"user_tz":-120,"elapsed":566,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"b79dae67-9129-4395-e971-ac24315d5d2e","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.649811Z","iopub.execute_input":"2025-04-25T17:54:34.650107Z","iopub.status.idle":"2025-04-25T17:54:34.915181Z","shell.execute_reply.started":"2025-04-25T17:54:34.650085Z","shell.execute_reply":"2025-04-25T17:54:34.914108Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n\n### ✅ Step 6: Normalize Signals\n\n- Signals have different scales (e.g., heart rate vs steps vs glucose).\n- Normalize all signal features to the same range (e.g., 0 to 1).\n- This ensures fair contribution to the model.\n\n\n","metadata":{"id":"mjQ6Zq55NIKv"}},{"cell_type":"markdown","source":"\n### ✅ Step 7: Select Features and Target\n\n- Input features: all past signal columns (e.g., `bg-*`, `hr-*`, etc.)\n- Target variable: `bg+1:00` (glucose level in 1 hour)\n\n","metadata":{"id":"Q6XXK3PdOCR-"}},{"cell_type":"code","source":"test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.916395Z","iopub.execute_input":"2025-04-25T17:54:34.917476Z","iopub.status.idle":"2025-04-25T17:54:34.927826Z","shell.execute_reply.started":"2025-04-25T17:54:34.917412Z","shell.execute_reply":"2025-04-25T17:54:34.926932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Target column (what we want to predict)\ntarget_col = 'bg+1:00'\n# Columns to exclude from features\nexclude_cols = ['id', 'p_num', 'time', target_col]\n\n# Create feature matrix X and target vector y\nX = data.drop(columns=exclude_cols, errors='ignore')\ny = data[target_col]\nprint(\"X shape:\", X.shape)\nprint(\"y shape:\", y.shape)\n\n# Show first few rows\nX.head()\n","metadata":{"id":"Mw9_L11TPC0-","executionInfo":{"status":"ok","timestamp":1745122788957,"user_tz":-120,"elapsed":825,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"22e4ad91-2aeb-4dfe-c982-1b6933bba885","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:34.929142Z","iopub.execute_input":"2025-04-25T17:54:34.929462Z","iopub.status.idle":"2025-04-25T17:54:35.482943Z","shell.execute_reply.started":"2025-04-25T17:54:34.929423Z","shell.execute_reply":"2025-04-25T17:54:35.482045Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"###✅ 🔄 Preprocessing Steps for test_df (Full Pipeline)","metadata":{"id":"18uJQBPhPwJw"}},{"cell_type":"code","source":"data.drop(columns='hr-0:05_filtered', inplace=True)","metadata":{"id":"6UISzM3ZTsuH","executionInfo":{"status":"ok","timestamp":1745122816631,"user_tz":-120,"elapsed":603,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:35.483985Z","iopub.execute_input":"2025-04-25T17:54:35.485028Z","iopub.status.idle":"2025-04-25T17:54:36.128623Z","shell.execute_reply.started":"2025-04-25T17:54:35.484996Z","shell.execute_reply":"2025-04-25T17:54:36.127652Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check if train and test have same feature columns (after dropping target from train)\n\ntrain_cols = data.columns\ntest_cols = test_df.columns\n\n# 1. Compare column names\nprint(\"Same columns? \", list(train_cols) == list(test_cols))\n\n# 2. Show extra or missing columns\nprint(\"\\nColumns only in train:\")\nprint(set(train_cols) - set(test_cols))\n\nprint(\"\\nColumns only in test:\")\nprint(set(test_cols) - set(train_cols))\n\n# 3. Check shape\nprint(\"\\nTrain shape:\", data.shape)\nprint(\"Test shape :\", test_df.shape)\n\n# 4. Optional: Check data types\nprint(\"\\nTrain types:\")\nprint(X.dtypes.value_counts())\n\nprint(\"\\nTest types:\")\nprint(test_df.dtypes.value_counts())\n","metadata":{"id":"wgxeKIEyUYqP","executionInfo":{"status":"ok","timestamp":1745122820390,"user_tz":-120,"elapsed":43,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"571a2698-7c44-449f-a567-b6b90c13366e","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:36.133939Z","iopub.execute_input":"2025-04-25T17:54:36.134205Z","iopub.status.idle":"2025-04-25T17:54:36.144959Z","shell.execute_reply.started":"2025-04-25T17:54:36.134187Z","shell.execute_reply":"2025-04-25T17:54:36.144094Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### ✅ Step 9: Train-Test Split & Step 10: Train a Regression Model\n\n- Split the cleaned dataset into **80% training** and **20% validation**.\n- Train a simple **Linear Regression** model as a baseline.\n\n```python\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\n\n","metadata":{"id":"cqf0OA5JOyNv"}},{"cell_type":"code","source":"","metadata":{"id":"mzoXu9Ywcq2y","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare features X and target y\nX_scal = data.drop(columns=['id', 'p_num', 'time', 'bg+1:00'], errors='ignore')\ny = data['bg+1:00']","metadata":{"id":"xajNlzmbcOmH","executionInfo":{"status":"ok","timestamp":1745123420590,"user_tz":-120,"elapsed":1209,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:36.145753Z","iopub.execute_input":"2025-04-25T17:54:36.145972Z","iopub.status.idle":"2025-04-25T17:54:36.853213Z","shell.execute_reply.started":"2025-04-25T17:54:36.145955Z","shell.execute_reply":"2025-04-25T17:54:36.851966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_scal","metadata":{"id":"M8tjt5Rbc3Nn","executionInfo":{"status":"ok","timestamp":1745123426701,"user_tz":-120,"elapsed":447,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"703b42dd-6cae-430d-8b6e-c4e7762c1d82","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:36.854386Z","iopub.execute_input":"2025-04-25T17:54:36.854745Z","iopub.status.idle":"2025-04-25T17:54:37.041714Z","shell.execute_reply.started":"2025-04-25T17:54:36.854714Z","shell.execute_reply":"2025-04-25T17:54:37.040953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y","metadata":{"id":"5NLlU5RdcvuQ","executionInfo":{"status":"ok","timestamp":1745123432219,"user_tz":-120,"elapsed":50,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"f4d0db66-b4ac-4448-e536-708e79f96ba1","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:37.042885Z","iopub.execute_input":"2025-04-25T17:54:37.043199Z","iopub.status.idle":"2025-04-25T17:54:37.050887Z","shell.execute_reply.started":"2025-04-25T17:54:37.04317Z","shell.execute_reply":"2025-04-25T17:54:37.04985Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler =  StandardScaler()\nX = scaler.fit_transform(X_scal)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:37.051895Z","iopub.execute_input":"2025-04-25T17:54:37.052178Z","iopub.status.idle":"2025-04-25T17:54:38.688932Z","shell.execute_reply.started":"2025-04-25T17:54:37.052145Z","shell.execute_reply":"2025-04-25T17:54:38.688101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_df = pd.DataFrame(X, columns=X_scal.columns, index=X_scal.index)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:38.689773Z","iopub.execute_input":"2025-04-25T17:54:38.690368Z","iopub.status.idle":"2025-04-25T17:54:38.69573Z","shell.execute_reply.started":"2025-04-25T17:54:38.690346Z","shell.execute_reply":"2025-04-25T17:54:38.694951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:38.69671Z","iopub.execute_input":"2025-04-25T17:54:38.697063Z","iopub.status.idle":"2025-04-25T17:54:38.758386Z","shell.execute_reply.started":"2025-04-25T17:54:38.697034Z","shell.execute_reply":"2025-04-25T17:54:38.757441Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# Split into train and validation sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n\n","metadata":{"id":"uFlskDi3VBJW","executionInfo":{"status":"ok","timestamp":1745123446531,"user_tz":-120,"elapsed":11897,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"a769ef9c-e04f-4493-9d03-89c7474d3c1f","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:38.75934Z","iopub.execute_input":"2025-04-25T17:54:38.759654Z","iopub.status.idle":"2025-04-25T17:54:39.574706Z","shell.execute_reply.started":"2025-04-25T17:54:38.759627Z","shell.execute_reply":"2025-04-25T17:54:39.57389Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train a linear regression model\nmodel = LinearRegression()\nmodel.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:39.57551Z","iopub.execute_input":"2025-04-25T17:54:39.575823Z","iopub.status.idle":"2025-04-25T17:54:44.991359Z","shell.execute_reply.started":"2025-04-25T17:54:39.575783Z","shell.execute_reply":"2025-04-25T17:54:44.990481Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### ✅ Step 11: Evaluate the Model & Step 12: Apply to Test Data\n\n- Evaluate on the **validation set** using MSE and R², and plot actual vs predicted.  \n- Predict on the **test set** and save a submission file.\n\n\n\n\n","metadata":{"id":"AyHRlL2cVB8v"}},{"cell_type":"code","source":"\n# 1. Predict on validation set\ny_pred = model.predict(X_test)\n\n# 2. Compute evaluation metrics\nprint(\"Mean Squared Error (MSE):\", mean_squared_error(y_test, y_pred))\nprint(\"R² Score:\", r2_score(y_test, y_pred))\n\n# 3. Plot actual vs predicted for validation\nplt.figure(figsize=(6, 6))\nplt.scatter(y_test, y_pred, alpha=0.3)\nplt.xlabel(\"Actual bg+1:00\")\nplt.ylabel(\"Predicted bg+1:00\")\nplt.title(\"Actual vs Predicted on Validation Set\")\nplt.grid(True)\nplt.show()\n\n# 4. Predict on test set\ny_test_pred = model.predict(X_test)\n\n","metadata":{"id":"VS8UqzIgWPL4","executionInfo":{"status":"ok","timestamp":1745123452889,"user_tz":-120,"elapsed":1610,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"e5d91e3a-6a58-4916-88ca-0ef7f589af76","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:44.992073Z","iopub.execute_input":"2025-04-25T17:54:44.992331Z","iopub.status.idle":"2025-04-25T17:54:45.329882Z","shell.execute_reply.started":"2025-04-25T17:54:44.99231Z","shell.execute_reply":"2025-04-25T17:54:45.328907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Compute residuals\nresiduals = y_test - y_pred\n\n# Plot histogram of residuals\nplt.figure(figsize=(6,4))\nplt.hist(residuals, bins=50, edgecolor='k', alpha=0.7)\nplt.title(\"Residuals Distribution\")\nplt.xlabel(\"Error (mmol/L)\")\nplt.ylabel(\"Frequency\")\nplt.grid(True)\nplt.show()\n\n# Scatter residuals vs predicted\nplt.figure(figsize=(6,4))\nplt.scatter(y_pred, residuals, alpha=0.3)\nplt.axhline(0, color='red', linestyle='--')\nplt.title(\"Residuals vs Predicted\")\nplt.xlabel(\"Predicted bg+1:00\")\nplt.ylabel(\"Residual (Actual–Predicted)\")\nplt.grid(True)\nplt.show()\n","metadata":{"id":"sqrVDJACW85J","executionInfo":{"status":"ok","timestamp":1745123467517,"user_tz":-120,"elapsed":1004,"user":{"displayName":"Hassan Abdul-razeq","userId":"12741250304608066816"}},"outputId":"814ba83e-dc84-4d36-e267-64f9dd309ae9","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:45.33111Z","iopub.execute_input":"2025-04-25T17:54:45.331369Z","iopub.status.idle":"2025-04-25T17:54:45.801864Z","shell.execute_reply.started":"2025-04-25T17:54:45.331349Z","shell.execute_reply":"2025-04-25T17:54:45.801012Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split, RandomizedSearchCV\nfrom sklearn.metrics import mean_squared_error, r2_score\nfrom scipy.stats import randint\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# 1. Split the data\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# 2. Build a baseline model\nbaseline_model = RandomForestRegressor(random_state=42)\nbaseline_model.fit(X_train, y_train)\nbaseline_preds = baseline_model.predict(X_val)\n\nbaseline_mse = mean_squared_error(y_val, baseline_preds)\nbaseline_r2 = r2_score(y_val, baseline_preds)\n\nprint(\"🔹 Baseline Model\")\nprint(\"MSE:\", baseline_mse)\nprint(\"R² Score:\", baseline_r2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T17:54:45.803228Z","iopub.execute_input":"2025-04-25T17:54:45.80359Z","iopub.status.idle":"2025-04-25T19:27:33.245022Z","shell.execute_reply.started":"2025-04-25T17:54:45.803562Z","shell.execute_reply":"2025-04-25T19:27:33.244136Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# تحديد القيم اللي هيجربها RandomizedSearchCV\nparam_dist = {\n    'n_estimators': randint(100, 300),\n    'max_depth': [None] + list(range(5, 20)),\n    'min_samples_split': randint(2, 10),\n    'min_samples_leaf': randint(1, 5),\n    'max_features': ['auto', 'sqrt', 'log2']\n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T19:27:33.246285Z","iopub.execute_input":"2025-04-25T19:27:33.246596Z","iopub.status.idle":"2025-04-25T19:27:33.256119Z","shell.execute_reply.started":"2025-04-25T19:27:33.246575Z","shell.execute_reply":"2025-04-25T19:27:33.255174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error, r2_score\nimport matplotlib.pyplot as plt\n\n# 1. Split the data again\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# 2. Create and train the model\nrf_model = RandomForestRegressor(n_estimators=100, random_state=42)\nrf_model.fit(X_train, y_train)\n\n# 3. Predict on validation set\n","metadata":{"id":"ExwnjF-jZfEC","trusted":true,"execution":{"iopub.status.busy":"2025-04-25T19:27:33.257318Z","iopub.execute_input":"2025-04-25T19:27:33.257679Z","iopub.status.idle":"2025-04-25T21:00:19.051327Z","shell.execute_reply.started":"2025-04-25T19:27:33.257632Z","shell.execute_reply":"2025-04-25T21:00:19.050155Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = rf_model.predict(X_val)\n\n# 4. Evaluate\nprint(\"Random Forest Regressor:\")\nprint(\"Mean Squared Error (MSE):\", mean_squared_error(y_val, y_pred))\nprint(\"R² Score:\", r2_score(y_val, y_pred))\n\n# 5. Plot actual vs predicted\nplt.figure(figsize=(6, 6))\nplt.scatter(y_val, y_pred, alpha=0.3, color='teal')\nplt.xlabel(\"Actual bg+1:00\")\nplt.ylabel(\"Predicted bg+1:00\")\nplt.title(\"Random Forest: Actual vs Predicted\")\nplt.grid(True)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-25T21:00:19.052441Z","iopub.execute_input":"2025-04-25T21:00:19.052818Z","iopub.status.idle":"2025-04-25T21:00:22.10832Z","shell.execute_reply.started":"2025-04-25T21:00:19.052737Z","shell.execute_reply":"2025-04-25T21:00:22.10723Z"}},"outputs":[],"execution_count":null}]}