{"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":"code","source":"!pip install -qq pytorch_tabnet","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:08.143834Z","iopub.execute_input":"2024-11-29T15:33:08.144415Z","iopub.status.idle":"2024-11-29T15:33:19.656906Z","shell.execute_reply.started":"2024-11-29T15:33:08.144374Z","shell.execute_reply":"2024-11-29T15:33:19.655468Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Import all the necessary Library","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:19.658922Z","iopub.execute_input":"2024-11-29T15:33:19.659242Z","iopub.status.idle":"2024-11-29T15:33:19.664290Z","shell.execute_reply.started":"2024-11-29T15:33:19.659213Z","shell.execute_reply":"2024-11-29T15:33:19.663397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom tqdm import tqdm\nfrom itertools import product\nfrom sklearn.model_selection import GroupKFold\nfrom sklearn.impute import SimpleImputer\nfrom pytorch_tabnet.tab_model import TabNetRegressor\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:19.665452Z","iopub.execute_input":"2024-11-29T15:33:19.665771Z","iopub.status.idle":"2024-11-29T15:33:23.657952Z","shell.execute_reply.started":"2024-11-29T15:33:19.665743Z","shell.execute_reply":"2024-11-29T15:33:23.656986Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load and preprocessing dataset","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/brist1d/train.csv', \n    index_col='id', \n    parse_dates=['time'],\n)\n\ntest_data = pd.read_csv( '/kaggle/input/brist1d/test.csv', \n    index_col='id', \n    parse_dates=['time'],\n)\n\nsub_data = pd.read_csv(\"/kaggle/input/brist1d/sample_submission.csv\",\n    index_col='id',\n)\n\n# Some frameworks may not handle column names with special characters like colons properly\ntrain_data.columns = train_data.columns.str.replace(':', '-')\ntest_data.columns = test_data.columns.str.replace(':', '-')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:23.660418Z","iopub.execute_input":"2024-11-29T15:33:23.660893Z","iopub.status.idle":"2024-11-29T15:33:35.745245Z","shell.execute_reply.started":"2024-11-29T15:33:23.660841Z","shell.execute_reply":"2024-11-29T15:33:35.744228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:35.746709Z","iopub.execute_input":"2024-11-29T15:33:35.747365Z","iopub.status.idle":"2024-11-29T15:33:35.780770Z","shell.execute_reply.started":"2024-11-29T15:33:35.747318Z","shell.execute_reply":"2024-11-29T15:33:35.779623Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:35.782102Z","iopub.execute_input":"2024-11-29T15:33:35.782509Z","iopub.status.idle":"2024-11-29T15:33:35.803402Z","shell.execute_reply.started":"2024-11-29T15:33:35.782464Z","shell.execute_reply":"2024-11-29T15:33:35.802428Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data variables","metadata":{}},{"cell_type":"code","source":"# Define the hour and minute ranges for constructing feature column names\nhours = range(0, 6, 1)\nminutes = range(0, 60, 5)\n\ntarget_col = 'bg+1-00' # Target column name for prediction\ngroup_col = 'p_num'    # Column name for grouping (e.g., participant number)\ndate_col = 'time'      # Column name for time data\n\n# Need the last 12 time intervals(1 hour)\nbg_cols   = [f'bg-{i}-{j:02d}' for i, j in product(hours, minutes)][:12]\ninsu_cols = [f'insulin-{i}-{j:02d}' for i, j in product(hours, minutes)][:12]\ncarb_cols = [f'carbs-{i}-{j:02d}' for i, j in product(hours, minutes)][:12]\nhr_cols   = [f'hr-{i}-{j:02d}' for i, j in product(hours, minutes)][:12]\nstep_cols = [f'steps-{i}-{j:02d}' for i, j in product(hours, minutes)][:12]\ncals_cols = [f'cals-{i}-{j:02d}' for i, j in product(hours, minutes)][:12]\n\nfeature_cols = bg_cols + insu_cols + carb_cols + hr_cols + step_cols + cals_cols\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:35.804549Z","iopub.execute_input":"2024-11-29T15:33:35.804916Z","iopub.status.idle":"2024-11-29T15:33:35.813643Z","shell.execute_reply.started":"2024-11-29T15:33:35.804875Z","shell.execute_reply":"2024-11-29T15:33:35.812730Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Filling the missing value\n\n**Apply linear interpolation for fill missing values within each time series**\n","metadata":{}},{"cell_type":"code","source":"for colset in [bg_cols, insu_cols, carb_cols, hr_cols, step_cols, cals_cols]:\n    train_data[colset] = train_data[colset].interpolate(axis=1)\n    test_data[colset] = test_data[colset].interpolate(axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:35.814981Z","iopub.execute_input":"2024-11-29T15:33:35.815297Z","iopub.status.idle":"2024-11-29T15:33:50.422577Z","shell.execute_reply.started":"2024-11-29T15:33:35.815269Z","shell.execute_reply":"2024-11-29T15:33:50.421406Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Using imputer on on the training data and fills missing values with the mean value**\r\n","metadata":{}},{"cell_type":"code","source":"imputer = SimpleImputer()\n\ntrain_data[feature_cols] = imputer.fit_transform(train_data[feature_cols])\ntest_data[feature_cols] = imputer.transform(test_data[feature_cols])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:50.423819Z","iopub.execute_input":"2024-11-29T15:33:50.424134Z","iopub.status.idle":"2024-11-29T15:33:50.864819Z","shell.execute_reply.started":"2024-11-29T15:33:50.424107Z","shell.execute_reply":"2024-11-29T15:33:50.863705Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Time Features\n\r**Now Create a cyclic time features using sine and cosine transformations for the 'hour' component of the date column to helps encode the hour of the day in a way that captures its cyclical nature (e.g., 23:00 is close to 00:00)**)\r\n","metadata":{}},{"cell_type":"code","source":"train_data['sin_hour'] = np.sin(np.pi * train_data[date_col].dt.hour / 12)\ntrain_data['cos_hour'] = np.cos(np.pi * train_data[date_col].dt.hour / 12)\n\ntest_data['sin_hour'] = np.sin(np.pi * test_data[date_col].dt.hour / 12)\ntest_data['cos_hour'] = np.cos(np.pi * test_data[date_col].dt.hour / 12)\n\nfeature_cols.extend(['sin_hour', 'cos_hour'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:50.867107Z","iopub.execute_input":"2024-11-29T15:33:50.867413Z","iopub.status.idle":"2024-11-29T15:33:50.895578Z","shell.execute_reply.started":"2024-11-29T15:33:50.867383Z","shell.execute_reply":"2024-11-29T15:33:50.894793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features_grouped = []\n\nfor colset in [bg_cols, insu_cols, carb_cols, hr_cols, step_cols, cals_cols]:\n    group_idxs = [idx for idx, col in enumerate(feature_cols) if col in colset]\n    features_grouped.append(group_idxs)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:50.896756Z","iopub.execute_input":"2024-11-29T15:33:50.897085Z","iopub.status.idle":"2024-11-29T15:33:50.902645Z","shell.execute_reply.started":"2024-11-29T15:33:50.897056Z","shell.execute_reply":"2024-11-29T15:33:50.901450Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Train the model","metadata":{}},{"cell_type":"code","source":"# Ensure feature_cols, target_col, and group_col are properly defined\nx = train_data[feature_cols]\ny = train_data[[target_col]]\ngroups = train_data[group_col]\n\ncv = GroupKFold(5)\nsub_data['bg+1:00'] = 0.0\n\nfor idx_train, idx_valid in cv.split(x, y, groups=groups):\n    \n    # Split the data into training and validation sets for the current fold\n    x_train, y_train = x.iloc[idx_train].to_numpy(), y.iloc[idx_train].to_numpy()\n    x_valid, y_valid = x.iloc[idx_valid].to_numpy(), y.iloc[idx_valid].to_numpy()\n    \n    # Initialize TabNet Regressor\n    reg = TabNetRegressor(\n        n_d=8,              # Example hyperparameter, tune as needed\n        n_a=8,              # Example hyperparameter, tune as needed\n        n_steps=3,          # Example hyperparameter, tune as needed\n        gamma=1.0,          # Relaxation factor for attention masks\n        lambda_sparse=0.0001 # Sparsity regularization coefficient\n    )\n    \n    # reg = TabNetRegressor(\n    #     'n_d': [8, 16, 32],\n    #     'n_a': [8, 16, 32],\n    #     'n_steps': [3, 5, 7],\n    #     'gamma': [1.0, 1.3, 1.5],\n    #     'lambda_sparse': [0.0001, 0.001, 0.01]\n    # )\n    \n    # Train the model using the training set and validate using the validation set\n    reg.fit(\n        x_train, y_train,\n        eval_set=[(x_valid, y_valid)],\n        eval_metric=['rmse'],\n        batch_size=10000,\n    )\n    \n    # Prepare the test data and make predictions\n    x_test = test_data[feature_cols].to_numpy()\n    \n    # Aggregate the predictions across the 5 folds (averaging for ensemble)\n    sub_data['bg+1:00'] += reg.predict(x_test)[:, 0] / 5\n    \nsub_data.to_csv('submission.csv')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T15:33:50.904062Z","iopub.execute_input":"2024-11-29T15:33:50.904414Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}