{"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":"# **การเลือกข้อมูล:**\n* **มีชุดข้อมูลหลัก 2 ชุด:** \n    * train_events และ train_series","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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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\nimport os\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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-31T12:59:36.074769Z","iopub.execute_input":"2023-10-31T12:59:36.075160Z","iopub.status.idle":"2023-10-31T12:59:36.086138Z","shell.execute_reply.started":"2023-10-31T12:59:36.075105Z","shell.execute_reply":"2023-10-31T12:59:36.084921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/child-mind-institute-detect-sleep-states/'\ntrain_events = pd.read_csv(path + 'train_events.csv')\ntrain_series = pd.read_parquet(path + 'train_series.parquet')\nprint('Reading Data done!')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:59:36.088248Z","iopub.execute_input":"2023-10-31T12:59:36.088547Z","iopub.status.idle":"2023-10-31T13:00:25.909785Z","shell.execute_reply.started":"2023-10-31T12:59:36.088521Z","shell.execute_reply":"2023-10-31T13:00:25.908535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_events.head())\nprint(train_series.head())","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:00:25.911206Z","iopub.execute_input":"2023-10-31T13:00:25.911518Z","iopub.status.idle":"2023-10-31T13:00:25.923328Z","shell.execute_reply.started":"2023-10-31T13:00:25.911490Z","shell.execute_reply":"2023-10-31T13:00:25.921987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **การประมวลผลข้อมูล:**\n* **การจัดการข้อมูลที่ขาดหาย:**\n    * ข้อมูลที่ขาดหายในชุดข้อมูล train_events ถูกลบเพื่อให้ได้ข้อมูลที่มีคุณภาพ\n* **การเรียงลำดับข้อมูล:**\n    * รีเซ็ต index ของชุดข้อมูล train_events เพื่อให้มีความเป็นระเบียบ\n* **การรวมข้อมูล:**\n    * เหตุการณ์ประเภท 'onset' ที่ตามด้วย 'wakeup' ในคืนเดียวกันถูกผสมเข้าด้วยกันเป็นข้อมูลเดียว\n* **การแยกข้อมูล:**\n    * แยกวันและเวลาออกเป็น 2 ส่วน ได้แก่ start_time และ end_time\n    * แยก step ออกเป็น 2 ส่วน ได้แก่ start_step และ end_step","metadata":{}},{"cell_type":"code","source":"train_events = train_events.dropna(axis=0)\n# Reset the index of the DataFrame\ntrain_events = train_events.reset_index(drop=True)\nprint('Drop Null Data Complete and reset index')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:00:25.924948Z","iopub.execute_input":"2023-10-31T13:00:25.925378Z","iopub.status.idle":"2023-10-31T13:00:25.944658Z","shell.execute_reply.started":"2023-10-31T13:00:25.925337Z","shell.execute_reply":"2023-10-31T13:00:25.943417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def format_date(date_string):\n    if pd.notnull(date_string):\n        date_time = pd.to_datetime(date_string)\n        \n        if not pd.isna(date_time):  # Check if date_time is not 'NaT'\n            formatted_date = date_time.strftime(\"%Y-%m-%d %H:%M:%S\")\n            return formatted_date\n        else:\n            return date_string\n    else:\n        return date_string\n\ndef merge_events(train_events_df):\n    train_events_df['start_step'] = 0.0\n    train_events_df['end_step'] = 0.0\n    train_events_df['start_time'] = \"\"\n    train_events_df['end_time'] = \"\"\n    for i in range(len(train_events_df)-1):\n        if (train_events_df.loc[i, 'event'] == 'onset' and \n            train_events_df.loc[i+1, 'event'] == 'wakeup' and\n            train_events_df.loc[i, 'night'] == train_events_df.loc[i+1, 'night']):\n            \n            train_events_df.loc[i, 'start_step'] = train_events_df.loc[i, 'step']\n            train_events_df.loc[i, 'end_step'] = train_events_df.loc[i+1, 'step']\n            train_events_df.loc[i, 'start_time'] = str(train_events_df.loc[i, 'timestamp'])\n            train_events_df.loc[i, 'end_time'] = str(train_events_df.loc[i+1, 'timestamp'])\n    \n    return train_events_df\n\nprint('Set up function complete')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:00:25.947526Z","iopub.execute_input":"2023-10-31T13:00:25.947849Z","iopub.status.idle":"2023-10-31T13:00:25.959361Z","shell.execute_reply.started":"2023-10-31T13:00:25.947820Z","shell.execute_reply":"2023-10-31T13:00:25.958153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events = merge_events(train_events)\nprint('merge data complete')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:00:25.960656Z","iopub.execute_input":"2023-10-31T13:00:25.960972Z","iopub.status.idle":"2023-10-31T13:00:29.586800Z","shell.execute_reply.started":"2023-10-31T13:00:25.960943Z","shell.execute_reply":"2023-10-31T13:00:29.585475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **การทำความสะอาดข้อมูล:**\n* **การจัดการข้อมูลที่ไม่ได้ใช้:**\n    * ลบแถว(column)ชื่อ event, timestamp, step\n    * ลบ UTC ในข้อมูลเวลาออก","metadata":{}},{"cell_type":"code","source":"train_events = train_events[train_events['event'] != 'wakeup']\ntrain_events = train_events.drop(columns=['timestamp', 'event', 'step'])\ntrain_events = train_events.reset_index(drop=True)\ntrain_events[['start_time', 'end_time']] = train_events[['start_time', 'end_time']].apply(lambda x: x.apply(format_date))\nprint('Drop column and convert format date')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:00:29.588435Z","iopub.execute_input":"2023-10-31T13:00:29.588845Z","iopub.status.idle":"2023-10-31T13:00:40.957280Z","shell.execute_reply.started":"2023-10-31T13:00:29.588812Z","shell.execute_reply":"2023-10-31T13:00:40.956169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_events.head())\nprint(train_series.head())","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:00:40.959375Z","iopub.execute_input":"2023-10-31T13:00:40.959807Z","iopub.status.idle":"2023-10-31T13:00:40.972452Z","shell.execute_reply.started":"2023-10-31T13:00:40.959763Z","shell.execute_reply":"2023-10-31T13:00:40.971294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Submission**","metadata":{}},{"cell_type":"code","source":"train_events.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:00:40.974212Z","iopub.execute_input":"2023-10-31T13:00:40.974910Z","iopub.status.idle":"2023-10-31T13:00:41.027417Z","shell.execute_reply.started":"2023-10-31T13:00:40.974864Z","shell.execute_reply":"2023-10-31T13:00:41.026206Z"},"trusted":true},"execution_count":null,"outputs":[]}]}