{"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 numpy as np\nimport pandas as pd\nfrom itertools import groupby","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-08T09:58:41.908269Z","iopub.execute_input":"2023-09-08T09:58:41.908677Z","iopub.status.idle":"2023-09-08T09:58:42.370314Z","shell.execute_reply.started":"2023-09-08T09:58:41.908624Z","shell.execute_reply":"2023-09-08T09:58:42.368964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_parquet(\"/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train.parquet\")\ntest  = pd.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:05:15.708018Z","iopub.execute_input":"2023-09-08T10:05:15.708450Z","iopub.status.idle":"2023-09-08T10:05:23.090969Z","shell.execute_reply.started":"2023-09-08T10:05:15.708415Z","shell.execute_reply":"2023-09-08T10:05:23.089738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# parse the timestamp and create an \"hour\" feature\ntrain[\"timestamp\"] = pd.to_datetime(train[\"timestamp\"],utc=True)\ntrain[\"hour\"] = train[\"timestamp\"].dt.hour\n\ntest[\"timestamp\"] = pd.to_datetime(test[\"timestamp\"],utc=True)\ntest[\"hour\"] = test[\"timestamp\"].dt.hour","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:05:29.729499Z","iopub.execute_input":"2023-09-08T10:05:29.729882Z","iopub.status.idle":"2023-09-08T10:07:16.825502Z","shell.execute_reply.started":"2023-09-08T10:05:29.729850Z","shell.execute_reply":"2023-09-08T10:07:16.824312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:07:16.827119Z","iopub.execute_input":"2023-09-08T10:07:16.827461Z","iopub.status.idle":"2023-09-08T10:07:16.843100Z","shell.execute_reply.started":"2023-09-08T10:07:16.827431Z","shell.execute_reply":"2023-09-08T10:07:16.841990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:10:59.903323Z","iopub.execute_input":"2023-09-08T10:10:59.903810Z","iopub.status.idle":"2023-09-08T10:11:00.068477Z","shell.execute_reply.started":"2023-09-08T10:10:59.903774Z","shell.execute_reply":"2023-09-08T10:11:00.067313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X =train[[\"anglez\",\"enmo\",\"hour\"]]\ny = train['awake']","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:10:14.481307Z","iopub.execute_input":"2023-09-08T10:10:14.481715Z","iopub.status.idle":"2023-09-08T10:10:14.599268Z","shell.execute_reply.started":"2023-09-08T10:10:14.481683Z","shell.execute_reply":"2023-09-08T10:10:14.598009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:11:02.511568Z","iopub.execute_input":"2023-09-08T10:11:02.511956Z","iopub.status.idle":"2023-09-08T10:11:05.175753Z","shell.execute_reply.started":"2023-09-08T10:11:02.511927Z","shell.execute_reply":"2023-09-08T10:11:05.174735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nfrom sklearn.ensemble import RandomForestClassifier\nclassifier = RandomForestClassifier(n_estimators=10,\n                                    max_depth=3,\n                                    min_samples_leaf=100)\n\n\nclassifier.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:11:09.195038Z","iopub.execute_input":"2023-09-08T10:11:09.196247Z","iopub.status.idle":"2023-09-08T10:12:37.455637Z","shell.execute_reply.started":"2023-09-08T10:11:09.196196Z","shell.execute_reply":"2023-09-08T10:12:37.452978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier.score(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:12:37.552174Z","iopub.execute_input":"2023-09-08T10:12:37.552626Z","iopub.status.idle":"2023-09-08T10:12:39.751797Z","shell.execute_reply.started":"2023-09-08T10:12:37.552592Z","shell.execute_reply":"2023-09-08T10:12:39.750622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost as xg\nxgm = xg.XGBClassifier()\nxgm.fit(X_train,y_train)\nxgm.score(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:12:40.238620Z","iopub.execute_input":"2023-09-08T10:12:40.239021Z","iopub.status.idle":"2023-09-08T10:17:33.061084Z","shell.execute_reply.started":"2023-09-08T10:12:40.238990Z","shell.execute_reply":"2023-09-08T10:17:33.059864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test[[\"anglez\",\"enmo\",\"hour\"]]\ntest[\"score\"] = xgm.predict_proba(X_test)[:,1]","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:22:17.535111Z","iopub.execute_input":"2023-09-08T10:22:17.535533Z","iopub.status.idle":"2023-09-08T10:22:17.547526Z","shell.execute_reply.started":"2023-09-08T10:22:17.535499Z","shell.execute_reply":"2023-09-08T10:22:17.546572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"not_awake\"] = 1-test[\"score\"]\n# exponential smoothing of the predictions\ntest[\"smooth\"] = test[\"not_awake\"].ewm(span = 100).mean()\n# re-binarize\ntest[\"smooth\"] = 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\ntest[\"event\"] = get_event(test)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:22:17.731971Z","iopub.execute_input":"2023-09-08T10:22:17.732590Z","iopub.status.idle":"2023-09-08T10:22:17.744773Z","shell.execute_reply.started":"2023-09-08T10:22:17.732558Z","shell.execute_reply":"2023-09-08T10:22:17.743527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = test.loc[test[\"event\"] != 0][[\"series_id\",\"step\",\"event\",\"score\"]].copy().reset_index(drop=True).reset_index(names=\"row_id\")\nsample_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:22:17.951314Z","iopub.execute_input":"2023-09-08T10:22:17.951742Z","iopub.status.idle":"2023-09-08T10:22:17.984244Z","shell.execute_reply.started":"2023-09-08T10:22:17.951696Z","shell.execute_reply":"2023-09-08T10:22:17.983296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}