{"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":"## BrisT1D Competition with QUANT\n\nThis problem aims to predict the level of blood glucose in a patient an hour in advance.\n\nThis can be treated as a time series extrinstic regression problem, where given time series of one source or more, the algorithm must predict a real value related to this time series.\n\nyou can see more about this problem here [https://link.springer.com/article/10.1007/s10618-021-00745-9]\n\nThis notebook shows a technique that present the best results in the literature : interval-based methods [https://link.springer.com/article/10.1007/s10618-024-01027-w]. And this notebook shows a recent technique called QUANT [https://link.springer.com/article/10.1007/s10618-024-01036-9].\n\nQUANT computes quantiles over evenly spaced intervals of the series. If the series have length n, it will compute the quantiles of dyadic intervals of length n, n/2, n/4, ...\n\nThis technique improved Catboost (without fine-tuning) RMSE from 2.55 to 2.53.\n\nIt increases the number of features from 300~ to 4000~. But if you have computational power, why not? I've used kaggle GPU's to run Catboost.\n","metadata":{}},{"cell_type":"markdown","source":"## first install aeon (it might take some time to import QUANTTransformer due to aeon)","metadata":{}},{"cell_type":"code","source":"pip install aeon","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from aeon.transformations.collection.interval_based import QUANTTransformer","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## load data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/brist1d/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/brist1d/test.csv\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Extract each series and fill nans","metadata":{}},{"cell_type":"code","source":"def features_by_type(type):\n    features = []\n    for i in range(6):\n        for j in range(0,60,5):\n            if j == 0:\n                features.append(f\"{type}-{i}:00\")\n            elif j == 5:\n                features.append(f\"{type}-{i}:05\")\n            else:\n                features.append(f\"{type}-{i}:{j}\")\n    return features","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## BGS (filling nans with closest values)","metadata":{}},{"cell_type":"code","source":"bg = features_by_type(\"bg\")\nbg_train = train[bg].fillna(method = \"ffill\", axis = 1).fillna(method = \"bfill\", axis = 1)\n#bg_train = bg_train.to_numpy()\ny_train = train[\"bg+1:00\"].to_numpy()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bg_test = test[bg].fillna(method = \"ffill\", axis = 1).fillna(method = \"bfill\", axis = 1)\nbg_test = bg_test.to_numpy()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## INSULIN","metadata":{}},{"cell_type":"code","source":"insulin = features_by_type(\"insulin\")\ninsulin_train = train[insulin]\ninsulin_train = insulin_train.fillna(value = 0)\ninsulin_train = insulin_train.to_numpy()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"insulin_test = test[insulin].fillna(value = 0)\ninsulin_test = insulin_test.to_numpy()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Heart Rate","metadata":{}},{"cell_type":"code","source":"hr = features_by_type(\"hr\")\nhr_train = train[hr].fillna(value = 0)\nhr_train = hr_train.to_numpy()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hr_test = test[hr].fillna(value = 0)\nhr_test = hr_test.to_numpy()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## STEPS","metadata":{}},{"cell_type":"code","source":"steps = features_by_type(\"steps\")\nsteps_train = train[steps].fillna(value = 0)\nsteps_train = steps_train.to_numpy()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"steps_test = test[steps].fillna(value = 0)\nsteps_test = steps_test.to_numpy()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## CALS","metadata":{}},{"cell_type":"code","source":"cals = features_by_type(\"cals\")\ncals_train = train[cals].fillna(value = 0)\ncals_train = cals_train.to_numpy()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cals_test = test[cals].fillna(value = 0)\ncals_test = cals_test.to_numpy()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## COMPUTE QUANT","metadata":{}},{"cell_type":"code","source":"def to_quant(X_train, X_test):\n    transformer = QUANTTransformer()\n    X_train = transformer.fit_transform(X_train)\n    X_test = transformer.transform(X_test)\n    return X_train, X_test","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bg_train,bg_test = to_quant(bg_train,bg_test) \ninsulin_train, insulin_test = to_quant(insulin_train, insulin_test) \nhr_train,hr_test = to_quant(hr_train,hr_test) \nsteps_train, steps_test = to_quant(steps_train, steps_test) \ncals_train, cals_test = to_quant(cals_train, cals_test) ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## now we have the quant features from the series","metadata":{}},{"cell_type":"code","source":"X_train = np.concatenate([bg_train, insulin_train, hr_train, steps_train, cals_train], axis=1)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = np.concatenate([bg_test, insulin_test, hr_test, steps_test, cals_test], axis=1)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}