{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Setup","metadata":{}},{"cell_type":"markdown","source":"## Packages","metadata":{}},{"cell_type":"code","source":"!pip install darts","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:08:48.872759Z","iopub.execute_input":"2023-11-04T09:08:48.873157Z","iopub.status.idle":"2023-11-04T09:09:10.85461Z","shell.execute_reply.started":"2023-11-04T09:08:48.873124Z","shell.execute_reply":"2023-11-04T09:09:10.853515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom itertools import groupby\nimport gc\nimport pandas as pd\nimport numpy as np\nimport gc\nimport time\nimport json\nfrom datetime import datetime\nimport matplotlib.pyplot as plt\nimport os\nimport joblib\nimport random\nimport math\nfrom tqdm.auto import tqdm \nimport polars as pl\n\nfrom scipy.interpolate import interp1d\n\nfrom math import pi, sqrt, exp\nimport sklearn,sklearn.model_selection\nimport torch\nfrom torch import nn,Tensor\nimport torch.nn.functional as F\nfrom torch.utils.data import DataLoader, Dataset, SubsetRandomSampler\nfrom sklearn.metrics import average_precision_score\nfrom timm.scheduler import CosineLRScheduler\nplt.style.use(\"ggplot\")\n\nfrom pyarrow.parquet import ParquetFile\nimport pyarrow as pa \nimport ctypes\n\nfrom darts.models import BlockRNNModel\nfrom darts.datasets import TimeSeries","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:12:15.223916Z","iopub.execute_input":"2023-11-04T09:12:15.224652Z","iopub.status.idle":"2023-11-04T09:12:36.811401Z","shell.execute_reply.started":"2023-11-04T09:12:15.224621Z","shell.execute_reply":"2023-11-04T09:12:36.810576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:12:36.812821Z","iopub.execute_input":"2023-11-04T09:12:36.813116Z","iopub.status.idle":"2023-11-04T09:12:36.822886Z","shell.execute_reply.started":"2023-11-04T09:12:36.81309Z","shell.execute_reply":"2023-11-04T09:12:36.821921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# multiclass data\ntrain = pl.read_parquet(\"/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train_multi.parquet\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:13:21.614199Z","iopub.execute_input":"2023-11-04T09:13:21.615379Z","iopub.status.idle":"2023-11-04T09:13:23.728875Z","shell.execute_reply.started":"2023-11-04T09:13:21.615337Z","shell.execute_reply":"2023-11-04T09:13:23.727766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:13:33.915419Z","iopub.execute_input":"2023-11-04T09:13:33.916266Z","iopub.status.idle":"2023-11-04T09:13:33.946028Z","shell.execute_reply.started":"2023-11-04T09:13:33.916233Z","shell.execute_reply":"2023-11-04T09:13:33.945228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.replace(\"awake\", train[\"awake\"].cast(pl.Int8))","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:13:38.124575Z","iopub.execute_input":"2023-11-04T09:13:38.125518Z","iopub.status.idle":"2023-11-04T09:13:38.261563Z","shell.execute_reply.started":"2023-11-04T09:13:38.125474Z","shell.execute_reply":"2023-11-04T09:13:38.26044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ndef process_timestamp(df):\n    df = df.with_columns(\n        year = df[\"timestamp\"].str.slice(0,4).cast(pl.Int16),\n        month = df[\"timestamp\"].str.slice(5,2).cast(pl.Int8),\n        day = df[\"timestamp\"].str.slice(8,2).cast(pl.Int8),\n        hour = df[\"timestamp\"].str.slice(11,2).cast(pl.Int8),\n        minute = df[\"timestamp\"].str.slice(14,2).cast(pl.Int8),\n        second = df[\"timestamp\"].str.slice(17,2).cast(pl.Int8),\n        time_zone = df[\"timestamp\"].str.slice(-5,3).cast(pl.Int32)\n    )\n    return df\n    \n    \n# print (process_timestamp(train_smooth))\n\ntrain_time_extract = process_timestamp(train)\n\ntrain_time_extract","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:13:50.760049Z","iopub.execute_input":"2023-11-04T09:13:50.760767Z","iopub.status.idle":"2023-11-04T09:13:56.36806Z","shell.execute_reply.started":"2023-11-04T09:13:50.760735Z","shell.execute_reply":"2023-11-04T09:13:56.36713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train_time_extract.to_pandas()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:31:14.387754Z","iopub.execute_input":"2023-11-04T09:31:14.38811Z","iopub.status.idle":"2023-11-04T09:31:19.791955Z","shell.execute_reply.started":"2023-11-04T09:31:14.388081Z","shell.execute_reply":"2023-11-04T09:31:19.791153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:31:21.370566Z","iopub.execute_input":"2023-11-04T09:31:21.37096Z","iopub.status.idle":"2023-11-04T09:31:21.392721Z","shell.execute_reply.started":"2023-11-04T09:31:21.37093Z","shell.execute_reply":"2023-11-04T09:31:21.391854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def make_features(df):\n#     # parse the timestamp and create an \"hour\" feature\n#     df['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.tz_localize(None))\n#     df[\"hour\"] = df[\"timestamp\"].dt.hour\n    \n#     periods = 20\n#     df[\"anglez\"] = abs(df[\"anglez\"])\n#     df[\"anglez_diff\"] = df.groupby('series_id')['anglez'].diff(periods=periods).fillna(method=\"bfill\").astype('float16')\n#     df[\"enmo_diff\"] = df.groupby('series_id')['enmo'].diff(periods=periods).fillna(method=\"bfill\").astype('float16')\n#     df[\"anglez_rolling_mean\"] = df[\"anglez\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n#     df[\"enmo_rolling_mean\"] = df[\"enmo\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n#     df[\"anglez_rolling_max\"] = df[\"anglez\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n#     df[\"enmo_rolling_max\"] = df[\"enmo\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n#     df[\"anglez_rolling_std\"] = df[\"anglez\"].rolling(periods,center=True).std().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n#     df[\"enmo_rolling_std\"] = df[\"enmo\"].rolling(periods,center=True).std().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n#     df[\"anglez_diff_rolling_mean\"] = df[\"anglez_diff\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n#     df[\"enmo_diff_rolling_mean\"] = df[\"enmo_diff\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n#     df[\"anglez_diff_rolling_max\"] = df[\"anglez_diff\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n#     df[\"enmo_diff_rolling_max\"] = df[\"enmo_diff\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    \n#     return df\n\n# features = [\"hour\",\n#             \"anglez\",,\n#            ]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class PATHS:\n#     MAIN_DIR = \"/kaggle/input/child-mind-institute-detect-sleep-states/\"\n#     # CSV FILES : \n#     SUBMISSION = MAIN_DIR + \"sample_submission.csv\"\n#     TRAIN_EVENTS = MAIN_DIR + \"train_events.csv\"\n#     # PARQUET FILES:\n#     TRAIN_SERIES = MAIN_DIR + \"train_series.parquet\"\n#     TEST_SERIES = MAIN_DIR + \"test_series.parquet\"","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:25:59.311526Z","iopub.execute_input":"2023-11-04T02:25:59.311957Z","iopub.status.idle":"2023-11-04T02:25:59.317711Z","shell.execute_reply.started":"2023-11-04T02:25:59.311923Z","shell.execute_reply":"2023-11-04T02:25:59.31686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class CFG:\n#     DEMO_MODE = True","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:26:02.965911Z","iopub.execute_input":"2023-11-04T02:26:02.966356Z","iopub.status.idle":"2023-11-04T02:26:02.9722Z","shell.execute_reply.started":"2023-11-04T02:26:02.966322Z","shell.execute_reply":"2023-11-04T02:26:02.97132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class data_reader:\n#     def __init__(self, demo_mode):\n#         super().__init__()\n#         # MAPPING FOR DATA LOADING :\n#         self.names_mapping = {\n#             \"submission\" : {\"path\" : PATHS.SUBMISSION, \"is_parquet\" : False, \"has_timestamp\" : False}, \n#             \"train_events\" : {\"path\" : PATHS.TRAIN_EVENTS, \"is_parquet\" : False, \"has_timestamp\" : True},\n#             \"train_series\" : {\"path\" : PATHS.TRAIN_SERIES, \"is_parquet\" : True, \"has_timestamp\" : True},\n#             \"test_series\" : {\"path\" : PATHS.TEST_SERIES, \"is_parquet\" : True, \"has_timestamp\" : True}\n#         }\n#         self.valid_names = [\"submission\", \"train_events\", \"train_series\", \"test_series\"]\n#         self.demo_mode = demo_mode\n    \n#     def verify(self, data_name):\n#         \"function for data name verification\"\n#         if data_name not in self.valid_names:\n#             print(\"PLEASE ENTER A VALID DATASET NAME, VALID NAMES ARE : \", valid_names)\n#         return\n    \n#     def cleaning(self, data):\n#         \"cleaning function : drop na values\"\n#         before_cleaning = len(data)\n#         print(\"Number of missing timestamps : \", len(data[data[\"timestamp\"].isna()]))\n#         data = data.dropna(subset=[\"timestamp\"])\n#         after_cleaning = len(data)\n#         print(\"Percentage of removed steps : {:.1f}%\".format(100 * (before_cleaning - after_cleaning) / before_cleaning) )\n# #         print(data.isna().any())\n# #         data = data.bfill()\n#         return data\n    \n#     @staticmethod\n#     def reduce_memory_usage(data):\n#         \"iterate through all the columns of a dataframe and modify the data type to reduce memory usage.\"\n#         start_mem = data.memory_usage().sum() / 1024**2\n#         print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n#         for col in data.columns:\n#             col_type = data[col].dtype    \n#             if col_type != object:\n#                 c_min = data[col].min()\n#                 c_max = data[col].max()\n#                 if str(col_type)[:3] == 'int':\n#                     if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n#                         data[col] = data[col].astype(np.int8)\n#                     elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n#                         data[col] = data[col].astype(np.int16)\n#                     elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n#                         data[col] = data[col].astype(np.int32)\n#                     elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n#                         data[col] = data[col].astype(np.int64)  \n#                 else:\n#                     if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n#                         data[col] = data[col].astype(np.float16)\n#                     elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n#                         data[col] = data[col].astype(np.float32)\n#                     else:\n#                         data[col] = data[col].astype(np.float64)\n#             else:\n#                 data[col] = data[col].astype('category')\n\n#         end_mem = data.memory_usage().sum() / 1024**2\n#         print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n#         print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n#         return data\n    \n#     def load_data(self, data_name):\n#         \"function for data loading\"\n#         self.verify(data_name)\n#         data_props = self.names_mapping[data_name]\n#         if data_props[\"is_parquet\"]:\n#             if self.demo_mode:\n#                 pf = ParquetFile(data_props[\"path\"]) \n#                 demo_steps = next(pf.iter_batches(batch_size=20_000)) \n#                 data = pa.Table.from_batches([demo_steps]).to_pandas()\n#             else:\n#                 data = pd.read_parquet(data_props[\"path\"])\n#         else:\n#             if self.demo_mode:\n#                 data = pd.read_csv(data_props[\"path\"], nsteps=20_000)\n#             else:\n#                 data = pd.read_csv(data_props[\"path\"])\n                \n#         gc.collect()\n#         if data_props[\"has_timestamp\"]:\n#             print('cleaning')\n#             data = self.cleaning(data)\n#             gc.collect()\n#         data = self.reduce_memory_usage(data)\n#         return data","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:26:04.680068Z","iopub.execute_input":"2023-11-04T02:26:04.680459Z","iopub.status.idle":"2023-11-04T02:26:04.708678Z","shell.execute_reply.started":"2023-11-04T02:26:04.680427Z","shell.execute_reply":"2023-11-04T02:26:04.707424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reader = data_reader(demo_mode=False)\n# series = reader.load_data(data_name=\"train_series\")\n# events = reader.load_data(data_name=\"train_events\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:20:24.749363Z","iopub.execute_input":"2023-11-04T09:20:24.750193Z","iopub.status.idle":"2023-11-04T09:20:24.754297Z","shell.execute_reply.started":"2023-11-04T09:20:24.750148Z","shell.execute_reply":"2023-11-04T09:20:24.753243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# targets = []\n# data = []\n# ids = series.series_id.unique()\n\n# for viz_id in tqdm(ids):\n#     viz_targets = []\n#     viz_events = events[events.series_id == viz_id]\n#     viz_series = series.loc[(series.series_id==viz_id)].copy().reset_index()\n#     viz_series['dt'] = pd.to_datetime(viz_series.timestamp,format = '%Y-%m-%dT%H:%M:%S%z').astype(\"datetime64[ns, UTC-04:00]\")\n#     viz_series['hour'] = viz_series['dt'].dt.hour\n\n#     check = 0\n#     for i in range(len(viz_events)-1):\n#         if viz_events.iloc[i].event =='onset' and viz_events.iloc[i+1].event =='wakeup' and viz_events.iloc[i].night==viz_events.iloc[i+1].night:\n#             start,end = viz_events.timestamp.iloc[i],viz_events.timestamp.iloc[i+1]\n\n#             start_id = viz_series.loc[viz_series.timestamp ==start].index.values[0]\n#             end_id = viz_series.loc[viz_series.timestamp ==end].index.values[0]\n#             viz_targets.append((start_id,end_id))\n#             check+=1\n#     targets.append(viz_targets)\n#     data.append(viz_series[['anglez','enmo','step']]) #you can include features like hour or minnute or second if you want to\n# joblib.dump((targets,data,ids), 'train_data.pkl')\n# len(data)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n# train = pl.read_parquet('/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:21:09.137243Z","iopub.execute_input":"2023-11-04T09:21:09.137617Z","iopub.status.idle":"2023-11-04T09:21:09.14178Z","shell.execute_reply.started":"2023-11-04T09:21:09.13759Z","shell.execute_reply":"2023-11-04T09:21:09.140789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train.replace(\"awake\", train[\"awake\"].cast(pl.Int8))","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:21:07.195217Z","iopub.execute_input":"2023-11-04T09:21:07.195907Z","iopub.status.idle":"2023-11-04T09:21:07.199926Z","shell.execute_reply.started":"2023-11-04T09:21:07.195874Z","shell.execute_reply":"2023-11-04T09:21:07.198921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def luan_unique(seq):\n#     seen = set()\n#     seen_add = seen.add\n#     return [x for x in seq if not (x in seen or seen_add(x))]\n\n\n# ids = luan_unique(train['series_id'])\n\n# print(ids)\n\n# len(ids)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:21:03.767221Z","iopub.execute_input":"2023-11-04T09:21:03.768099Z","iopub.status.idle":"2023-11-04T09:21:03.772071Z","shell.execute_reply.started":"2023-11-04T09:21:03.768067Z","shell.execute_reply":"2023-11-04T09:21:03.770986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n\n# dict_ids2data = {}\n\n# for i in tqdm(range(len(ids))):\n# # for i in range(2):\n#     print (i,': ', ids[i])\n#     dict_ids2data[ids[i]] = train.filter(pl.col(\"series_id\") == ids[i])\n    \n#     dict_ids2data[ids[i]] = dict_ids2data[ids[i]].with_columns(\n#         anglez = pl.col(\"anglez\").rolling_mean(window_size=21, center = True),\n#         enmo = pl.col(\"enmo\").rolling_mean(window_size=21, center = True),\n#     )\n    \n#     dict_ids2data[ids[i]] = dict_ids2data[ids[i]].fill_null(strategy=\"forward\").fill_null(strategy=\"backward\")\n    \n\n\n# train_smooth = pl.concat([i for i in dict_ids2data.values()], rechunk=True)\n\n\n# del dict_ids2data\n# gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:21:00.593014Z","iopub.execute_input":"2023-11-04T09:21:00.593471Z","iopub.status.idle":"2023-11-04T09:21:00.598107Z","shell.execute_reply.started":"2023-11-04T09:21:00.593439Z","shell.execute_reply":"2023-11-04T09:21:00.597064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_smooth[\"timestamp\"].str.slice(-5,).unique()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:20:57.761039Z","iopub.execute_input":"2023-11-04T09:20:57.761421Z","iopub.status.idle":"2023-11-04T09:20:57.766411Z","shell.execute_reply.started":"2023-11-04T09:20:57.761392Z","shell.execute_reply":"2023-11-04T09:20:57.76548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n\n# def process_timestamp(df):\n#     df = df.with_columns(\n#         year = df[\"timestamp\"].str.slice(0,4).cast(pl.Int16),\n#         month = df[\"timestamp\"].str.slice(5,2).cast(pl.Int8),\n#         day = df[\"timestamp\"].str.slice(8,2).cast(pl.Int8),\n#         hour = df[\"timestamp\"].str.slice(11,2).cast(pl.Int8),\n#         minute = df[\"timestamp\"].str.slice(14,2).cast(pl.Int8),\n#         second = df[\"timestamp\"].str.slice(17,2).cast(pl.Int8),\n#         time_zone = df[\"timestamp\"].str.slice(-5,3).cast(pl.Int32)\n#     )\n#     return df\n    \n    \n# # print (process_timestamp(train_smooth))\n\n# train_time_extract = process_timestamp(train_smooth)\n\n# train_time_extract","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:20:52.655997Z","iopub.execute_input":"2023-11-04T09:20:52.656749Z","iopub.status.idle":"2023-11-04T09:20:52.661329Z","shell.execute_reply.started":"2023-11-04T09:20:52.656716Z","shell.execute_reply":"2023-11-04T09:20:52.660237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_time_extract['timestamp'][9]","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:20:49.17947Z","iopub.execute_input":"2023-11-04T09:20:49.180169Z","iopub.status.idle":"2023-11-04T09:20:49.184792Z","shell.execute_reply.started":"2023-11-04T09:20:49.18013Z","shell.execute_reply":"2023-11-04T09:20:49.183763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import joblib\n# joblib.dump(train_time_extract, 'train_luan.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:20:47.120941Z","iopub.execute_input":"2023-11-04T09:20:47.121883Z","iopub.status.idle":"2023-11-04T09:20:47.125576Z","shell.execute_reply.started":"2023-11-04T09:20:47.121848Z","shell.execute_reply":"2023-11-04T09:20:47.124591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n# train_time_extract.write_csv('train_luan.csv')\n# train_time_extract.write_parquet('train_luan.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:20:44.171595Z","iopub.execute_input":"2023-11-04T09:20:44.172351Z","iopub.status.idle":"2023-11-04T09:20:44.176397Z","shell.execute_reply.started":"2023-11-04T09:20:44.17232Z","shell.execute_reply":"2023-11-04T09:20:44.17529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train   = pd.read_csv('train_luan.csv')\n# train","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:20:42.402011Z","iopub.execute_input":"2023-11-04T09:20:42.402781Z","iopub.status.idle":"2023-11-04T09:20:42.406718Z","shell.execute_reply.started":"2023-11-04T09:20:42.402747Z","shell.execute_reply":"2023-11-04T09:20:42.405683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = ['series_id',\"anglez\", \"enmo\", \"year\", \"month\", \"day\", \"hour\", \"minute\", \"second\", \"time_zone\", \"awake\"]\n# X_train = train[features]\n# y_train = train[\"awake\"]\n\n# # save some memory\n# del train\n# gc.collect();","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:22:44.611851Z","iopub.execute_input":"2023-11-04T09:22:44.612759Z","iopub.status.idle":"2023-11-04T09:22:44.617158Z","shell.execute_reply.started":"2023-11-04T09:22:44.612726Z","shell.execute_reply":"2023-11-04T09:22:44.616068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = ['series_id',\"anglez\", \"enmo\", \"year\", \"month\", \"day\", \"hour\", \"minute\", \"second\", \"time_zone\", \"awake\"]\ntrain = TimeSeries.from_group_dataframe(train[features], 'series_id')\n# train","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:31:35.481024Z","iopub.execute_input":"2023-11-04T09:31:35.482024Z","iopub.status.idle":"2023-11-04T09:31:46.212056Z","shell.execute_reply.started":"2023-11-04T09:31:35.481987Z","shell.execute_reply":"2023-11-04T09:31:46.210681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = [\"anglez\", \"enmo\", \"year\", \"month\", \"day\", \"hour\", \"minute\", \"second\", \"time_zone\"]\n# train[0].pd_dataframe()\n\nX_train = pd.concat([train[0].pd_dataframe()[features], train[1].pd_dataframe()[features]]) \ny_train = pd.concat([train[0].pd_dataframe()[\"awake\"], train[1].pd_dataframe()[\"awake\"]])","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:36:00.268604Z","iopub.execute_input":"2023-11-04T09:36:00.26937Z","iopub.status.idle":"2023-11-04T09:36:00.327105Z","shell.execute_reply.started":"2023-11-04T09:36:00.269337Z","shell.execute_reply":"2023-11-04T09:36:00.326118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:36:03.850998Z","iopub.execute_input":"2023-11-04T09:36:03.851735Z","iopub.status.idle":"2023-11-04T09:36:03.87594Z","shell.execute_reply.started":"2023-11-04T09:36:03.851704Z","shell.execute_reply":"2023-11-04T09:36:03.874962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:36:09.615093Z","iopub.execute_input":"2023-11-04T09:36:09.615457Z","iopub.status.idle":"2023-11-04T09:36:09.62397Z","shell.execute_reply.started":"2023-11-04T09:36:09.615433Z","shell.execute_reply":"2023-11-04T09:36:09.623043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = TimeSeries.from_dataframe(X_train)\ny_train = TimeSeries.from_dataframe(pd.DataFrame(y_train))","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:36:12.917447Z","iopub.execute_input":"2023-11-04T09:36:12.917819Z","iopub.status.idle":"2023-11-04T09:36:12.952034Z","shell.execute_reply.started":"2023-11-04T09:36:12.917791Z","shell.execute_reply":"2023-11-04T09:36:12.951234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"model = BlockRNNModel(\n    input_chunk_length=48,\n    output_chunk_length=24,\n    n_rnn_layers=2,\n    n_epochs=10,\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:36:17.233139Z","iopub.execute_input":"2023-11-04T09:36:17.233786Z","iopub.status.idle":"2023-11-04T09:36:17.281613Z","shell.execute_reply.started":"2023-11-04T09:36:17.233752Z","shell.execute_reply":"2023-11-04T09:36:17.280817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(y_train, past_covariates=X_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T09:36:25.12172Z","iopub.execute_input":"2023-11-04T09:36:25.122559Z","iopub.status.idle":"2023-11-04T10:22:51.982619Z","shell.execute_reply.started":"2023-11-04T09:36:25.122526Z","shell.execute_reply":"2023-11-04T10:22:51.981731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Testing","metadata":{}},{"cell_type":"code","source":"# multiclass data\ntest = pl.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T10:45:07.913622Z","iopub.execute_input":"2023-11-04T10:45:07.914398Z","iopub.status.idle":"2023-11-04T10:45:07.920472Z","shell.execute_reply.started":"2023-11-04T10:45:07.914364Z","shell.execute_reply":"2023-11-04T10:45:07.919532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-11-04T10:45:14.229717Z","iopub.execute_input":"2023-11-04T10:45:14.230082Z","iopub.status.idle":"2023-11-04T10:45:14.239394Z","shell.execute_reply.started":"2023-11-04T10:45:14.230044Z","shell.execute_reply":"2023-11-04T10:45:14.238497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_time_extract = process_timestamp(test)\n\ntest_time_extract","metadata":{"execution":{"iopub.status.busy":"2023-11-04T10:46:30.188985Z","iopub.execute_input":"2023-11-04T10:46:30.189897Z","iopub.status.idle":"2023-11-04T10:46:30.204624Z","shell.execute_reply.started":"2023-11-04T10:46:30.189864Z","shell.execute_reply":"2023-11-04T10:46:30.203673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test_time_extract.to_pandas()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T11:20:12.56007Z","iopub.execute_input":"2023-11-04T11:20:12.560537Z","iopub.status.idle":"2023-11-04T11:20:12.567999Z","shell.execute_reply.started":"2023-11-04T11:20:12.560506Z","shell.execute_reply":"2023-11-04T11:20:12.567233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-11-04T10:48:35.655526Z","iopub.execute_input":"2023-11-04T10:48:35.656298Z","iopub.status.idle":"2023-11-04T10:48:35.674528Z","shell.execute_reply.started":"2023-11-04T10:48:35.656269Z","shell.execute_reply":"2023-11-04T10:48:35.673602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = [\"anglez\", \"enmo\", \"year\", \"month\", \"day\", \"hour\", \"minute\", \"second\", \"time_zone\"]\n# train[0].pd_dataframe()\n\ntest = test[features]\ntest","metadata":{"execution":{"iopub.status.busy":"2023-11-04T11:20:15.557683Z","iopub.execute_input":"2023-11-04T11:20:15.558021Z","iopub.status.idle":"2023-11-04T11:20:15.575768Z","shell.execute_reply.started":"2023-11-04T11:20:15.557996Z","shell.execute_reply":"2023-11-04T11:20:15.574798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = TimeSeries.from_dataframe(test.reset_index(drop=True))\ntest","metadata":{"execution":{"iopub.status.busy":"2023-11-04T11:20:20.354691Z","iopub.execute_input":"2023-11-04T11:20:20.355048Z","iopub.status.idle":"2023-11-04T11:20:20.374161Z","shell.execute_reply.started":"2023-11-04T11:20:20.355017Z","shell.execute_reply":"2023-11-04T11:20:20.373236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-04T10:55:51.606587Z","iopub.execute_input":"2023-11-04T10:55:51.606981Z","iopub.status.idle":"2023-11-04T10:55:51.622235Z","shell.execute_reply.started":"2023-11-04T10:55:51.606952Z","shell.execute_reply":"2023-11-04T10:55:51.621174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.pd_dataframe().reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T11:04:07.815044Z","iopub.execute_input":"2023-11-04T11:04:07.815689Z","iopub.status.idle":"2023-11-04T11:04:07.84032Z","shell.execute_reply.started":"2023-11-04T11:04:07.815658Z","shell.execute_reply":"2023-11-04T11:04:07.839377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating bool series True for NaN values  \nbool_series = test.pd_dataframe().reset_index().isnull().any(axis=1)\n# bool_series = pd.isnull() \nbool_series\n# filtering data  \n# displaying data only with Gender = NaN  \ntest.pd_dataframe().reset_index()[bool_series]  ","metadata":{"execution":{"iopub.status.busy":"2023-11-04T11:06:44.506163Z","iopub.execute_input":"2023-11-04T11:06:44.506793Z","iopub.status.idle":"2023-11-04T11:06:44.5203Z","shell.execute_reply.started":"2023-11-04T11:06:44.506761Z","shell.execute_reply":"2023-11-04T11:06:44.51932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[features]","metadata":{"execution":{"iopub.status.busy":"2023-11-04T11:17:46.187504Z","iopub.execute_input":"2023-11-04T11:17:46.187891Z","iopub.status.idle":"2023-11-04T11:17:46.204981Z","shell.execute_reply.started":"2023-11-04T11:17:46.187862Z","shell.execute_reply":"2023-11-04T11:17:46.204033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = []\n# for i in range(len(test)):\npred.append(model.predict(1,test[features]))\npred","metadata":{"execution":{"iopub.status.busy":"2023-11-04T11:20:34.915286Z","iopub.execute_input":"2023-11-04T11:20:34.916Z","iopub.status.idle":"2023-11-04T11:20:35.062686Z","shell.execute_reply.started":"2023-11-04T11:20:34.91597Z","shell.execute_reply":"2023-11-04T11:20:35.061211Z"},"trusted":true},"execution_count":null,"outputs":[]}]}