{"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":"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\n\ntrain_series = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet',engine='pyarrow')\ntrain_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-27T04:03:10.345151Z","iopub.execute_input":"2023-09-27T04:03:10.345543Z","iopub.status.idle":"2023-09-27T04:04:00.916488Z","shell.execute_reply.started":"2023-09-27T04:03:10.345513Z","shell.execute_reply":"2023-09-27T04:04:00.914898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:04:28.287810Z","iopub.execute_input":"2023-09-27T04:04:28.288191Z","iopub.status.idle":"2023-09-27T04:04:28.302554Z","shell.execute_reply.started":"2023-09-27T04:04:28.288161Z","shell.execute_reply":"2023-09-27T04:04:28.300989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:04:31.219802Z","iopub.execute_input":"2023-09-27T04:04:31.220184Z","iopub.status.idle":"2023-09-27T04:04:31.233801Z","shell.execute_reply.started":"2023-09-27T04:04:31.220155Z","shell.execute_reply":"2023-09-27T04:04:31.232684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data=pd.merge(train_series,train_events,how='inner')","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:04:36.699132Z","iopub.execute_input":"2023-09-27T04:04:36.699749Z","iopub.status.idle":"2023-09-27T04:07:58.022217Z","shell.execute_reply.started":"2023-09-27T04:04:36.699715Z","shell.execute_reply":"2023-09-27T04:07:58.020817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:09:03.310093Z","iopub.execute_input":"2023-09-27T04:09:03.310533Z","iopub.status.idle":"2023-09-27T04:09:03.329805Z","shell.execute_reply.started":"2023-09-27T04:09:03.310498Z","shell.execute_reply":"2023-09-27T04:09:03.328619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:09:11.910015Z","iopub.execute_input":"2023-09-27T04:09:11.910387Z","iopub.status.idle":"2023-09-27T04:09:11.923045Z","shell.execute_reply.started":"2023-09-27T04:09:11.910358Z","shell.execute_reply":"2023-09-27T04:09:11.921760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tstamp(convert):\n    convert['timestamp'] = pd.to_datetime(convert['timestamp']).apply(lambda t: t.tz_localize(None))\n    convert[\"hour\"] = convert[\"timestamp\"].dt.hour\n    \n    return convert","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:09:23.289034Z","iopub.execute_input":"2023-09-27T04:09:23.289435Z","iopub.status.idle":"2023-09-27T04:09:23.296295Z","shell.execute_reply.started":"2023-09-27T04:09:23.289404Z","shell.execute_reply":"2023-09-27T04:09:23.294868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data=tstamp(all_data)","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:09:26.400489Z","iopub.execute_input":"2023-09-27T04:09:26.401249Z","iopub.status.idle":"2023-09-27T04:09:26.984343Z","shell.execute_reply.started":"2023-09-27T04:09:26.401197Z","shell.execute_reply":"2023-09-27T04:09:26.983215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:09:30.260214Z","iopub.execute_input":"2023-09-27T04:09:30.260869Z","iopub.status.idle":"2023-09-27T04:09:30.277160Z","shell.execute_reply.started":"2023-09-27T04:09:30.260824Z","shell.execute_reply":"2023-09-27T04:09:30.275678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data.info()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:09:50.413260Z","iopub.execute_input":"2023-09-27T04:09:50.413688Z","iopub.status.idle":"2023-09-27T04:09:50.430627Z","shell.execute_reply.started":"2023-09-27T04:09:50.413654Z","shell.execute_reply":"2023-09-27T04:09:50.429214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data.describe()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:09:57.941181Z","iopub.execute_input":"2023-09-27T04:09:57.941574Z","iopub.status.idle":"2023-09-27T04:09:57.981302Z","shell.execute_reply.started":"2023-09-27T04:09:57.941542Z","shell.execute_reply":"2023-09-27T04:09:57.980025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Encoding the object data:\nall_data['event'].value_counts()\nall_data['event']=all_data['event'].astype('category')\nall_data['event']=all_data['event'].cat.codes\n","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:10:03.972843Z","iopub.execute_input":"2023-09-27T04:10:03.973236Z","iopub.status.idle":"2023-09-27T04:10:03.983307Z","shell.execute_reply.started":"2023-09-27T04:10:03.973205Z","shell.execute_reply":"2023-09-27T04:10:03.982168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:10:08.973654Z","iopub.execute_input":"2023-09-27T04:10:08.974052Z","iopub.status.idle":"2023-09-27T04:10:09.017038Z","shell.execute_reply.started":"2023-09-27T04:10:08.974020Z","shell.execute_reply":"2023-09-27T04:10:09.016142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=tstamp(test)","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:10:40.009881Z","iopub.execute_input":"2023-09-27T04:10:40.010283Z","iopub.status.idle":"2023-09-27T04:10:40.028207Z","shell.execute_reply.started":"2023-09-27T04:10:40.010250Z","shell.execute_reply":"2023-09-27T04:10:40.027018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:10:49.656132Z","iopub.execute_input":"2023-09-27T04:10:49.656561Z","iopub.status.idle":"2023-09-27T04:10:49.670575Z","shell.execute_reply.started":"2023-09-27T04:10:49.656527Z","shell.execute_reply":"2023-09-27T04:10:49.669636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=['step','anglez','enmo','hour']\nx_train=all_data[features]\nx_test=test[features]\ny_train=all_data['event']","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:13:45.571049Z","iopub.execute_input":"2023-09-27T04:13:45.571481Z","iopub.status.idle":"2023-09-27T04:13:45.581260Z","shell.execute_reply.started":"2023-09-27T04:13:45.571447Z","shell.execute_reply":"2023-09-27T04:13:45.579078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:13:55.483973Z","iopub.execute_input":"2023-09-27T04:13:55.484398Z","iopub.status.idle":"2023-09-27T04:13:55.499894Z","shell.execute_reply.started":"2023-09-27T04:13:55.484363Z","shell.execute_reply":"2023-09-27T04:13:55.498581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['event'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:11:45.852170Z","iopub.execute_input":"2023-09-27T04:11:45.852607Z","iopub.status.idle":"2023-09-27T04:11:45.867405Z","shell.execute_reply.started":"2023-09-27T04:11:45.852572Z","shell.execute_reply":"2023-09-27T04:11:45.864870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\ndtree = DecisionTreeClassifier()\ndtree.fit(x_train, y_train)\n\ny_test=dtree.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:14:02.921996Z","iopub.execute_input":"2023-09-27T04:14:02.922381Z","iopub.status.idle":"2023-09-27T04:14:02.968691Z","shell.execute_reply.started":"2023-09-27T04:14:02.922350Z","shell.execute_reply":"2023-09-27T04:14:02.967475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nrf = RandomForestClassifier()\nrf.fit(x_train, y_train)\nprediction=rf.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:14:07.189278Z","iopub.execute_input":"2023-09-27T04:14:07.190658Z","iopub.status.idle":"2023-09-27T04:14:08.430251Z","shell.execute_reply.started":"2023-09-27T04:14:07.190613Z","shell.execute_reply":"2023-09-27T04:14:08.429216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=test[['series_id','step']]","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:14:19.995956Z","iopub.execute_input":"2023-09-27T04:14:19.996370Z","iopub.status.idle":"2023-09-27T04:14:20.004148Z","shell.execute_reply.started":"2023-09-27T04:14:19.996338Z","shell.execute_reply":"2023-09-27T04:14:20.002709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['event']=prediction","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:18:18.127625Z","iopub.execute_input":"2023-09-27T04:18:18.128102Z","iopub.status.idle":"2023-09-27T04:18:18.135018Z","shell.execute_reply.started":"2023-09-27T04:18:18.128066Z","shell.execute_reply":"2023-09-27T04:18:18.133859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-09-27T04:19:39.831461Z","iopub.execute_input":"2023-09-27T04:19:39.832229Z","iopub.status.idle":"2023-09-27T04:19:39.848744Z","shell.execute_reply.started":"2023-09-27T04:19:39.832153Z","shell.execute_reply":"2023-09-27T04:19:39.846553Z"},"trusted":true},"execution_count":null,"outputs":[]}]}