{"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":"code","source":"import pandas as pd\nimport numpy as np\nfrom datetime import datetime as dt\nfrom itertools import groupby\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nimport optuna\nimport lightgbm as lgb\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nplt.style.use('ggplot')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-13T15:52:41.654033Z","iopub.execute_input":"2023-09-13T15:52:41.654433Z","iopub.status.idle":"2023-09-13T15:52:47.422199Z","shell.execute_reply.started":"2023-09-13T15:52:41.654402Z","shell.execute_reply":"2023-09-13T15:52:47.420845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_parquet('/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train.parquet')\ndf_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:52:47.427310Z","iopub.execute_input":"2023-09-13T15:52:47.427861Z","iopub.status.idle":"2023-09-13T15:52:57.182253Z","shell.execute_reply.started":"2023-09-13T15:52:47.427826Z","shell.execute_reply":"2023-09-13T15:52:57.180988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:52:57.183444Z","iopub.execute_input":"2023-09-13T15:52:57.183871Z","iopub.status.idle":"2023-09-13T15:52:57.189988Z","shell.execute_reply.started":"2023-09-13T15:52:57.183835Z","shell.execute_reply":"2023-09-13T15:52:57.188505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['series_id'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:52:57.193126Z","iopub.execute_input":"2023-09-13T15:52:57.193600Z","iopub.status.idle":"2023-09-13T15:52:57.204354Z","shell.execute_reply.started":"2023-09-13T15:52:57.193555Z","shell.execute_reply":"2023-09-13T15:52:57.202688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:52:57.206110Z","iopub.execute_input":"2023-09-13T15:52:57.206469Z","iopub.status.idle":"2023-09-13T15:52:57.216578Z","shell.execute_reply.started":"2023-09-13T15:52:57.206441Z","shell.execute_reply":"2023-09-13T15:52:57.215635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[df_train['series_id']=='08db4255286f']['awake'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:52:57.218154Z","iopub.execute_input":"2023-09-13T15:52:57.219162Z","iopub.status.idle":"2023-09-13T15:52:57.230914Z","shell.execute_reply.started":"2023-09-13T15:52:57.219029Z","shell.execute_reply":"2023-09-13T15:52:57.229632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(20, 3))\nsns.lineplot(data=df_train[df_train['series_id']=='08db4255286f'], x=\"step\", y=\"anglez\",hue=\"awake\", linewidth = 0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:52:57.232549Z","iopub.execute_input":"2023-09-13T15:52:57.233771Z","iopub.status.idle":"2023-09-13T15:52:57.243231Z","shell.execute_reply.started":"2023-09-13T15:52:57.233732Z","shell.execute_reply":"2023-09-13T15:52:57.242177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(20, 3))\nsns.lineplot(data=df_train[df_train['series_id']=='08db4255286f'], x=\"step\", y=\"enmo\",hue=\"awake\", linewidth = 0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:52:57.245517Z","iopub.execute_input":"2023-09-13T15:52:57.246206Z","iopub.status.idle":"2023-09-13T15:52:57.258376Z","shell.execute_reply.started":"2023-09-13T15:52:57.246098Z","shell.execute_reply":"2023-09-13T15:52:57.256742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')\nprint(df_test.shape)\nprint(df_test['series_id'].unique())\ndf_test.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:52:57.259937Z","iopub.execute_input":"2023-09-13T15:52:57.260906Z","iopub.status.idle":"2023-09-13T15:52:57.286675Z","shell.execute_reply.started":"2023-09-13T15:52:57.260869Z","shell.execute_reply":"2023-09-13T15:52:57.285662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(20, 3))\nsns.lineplot(data=df_test[df_test['series_id']=='038441c925bb'], x=\"step\", y=\"anglez\", linewidth = 0.5)\nplt.show()\n# Since we don't have awake section for this hue is None","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:52:57.292355Z","iopub.execute_input":"2023-09-13T15:52:57.292968Z","iopub.status.idle":"2023-09-13T15:52:57.297398Z","shell.execute_reply.started":"2023-09-13T15:52:57.292926Z","shell.execute_reply":"2023-09-13T15:52:57.296520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for df in (df_train, df_test):\n    df['timestamp'] = pd.to_datetime(df['timestamp'], utc=True)\n    df['hour'] = df['timestamp'].dt.hour\n    \nfeatures = ['hour', 'enmo', 'anglez']","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:52:57.298748Z","iopub.execute_input":"2023-09-13T15:52:57.299451Z","iopub.status.idle":"2023-09-13T15:54:45.570014Z","shell.execute_reply.started":"2023-09-13T15:52:57.299406Z","shell.execute_reply":"2023-09-13T15:54:45.568640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(df_train[features], df_train['awake'], random_state=42, test_size=0.20, shuffle=False)\nX_test = df_test[features]","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:54:45.571470Z","iopub.execute_input":"2023-09-13T15:54:45.571958Z","iopub.status.idle":"2023-09-13T15:54:46.210311Z","shell.execute_reply.started":"2023-09-13T15:54:45.571923Z","shell.execute_reply":"2023-09-13T15:54:46.208855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Max value of anglez feature in train_set:', X_train['anglez'].max())\nprint('Min value of anglez feature in train_set:', X_train['anglez'].min())\n\nprint('Max value of anglez feature in train_set:', X_test['anglez'].max())\nprint('Min value of anglez feature in train_set:', X_test['anglez'].min())","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:54:46.211702Z","iopub.execute_input":"2023-09-13T15:54:46.212095Z","iopub.status.idle":"2023-09-13T15:54:46.217892Z","shell.execute_reply.started":"2023-09-13T15:54:46.212062Z","shell.execute_reply":"2023-09-13T15:54:46.216618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Just dividing anglez column with 90:\nX_train['anglez'] = X_train['anglez']/90\nX_val['anglez'] = X_val['anglez']/90\nX_test['anglez'] = X_test['anglez']/90\n\nprint('Max value of anglez feature in train_set:', X_train['anglez'].max())\nprint('Min value of anglez feature in train_set:', X_train['anglez'].min())\n\nprint('Max value of anglez feature in train_set:', X_test['anglez'].max())\nprint('Min value of anglez feature in train_set:', X_test['anglez'].min())","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:54:46.219509Z","iopub.execute_input":"2023-09-13T15:54:46.219971Z","iopub.status.idle":"2023-09-13T15:54:46.312403Z","shell.execute_reply.started":"2023-09-13T15:54:46.219928Z","shell.execute_reply":"2023-09-13T15:54:46.311147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:54:46.313920Z","iopub.execute_input":"2023-09-13T15:54:46.314261Z","iopub.status.idle":"2023-09-13T15:54:46.318991Z","shell.execute_reply.started":"2023-09-13T15:54:46.314232Z","shell.execute_reply":"2023-09-13T15:54:46.317959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optuna_lgb = False\nn_estimators = 50\nearly_stopping_round = 5\nrandom_state = 42\nparams = {'objective':'binary',\n         'boosting_type':'gbdt',\n         'learning_rate':0.01,\n         'verbosity': 0,\n         'random_state': 42}","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:54:46.320385Z","iopub.execute_input":"2023-09-13T15:54:46.321318Z","iopub.status.idle":"2023-09-13T15:54:46.334593Z","shell.execute_reply.started":"2023-09-13T15:54:46.321284Z","shell.execute_reply":"2023-09-13T15:54:46.333369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = lgb.LGBMClassifier(**params,\nforce_row_wise=True)\n\nmodel.fit(X_train, y_train,\neval_set=[(X_val, y_val)],)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:54:46.336069Z","iopub.execute_input":"2023-09-13T15:54:46.336591Z","iopub.status.idle":"2023-09-13T15:55:31.704506Z","shell.execute_reply.started":"2023-09-13T15:54:46.336528Z","shell.execute_reply":"2023-09-13T15:55:31.703363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:55:31.706350Z","iopub.execute_input":"2023-09-13T15:55:31.708206Z","iopub.status.idle":"2023-09-13T15:55:31.713673Z","shell.execute_reply.started":"2023-09-13T15:55:31.708154Z","shell.execute_reply":"2023-09-13T15:55:31.712314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred = model.predict(X_val)\nprint(classification_report(y_val, y_val_pred))\nprint(y_val_pred)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:55:31.715591Z","iopub.execute_input":"2023-09-13T15:55:31.716791Z","iopub.status.idle":"2023-09-13T15:55:35.896064Z","shell.execute_reply.started":"2023-09-13T15:55:31.716738Z","shell.execute_reply":"2023-09-13T15:55:35.894610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[\"score\"] = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:55:35.897492Z","iopub.execute_input":"2023-09-13T15:55:35.897917Z","iopub.status.idle":"2023-09-13T15:55:35.907340Z","shell.execute_reply.started":"2023-09-13T15:55:35.897885Z","shell.execute_reply":"2023-09-13T15:55:35.905992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[\"not_awake\"] = 1-df_test[\"score\"]\n# smoothing of the predictions\nsmoothing_length = 2*250\ndf_test[\"smooth\"] = df_test[\"not_awake\"].rolling(smoothing_length,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\")\n# re-binarize\ndf_test[\"smooth\"] = df_test[\"smooth\"].round()\n\n# https://stackoverflow.com/questions/73777727/how-to-mark-start-end-of-a-series-of-non-null-and-non-0-values-in-a-column-of-a\ndef get_event(df):\n    lstCV = zip(df.series_id, df.smooth)\n    lstPOI = []\n    for (c, v), g in groupby(lstCV, lambda cv: \n                            (cv[0], cv[1]!=0 and not pd.isnull(cv[1]))):\n        llg = sum(1 for item in g)\n        if v is False: \n            lstPOI.extend([0]*llg)\n        else: \n            lstPOI.extend(['onset']+(llg-2)*[0]+['wakeup'] if llg > 1 else [0])\n    return lstPOI\n\ndf_test[\"event\"] = get_event(df_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:55:35.909367Z","iopub.execute_input":"2023-09-13T15:55:35.909869Z","iopub.status.idle":"2023-09-13T15:55:35.937473Z","shell.execute_reply.started":"2023-09-13T15:55:35.909836Z","shell.execute_reply":"2023-09-13T15:55:35.935515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = df_test.loc[df_test[\"event\"] != 0][[\"series_id\",\"step\",\"event\",\"score\"]].copy().reset_index(drop=True).reset_index(names=\"row_id\")","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:55:35.939505Z","iopub.execute_input":"2023-09-13T15:55:35.939951Z","iopub.status.idle":"2023-09-13T15:55:35.961815Z","shell.execute_reply.started":"2023-09-13T15:55:35.939917Z","shell.execute_reply":"2023-09-13T15:55:35.960608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T15:55:35.963318Z","iopub.execute_input":"2023-09-13T15:55:35.967170Z","iopub.status.idle":"2023-09-13T15:55:35.981941Z","shell.execute_reply.started":"2023-09-13T15:55:35.967112Z","shell.execute_reply":"2023-09-13T15:55:35.979929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I'll try to improve on this in a couple of days, this is just a basic notebook which I wrote just to get started in the competition. Here you can see I divided anglez with 90, will try more experiments like that in future.\nAlright that's the end of the notebook. Thanks for reading it till here and if you've any suggestions or there's any space for improvement please tell me in the comments.\n\nSome useful links which I used while creating this notebook:\n\n1) https://www.kaggle.com/datasets/carlmcbrideellis/zzzs-lightweight-training-dataset-target\n\n\n2) https://www.kaggle.com/code/carlmcbrideellis/zzzs-make-clean-starter-dataset-target\n\n\n3) https://www.kaggle.com/code/prashant111/lightgbm-classifier-in-python\n\n4)https://www.analyticsvidhya.com/blog/2019/01/introduction-time-series-classification/","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"}}