{"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":"markdown","source":"### I have updated [Carl McBride Ellis's](https://www.kaggle.com/carlmcbrideellis) notebook by adding features based on day & month. Also I have included score based on predict_proba method from Random Forest Classifier\n\n\nWe shall be using the training data from the dataset [\"Zzzs: Lightweight training dataset + target\"](https://www.kaggle.com/datasets/carlmcbrideellis/zzzs-lightweight-training-dataset-target)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"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-08T11:01:26.400781Z","iopub.execute_input":"2023-09-08T11:01:26.401185Z","iopub.status.idle":"2023-09-08T11:01:26.812541Z","shell.execute_reply.started":"2023-09-08T11:01:26.401145Z","shell.execute_reply":"2023-09-08T11:01:26.811309Z"},"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\")","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:01:26.814904Z","iopub.execute_input":"2023-09-08T11:01:26.815535Z","iopub.status.idle":"2023-09-08T11:01:35.911435Z","shell.execute_reply.started":"2023-09-08T11:01:26.815474Z","shell.execute_reply":"2023-09-08T11:01:35.910599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# parse the timestamp and create \"hour\", \"day\", \"month\" feature\ntrain[\"date\"] = pd.to_datetime(train[\"timestamp\"],utc=True)\ntrain[\"hour\"] = train[\"date\"].dt.hour\ntrain[\"day\"] = train[\"date\"].dt.day\ntrain[\"month\"] = train[\"date\"].dt.month\n\ntest[\"date\"] = pd.to_datetime(test[\"timestamp\"],utc=True)\ntest[\"hour\"] = test[\"date\"].dt.hour\ntest[\"day\"] = test[\"date\"].dt.day\ntest[\"month\"] = test[\"date\"].dt.month","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:01:35.914018Z","iopub.execute_input":"2023-09-08T11:01:35.914457Z","iopub.status.idle":"2023-09-08T11:02:49.429733Z","shell.execute_reply.started":"2023-09-08T11:01:35.914416Z","shell.execute_reply":"2023-09-08T11:02:49.428618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nfrom sklearn.ensemble import RandomForestClassifier\nclassifier = RandomForestClassifier(n_estimators=10,\n                                    max_depth=3,\n                                    min_samples_leaf=100)\n\nX_train = train[[\"anglez\",\"enmo\",\"hour\", \"day\", \"month\"]]\ny_train = train[\"awake\"]\n\nclassifier.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:02:49.432272Z","iopub.execute_input":"2023-09-08T11:02:49.433135Z","iopub.status.idle":"2023-09-08T11:04:25.857036Z","shell.execute_reply.started":"2023-09-08T11:02:49.433093Z","shell.execute_reply":"2023-09-08T11:04:25.855848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test[[\"anglez\",\"enmo\",\"hour\", \"day\", \"month\"]]\ntest[\"awake\"] = classifier.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:04:25.858090Z","iopub.execute_input":"2023-09-08T11:04:25.858413Z","iopub.status.idle":"2023-09-08T11:04:25.868287Z","shell.execute_reply.started":"2023-09-08T11:04:25.858383Z","shell.execute_reply":"2023-09-08T11:04:25.867483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"not_awake\"] = test[\"awake\"].replace({0:1, 1:0})\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# calculate score\ntest['score1'] = classifier.predict_proba(X_test)[:,0]\ntest['score2'] = classifier.predict_proba(X_test)[:,1]\ntest['score'] = test[['score1', 'score2']].max(axis=1)\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    print()\n    lstCV = zip(df.series_id, df.smooth)\n    \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-08T11:04:25.869456Z","iopub.execute_input":"2023-09-08T11:04:25.870279Z","iopub.status.idle":"2023-09-08T11:04:25.897797Z","shell.execute_reply.started":"2023-09-08T11:04:25.870245Z","shell.execute_reply":"2023-09-08T11:04:25.896818Z"},"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-08T11:04:25.899067Z","iopub.execute_input":"2023-09-08T11:04:25.900219Z","iopub.status.idle":"2023-09-08T11:04:25.924772Z","shell.execute_reply.started":"2023-09-08T11:04:25.900186Z","shell.execute_reply":"2023-09-08T11:04:25.923507Z"},"trusted":true},"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"}}