{"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":"Thanks->\nMEER ATIF MAGSI  🧒Sleep States Child || Analysis and visualization","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport time\nimport seaborn as sns\nimport pyarrow.parquet as pq","metadata":{"execution":{"iopub.status.busy":"2023-10-20T17:57:52.661422Z","iopub.execute_input":"2023-10-20T17:57:52.661871Z","iopub.status.idle":"2023-10-20T17:57:54.417157Z","shell.execute_reply.started":"2023-10-20T17:57:52.661843Z","shell.execute_reply":"2023-10-20T17:57:54.416202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\n# drop NAN rows\ntrain_events = train_events.dropna(axis=0)\ntrain_events","metadata":{"execution":{"iopub.status.busy":"2023-10-20T17:57:57.722962Z","iopub.execute_input":"2023-10-20T17:57:57.724141Z","iopub.status.idle":"2023-10-20T17:57:57.857996Z","shell.execute_reply.started":"2023-10-20T17:57:57.724105Z","shell.execute_reply":"2023-10-20T17:57:57.857088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('************************************************','Rows and Columns of Train Events', train_events.shape,'************************************************')\nprint('***********************************************','Missing values in Train Events data', train_events.isna().any().any(),'************************************************')\nprint('************************************************','Missing Values in Train Events', train_events[train_events.duplicated()].sum(),'************************************************')","metadata":{"execution":{"iopub.status.busy":"2023-10-20T17:58:14.001603Z","iopub.execute_input":"2023-10-20T17:58:14.001943Z","iopub.status.idle":"2023-10-20T17:58:14.027592Z","shell.execute_reply.started":"2023-10-20T17:58:14.001913Z","shell.execute_reply":"2023-10-20T17:58:14.026598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* pairs (equal number) of onset/wakeup\n* alternating onset/wakeup events?","metadata":{}},{"cell_type":"code","source":"print('************************************************\\n',train_events['event'].value_counts(), '\\n************************************************')\nplt.figure(figsize=(4,4))\n\nax = sns.countplot(data=train_events, x='event')\nax.bar_label(ax.containers[0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-20T17:58:26.283252Z","iopub.execute_input":"2023-10-20T17:58:26.284001Z","iopub.status.idle":"2023-10-20T17:58:26.543219Z","shell.execute_reply.started":"2023-10-20T17:58:26.283963Z","shell.execute_reply":"2023-10-20T17:58:26.542195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events['night']","metadata":{"execution":{"iopub.status.busy":"2023-10-20T17:59:07.387751Z","iopub.execute_input":"2023-10-20T17:59:07.388563Z","iopub.status.idle":"2023-10-20T17:59:07.396310Z","shell.execute_reply.started":"2023-10-20T17:59:07.388528Z","shell.execute_reply":"2023-10-20T17:59:07.395419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# FIRST person in dataframe\n#filtered_df = train_events.loc[(train_events['series_id'] == '038441c925bb')]\nfiltered_df = train_events.loc[(train_events['series_id'] == train_events.loc[0]['series_id'])]\nprint(filtered_df)","metadata":{"execution":{"iopub.status.busy":"2023-10-20T17:59:44.211569Z","iopub.execute_input":"2023-10-20T17:59:44.212464Z","iopub.status.idle":"2023-10-20T17:59:44.225851Z","shell.execute_reply.started":"2023-10-20T17:59:44.212431Z","shell.execute_reply":"2023-10-20T17:59:44.224907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# MIDDLEish person in dataframe\n#filtered_df = train_events.loc[(train_events['series_id'] == 'fe90110788d2')]\nfiltered_df = train_events.loc[(train_events['series_id'] == train_events.loc[7011]['series_id'])]\n#print(filtered_df)\n\nwith pd.option_context('display.max_rows', None,):\n    print(filtered_df)","metadata":{"execution":{"iopub.status.busy":"2023-10-20T18:02:30.665020Z","iopub.execute_input":"2023-10-20T18:02:30.665383Z","iopub.status.idle":"2023-10-20T18:02:30.677968Z","shell.execute_reply.started":"2023-10-20T18:02:30.665354Z","shell.execute_reply":"2023-10-20T18:02:30.676915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# LAST person in dataframe\n#filtered_df = train_events.loc[(train_events['series_id'] == 'fe90110788d2')]\nfiltered_df = train_events.loc[(train_events['series_id'] == train_events.loc[14503]['series_id'])]\n#print(filtered_df)\n\nwith pd.option_context('display.max_rows', None,):\n    print(filtered_df)","metadata":{"execution":{"iopub.status.busy":"2023-10-20T18:02:36.979020Z","iopub.execute_input":"2023-10-20T18:02:36.979388Z","iopub.status.idle":"2023-10-20T18:02:36.995622Z","shell.execute_reply.started":"2023-10-20T18:02:36.979358Z","shell.execute_reply":"2023-10-20T18:02:36.994604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('************************************************\\n',train_events['night'].value_counts(), '\\n************************************************')\nplt.figure(figsize=(6,4))\n\nax = plt.plot(list(train_events['night']))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-05T14:46:42.411993Z","iopub.execute_input":"2023-10-05T14:46:42.412635Z","iopub.status.idle":"2023-10-05T14:46:42.653402Z","shell.execute_reply.started":"2023-10-05T14:46:42.412605Z","shell.execute_reply":"2023-10-05T14:46:42.652341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# find max night -> train_events['night']\nprint(train_events.loc[train_events['night'].idxmax()])","metadata":{"execution":{"iopub.status.busy":"2023-10-20T18:03:48.145808Z","iopub.execute_input":"2023-10-20T18:03:48.146284Z","iopub.status.idle":"2023-10-20T18:03:48.152678Z","shell.execute_reply.started":"2023-10-20T18:03:48.146249Z","shell.execute_reply":"2023-10-20T18:03:48.151724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events['date'] = train_events['timestamp'].str.split('T', expand=True)[0]\ntrain_events['time'] = train_events['timestamp'].str.split('T', expand=True)[1].str.split('-', expand=True)[0]\ntrain_events['timestamp'] = pd.to_datetime(train_events['date']+' '+train_events['time'])\ntrain_events['hour'] = train_events['timestamp'].dt.hour\ntrain_events","metadata":{"execution":{"iopub.status.busy":"2023-10-20T18:03:58.910971Z","iopub.execute_input":"2023-10-20T18:03:58.911658Z","iopub.status.idle":"2023-10-20T18:03:58.995798Z","shell.execute_reply.started":"2023-10-20T18:03:58.911626Z","shell.execute_reply":"2023-10-20T18:03:58.994901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_events.columns)","metadata":{"execution":{"iopub.status.busy":"2023-10-20T18:04:23.160812Z","iopub.execute_input":"2023-10-20T18:04:23.161136Z","iopub.status.idle":"2023-10-20T18:04:23.166984Z","shell.execute_reply.started":"2023-10-20T18:04:23.161111Z","shell.execute_reply":"2023-10-20T18:04:23.166115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6, 6))\nplt.title('Hours Distribution')\nsns.kdeplot(train_events['hour'],color='red');","metadata":{"execution":{"iopub.status.busy":"2023-10-20T18:04:29.231825Z","iopub.execute_input":"2023-10-20T18:04:29.232516Z","iopub.status.idle":"2023-10-20T18:04:29.606266Z","shell.execute_reply.started":"2023-10-20T18:04:29.232482Z","shell.execute_reply":"2023-10-20T18:04:29.605337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.jointplot(data=train_events, x='hour', y='event')","metadata":{"execution":{"iopub.status.busy":"2023-10-05T14:46:43.250589Z","iopub.execute_input":"2023-10-05T14:46:43.250899Z","iopub.status.idle":"2023-10-05T14:46:44.167081Z","shell.execute_reply.started":"2023-10-05T14:46:43.250866Z","shell.execute_reply":"2023-10-05T14:46:44.165399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.jointplot(data=train_events, x='step', y='event')","metadata":{"execution":{"iopub.status.busy":"2023-10-05T14:46:44.169152Z","iopub.execute_input":"2023-10-05T14:46:44.170494Z","iopub.status.idle":"2023-10-05T14:46:45.065859Z","shell.execute_reply.started":"2023-10-05T14:46:44.170444Z","shell.execute_reply":"2023-10-05T14:46:45.064187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.jointplot(data=train_events, x='night', y='event')","metadata":{"execution":{"iopub.status.busy":"2023-10-05T14:46:45.067603Z","iopub.execute_input":"2023-10-05T14:46:45.068040Z","iopub.status.idle":"2023-10-05T14:46:46.056030Z","shell.execute_reply.started":"2023-10-05T14:46:45.068000Z","shell.execute_reply":"2023-10-05T14:46:46.054612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events.dtypes.value_counts().plot.pie(autopct='%0.2f%%')","metadata":{"execution":{"iopub.status.busy":"2023-10-20T18:04:46.134148Z","iopub.execute_input":"2023-10-20T18:04:46.134730Z","iopub.status.idle":"2023-10-20T18:04:46.295547Z","shell.execute_reply.started":"2023-10-20T18:04:46.134699Z","shell.execute_reply":"2023-10-20T18:04:46.294620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events_new = train_events.drop(['series_id', 'timestamp'], axis='columns')\ntrain_events.dtypes.value_counts().plot.pie(autopct='%0.2f%%')","metadata":{"execution":{"iopub.status.busy":"2023-10-20T18:05:30.494525Z","iopub.execute_input":"2023-10-20T18:05:30.495353Z","iopub.status.idle":"2023-10-20T18:05:30.655598Z","shell.execute_reply.started":"2023-10-20T18:05:30.495318Z","shell.execute_reply":"2023-10-20T18:05:30.654133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# parquet_train_series = '/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet'\n# train_series = pq.read_table(parquet_train_series).to_pandas()\n# # print(train_file)\n# parquet_test_series = '/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet'\n# test_series = pq.read_table(parquet_test_series).to_pandas()\n# display(train_series)\n# display(test_series)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T14:46:46.428743Z","iopub.execute_input":"2023-10-05T14:46:46.429583Z","iopub.status.idle":"2023-10-05T14:46:46.436589Z","shell.execute_reply.started":"2023-10-05T14:46:46.429528Z","shell.execute_reply":"2023-10-05T14:46:46.435376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # FIRST person train metrics\n# filtered_df = train_series.loc[(train_series['series_id'] == train_series.loc[0]['series_id'])]\n# print(filtered_df)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T14:46:46.438526Z","iopub.execute_input":"2023-10-05T14:46:46.439296Z","iopub.status.idle":"2023-10-05T14:46:46.452780Z","shell.execute_reply.started":"2023-10-05T14:46:46.439243Z","shell.execute_reply":"2023-10-05T14:46:46.451047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train events (csv)\ntrain series (parquet)","metadata":{}}]}