{"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\n# import os\n# for 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-06T11:51:49.048808Z","iopub.execute_input":"2023-10-06T11:51:49.049137Z","iopub.status.idle":"2023-10-06T11:51:49.421266Z","shell.execute_reply.started":"2023-10-06T11:51:49.049111Z","shell.execute_reply":"2023-10-06T11:51:49.420469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Since the dataset is large, to avoid long times, preprocessing and EDA were carried out in the notebook <a href=https://www.kaggle.com/code/enricomanosperti/detect-sleep-states-preprocessing-and-eda> Detect Sleep States: Preprocessing and EDA</a> <br>\nThe dataset that will be used here is the one created in the previous notebook <a href=https://www.kaggle.com/datasets/enricomanosperti/sleep-states-preprocessed-dataset> Sleep States Preprocessed Dataset</a>\n","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:51:49.422693Z","iopub.execute_input":"2023-10-06T11:51:49.423288Z","iopub.status.idle":"2023-10-06T11:51:58.833367Z","shell.execute_reply.started":"2023-10-06T11:51:49.423260Z","shell.execute_reply":"2023-10-06T11:51:58.832256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nimport polars as pl\ntrain_series = (pl.scan_parquet('/kaggle/input/sleep-states-preprocessed-dataset/train_series_preprocessed.parquet')\n                .with_columns(\n                    (\n                        (pl.col(\"timestamp\").dt.hour().alias(\"hour\")),\n                    )\n                )\n                .collect()\n                .to_pandas()\n                .drop(columns='timestamp')\n               )\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:51:58.834627Z","iopub.execute_input":"2023-10-06T11:51:58.835335Z","iopub.status.idle":"2023-10-06T11:53:01.160032Z","shell.execute_reply.started":"2023-10-06T11:51:58.835285Z","shell.execute_reply":"2023-10-06T11:53:01.158785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:53:01.163365Z","iopub.execute_input":"2023-10-06T11:53:01.164150Z","iopub.status.idle":"2023-10-06T11:53:01.187419Z","shell.execute_reply.started":"2023-10-06T11:53:01.164083Z","shell.execute_reply":"2023-10-06T11:53:01.186284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest_series = (pl.scan_parquet('/kaggle/input/sleep-states-preprocessed-dataset/test_series_preprocessed.parquet')\n                .with_columns(\n                    (\n                        (pl.col(\"timestamp\").dt.hour().alias(\"hour\")),\n                    )\n                )\n                .collect()\n                .to_pandas()\n                .drop(columns='timestamp')\n               )","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:53:01.189092Z","iopub.execute_input":"2023-10-06T11:53:01.189789Z","iopub.status.idle":"2023-10-06T11:53:01.214972Z","shell.execute_reply.started":"2023-10-06T11:53:01.189748Z","shell.execute_reply":"2023-10-06T11:53:01.213682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_series.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:53:01.216792Z","iopub.execute_input":"2023-10-06T11:53:01.217913Z","iopub.status.idle":"2023-10-06T11:53:01.232499Z","shell.execute_reply.started":"2023-10-06T11:53:01.217869Z","shell.execute_reply":"2023-10-06T11:53:01.230953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create input and output","metadata":{}},{"cell_type":"code","source":"features = ['anglez', 'enmo', 'hour']\nencode_map = {'wakeup':0, 'onset':1}","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:53:01.234544Z","iopub.execute_input":"2023-10-06T11:53:01.235422Z","iopub.status.idle":"2023-10-06T11:53:01.244118Z","shell.execute_reply.started":"2023-10-06T11:53:01.235378Z","shell.execute_reply":"2023-10-06T11:53:01.243273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_series[features]\ny = train_series.event.map(encode_map)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:53:01.245923Z","iopub.execute_input":"2023-10-06T11:53:01.246785Z","iopub.status.idle":"2023-10-06T11:53:12.492989Z","shell.execute_reply.started":"2023-10-06T11:53:01.246741Z","shell.execute_reply":"2023-10-06T11:53:12.492006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:53:12.494294Z","iopub.execute_input":"2023-10-06T11:53:12.494565Z","iopub.status.idle":"2023-10-06T11:53:48.479888Z","shell.execute_reply.started":"2023-10-06T11:53:12.494542Z","shell.execute_reply":"2023-10-06T11:53:48.478835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n    \n    model = tf.keras.models.Sequential()\n    model.add(tf.keras.layers.BatchNormalization(input_shape=(3,)))\n    model.add(tf.keras.layers.Dropout(0.5))\n    model.add(tf.keras.layers.Dense(32, activation='relu'))\n    model.add(tf.keras.layers.Dropout(0.5))\n#     model.add(tf.keras.layers.Dense(16, activation='relu'))\n#     model.add(tf.keras.layers.Dropout(0.2))\n    model.add(tf.keras.layers.Dense(2, activation='softmax'))\n    \n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(),\n        loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n        metrics=['accuracy']\n    )\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:53:48.483602Z","iopub.execute_input":"2023-10-06T11:53:48.484082Z","iopub.status.idle":"2023-10-06T11:53:48.491152Z","shell.execute_reply.started":"2023-10-06T11:53:48.484037Z","shell.execute_reply":"2023-10-06T11:53:48.489757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:53:48.492775Z","iopub.execute_input":"2023-10-06T11:53:48.493101Z","iopub.status.idle":"2023-10-06T11:53:48.717523Z","shell.execute_reply.started":"2023-10-06T11:53:48.493068Z","shell.execute_reply":"2023-10-06T11:53:48.716378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train, \n          epochs=1,\n          batch_size=10000,\n          validation_data=(X_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:53:48.720330Z","iopub.execute_input":"2023-10-06T11:53:48.720800Z","iopub.status.idle":"2023-10-06T11:56:26.192653Z","shell.execute_reply.started":"2023-10-06T11:53:48.720773Z","shell.execute_reply":"2023-10-06T11:56:26.191394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_series['event'] = np.argmax(model.predict(test_series[features]), axis=1)\n# test_series['score'] = np.max(model.predict(test_series[features]), axis=1)\n\ntest_series['score_wakeup'] = np.transpose(model.predict(test_series[features]))[0]\ntest_series['score_onset'] = np.transpose(model.predict(test_series[features]))[1]","metadata":{"execution":{"iopub.status.busy":"2023-10-06T11:56:26.195040Z","iopub.execute_input":"2023-10-06T11:56:26.195856Z","iopub.status.idle":"2023-10-06T11:56:26.506048Z","shell.execute_reply.started":"2023-10-06T11:56:26.195819Z","shell.execute_reply":"2023-10-06T11:56:26.505039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:00:09.441683Z","iopub.execute_input":"2023-10-06T12:00:09.442034Z","iopub.status.idle":"2023-10-06T12:00:09.464355Z","shell.execute_reply.started":"2023-10-06T12:00:09.442006Z","shell.execute_reply":"2023-10-06T12:00:09.463491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:00:11.345220Z","iopub.execute_input":"2023-10-06T12:00:11.345874Z","iopub.status.idle":"2023-10-06T12:00:11.357918Z","shell.execute_reply.started":"2023-10-06T12:00:11.345832Z","shell.execute_reply":"2023-10-06T12:00:11.356688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:00:36.310764Z","iopub.execute_input":"2023-10-06T12:00:36.311122Z","iopub.status.idle":"2023-10-06T12:00:36.334035Z","shell.execute_reply.started":"2023-10-06T12:00:36.311095Z","shell.execute_reply":"2023-10-06T12:00:36.333272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_sample_submission = sample_submission.merge(test_series, on=['series_id', 'step'])\nnew_sample_submission","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:50:33.556814Z","iopub.execute_input":"2023-10-06T12:50:33.557172Z","iopub.status.idle":"2023-10-06T12:50:33.578243Z","shell.execute_reply.started":"2023-10-06T12:50:33.557145Z","shell.execute_reply":"2023-10-06T12:50:33.577084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_sample_submission.loc[new_sample_submission.event == 'onset', 'score'] = new_sample_submission.score_onset.round(0)\nnew_sample_submission.loc[new_sample_submission.event == 'wakeup', 'score'] = new_sample_submission.score_wakeup.round(0)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:50:34.069090Z","iopub.execute_input":"2023-10-06T12:50:34.069469Z","iopub.status.idle":"2023-10-06T12:50:34.078750Z","shell.execute_reply.started":"2023-10-06T12:50:34.069441Z","shell.execute_reply":"2023-10-06T12:50:34.077407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_sample_submission = new_sample_submission[sample_submission.columns]","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:50:34.937181Z","iopub.execute_input":"2023-10-06T12:50:34.937545Z","iopub.status.idle":"2023-10-06T12:50:34.944127Z","shell.execute_reply.started":"2023-10-06T12:50:34.937519Z","shell.execute_reply":"2023-10-06T12:50:34.943012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_sample_submission","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:50:35.689668Z","iopub.execute_input":"2023-10-06T12:50:35.690020Z","iopub.status.idle":"2023-10-06T12:50:35.701274Z","shell.execute_reply.started":"2023-10-06T12:50:35.689994Z","shell.execute_reply":"2023-10-06T12:50:35.700222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_sample_submission.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:50:38.386982Z","iopub.execute_input":"2023-10-06T12:50:38.387382Z","iopub.status.idle":"2023-10-06T12:50:38.401889Z","shell.execute_reply.started":"2023-10-06T12:50:38.387351Z","shell.execute_reply":"2023-10-06T12:50:38.400520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = sample_submission.copy()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:50:38.606065Z","iopub.execute_input":"2023-10-06T12:50:38.606753Z","iopub.status.idle":"2023-10-06T12:50:38.612228Z","shell.execute_reply.started":"2023-10-06T12:50:38.606710Z","shell.execute_reply":"2023-10-06T12:50:38.611076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['score'] = new_sample_submission.score","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:50:39.119161Z","iopub.execute_input":"2023-10-06T12:50:39.120364Z","iopub.status.idle":"2023-10-06T12:50:39.124428Z","shell.execute_reply.started":"2023-10-06T12:50:39.120323Z","shell.execute_reply":"2023-10-06T12:50:39.123659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:50:46.854097Z","iopub.execute_input":"2023-10-06T12:50:46.855267Z","iopub.status.idle":"2023-10-06T12:50:46.866285Z","shell.execute_reply.started":"2023-10-06T12:50:46.855215Z","shell.execute_reply":"2023-10-06T12:50:46.865178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T12:50:52.622681Z","iopub.execute_input":"2023-10-06T12:50:52.623793Z","iopub.status.idle":"2023-10-06T12:50:52.632008Z","shell.execute_reply.started":"2023-10-06T12:50:52.623754Z","shell.execute_reply":"2023-10-06T12:50:52.631247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}