{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1>Sleeping Patterns - EDA</h1>","metadata":{}},{"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 matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-10T16:41:42.992378Z","iopub.execute_input":"2023-09-10T16:41:42.992751Z","iopub.status.idle":"2023-09-10T16:41:43.426485Z","shell.execute_reply.started":"2023-09-10T16:41:42.992720Z","shell.execute_reply":"2023-09-10T16:41:43.425429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reading data.\n\nWe have:\n* 2 files in the parquet format (with train and test series)\n* 2 files in the CSV format (with train events and a sample submission)","metadata":{}},{"cell_type":"code","source":"train_series = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\ntrain_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\n\ntest_series = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:41:43.428467Z","iopub.execute_input":"2023-09-10T16:41:43.428966Z","iopub.status.idle":"2023-09-10T16:42:35.740082Z","shell.execute_reply.started":"2023-09-10T16:41:43.428933Z","shell.execute_reply":"2023-09-10T16:42:35.739075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:42:35.741735Z","iopub.execute_input":"2023-09-10T16:42:35.742205Z","iopub.status.idle":"2023-09-10T16:42:35.763571Z","shell.execute_reply.started":"2023-09-10T16:42:35.742160Z","shell.execute_reply":"2023-09-10T16:42:35.762269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Our target variable is `event`. Let's investigate it more in details.","metadata":{}},{"cell_type":"code","source":"train_events['event'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:42:35.764999Z","iopub.execute_input":"2023-09-10T16:42:35.765327Z","iopub.status.idle":"2023-09-10T16:42:35.781618Z","shell.execute_reply.started":"2023-09-10T16:42:35.765285Z","shell.execute_reply":"2023-09-10T16:42:35.780754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 2 `wakeup` more than `onset`.\n\nLet's see the time distribution of these events.","metadata":{}},{"cell_type":"code","source":"from datetime import datetime, timedelta","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:42:35.785009Z","iopub.execute_input":"2023-09-10T16:42:35.785347Z","iopub.status.idle":"2023-09-10T16:42:35.790849Z","shell.execute_reply.started":"2023-09-10T16:42:35.785317Z","shell.execute_reply":"2023-09-10T16:42:35.789797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events_time = train_events[['event', 'timestamp']].dropna()\nevents_time['timestamp'] = events_time['timestamp'].replace({'T': ' '}, regex=True)\nevents_time['timestamp'] = events_time['timestamp'].str.slice(stop=-5)\nevents_time['timestamp'] = pd.to_datetime(events_time['timestamp'], format='%Y-%m-%d %H:%M:%S')\nevents_time['rounded_times'] = events_time.apply(lambda row: row['timestamp'] + (datetime.min - row['timestamp']) % timedelta(minutes=30), axis=1)\n\nonset_times = events_time[events_time['event']=='onset']\nonset_times = onset_times['rounded_times'].dt.time\nonset_counts = onset_times.value_counts().sort_index().reset_index(name='onset')\n\nwakeup_times = events_time[events_time['event']=='wakeup']\nwakeup_times = wakeup_times['rounded_times'].dt.time\nwakeup_counts = wakeup_times.value_counts().sort_index().reset_index(name='wakeup')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:42:35.791969Z","iopub.execute_input":"2023-09-10T16:42:35.792326Z","iopub.status.idle":"2023-09-10T16:42:36.513929Z","shell.execute_reply.started":"2023-09-10T16:42:35.792286Z","shell.execute_reply":"2023-09-10T16:42:36.512977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(onset_counts))\nprint(len(wakeup_counts))","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:42:36.515283Z","iopub.execute_input":"2023-09-10T16:42:36.515605Z","iopub.status.idle":"2023-09-10T16:42:36.521691Z","shell.execute_reply.started":"2023-09-10T16:42:36.515576Z","shell.execute_reply":"2023-09-10T16:42:36.520600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Apparently there are no `wakeup` at 00:00.","metadata":{}},{"cell_type":"code","source":"counts = pd.merge(onset_counts, wakeup_counts, on='rounded_times', how='outer').sort_values('rounded_times')\ncounts.fillna(0, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:45:41.942649Z","iopub.execute_input":"2023-09-10T16:45:41.943806Z","iopub.status.idle":"2023-09-10T16:45:41.954000Z","shell.execute_reply.started":"2023-09-10T16:45:41.943742Z","shell.execute_reply":"2023-09-10T16:45:41.952825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onset_times.describe()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:53:50.962678Z","iopub.execute_input":"2023-09-10T16:53:50.963103Z","iopub.status.idle":"2023-09-10T16:53:50.975029Z","shell.execute_reply.started":"2023-09-10T16:53:50.963071Z","shell.execute_reply":"2023-09-10T16:53:50.973847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wakeup_times.describe()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:54:16.401287Z","iopub.execute_input":"2023-09-10T16:54:16.401662Z","iopub.status.idle":"2023-09-10T16:54:16.412403Z","shell.execute_reply.started":"2023-09-10T16:54:16.401633Z","shell.execute_reply":"2023-09-10T16:54:16.411342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,1, figsize=(12,6))\n\nplt.title('Normalized freqency of onset and wakeup times')\n\nplot_counts = counts.copy()\nplot_counts['rounded_times'] = plot_counts['rounded_times'].astype(str).str.slice(stop=-3)\nax.plot(plot_counts['rounded_times'], plot_counts['onset']/sum(plot_counts['onset']), label='onset')\nax.plot(plot_counts['rounded_times'], plot_counts['wakeup']/sum(plot_counts['wakeup']), label='wakeup')\nax.tick_params(labelrotation=45)\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:45:59.032136Z","iopub.execute_input":"2023-09-10T16:45:59.032544Z","iopub.status.idle":"2023-09-10T16:45:59.609590Z","shell.execute_reply.started":"2023-09-10T16:45:59.032510Z","shell.execute_reply":"2023-09-10T16:45:59.608513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The graph above shows us that:\n* most of the children go to sleep between 19:00 and 1:00\n* the most common time to go to sleep between 21:00-23:00 with a peak at 21:30\n* most of the children wake up between 5:30-9:00\n* the most common time to wake up is between 6:00-8:00 with a strong peak at 7:00\n\nAll of these observations follow a common sense, what is a good indicator.  \n\nNow let's look at the train time series and what they contain.","metadata":{}},{"cell_type":"code","source":"train_series.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:42:36.549243Z","iopub.status.idle":"2023-09-10T16:42:36.549634Z","shell.execute_reply.started":"2023-09-10T16:42:36.549440Z","shell.execute_reply":"2023-09-10T16:42:36.549458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series.describe()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:42:49.565507Z","iopub.execute_input":"2023-09-10T16:42:49.565935Z","iopub.status.idle":"2023-09-10T16:43:00.727060Z","shell.execute_reply.started":"2023-09-10T16:42:49.565901Z","shell.execute_reply":"2023-09-10T16:43:00.725225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_series[\"series_id\"].nunique())\nprint(test_series[\"series_id\"].nunique())","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:42:36.542063Z","iopub.status.idle":"2023-09-10T16:42:36.543016Z","shell.execute_reply.started":"2023-09-10T16:42:36.542794Z","shell.execute_reply":"2023-09-10T16:42:36.542821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have **277** test series but only **3** test series.","metadata":{}},{"cell_type":"code","source":"test_series[\"series_id\"].unique()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:42:36.544165Z","iopub.status.idle":"2023-09-10T16:42:36.544806Z","shell.execute_reply.started":"2023-09-10T16:42:36.544583Z","shell.execute_reply":"2023-09-10T16:42:36.544604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"WORK IN PROGRESS","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}