{"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":"# Simple Random Forest classifier submission\n\n**We shall be using the training data from the dataset \"Zzzs: Lightweight training dataset + target\"**","metadata":{}},{"cell_type":"markdown","source":"# *Import Library*\n\n","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom itertools import groupby\n","metadata":{"execution":{"iopub.status.busy":"2023-09-08T03:58:15.073108Z","iopub.execute_input":"2023-09-08T03:58:15.074444Z","iopub.status.idle":"2023-09-08T03:58:15.496837Z","shell.execute_reply.started":"2023-09-08T03:58:15.074388Z","shell.execute_reply":"2023-09-08T03:58:15.495472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# *Load train and test parquet dataset*\n\n**Convert date column into hour for better result**","metadata":{}},{"cell_type":"code","source":"train = pd.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet\")\ntest  = pd.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet\")\n\n# parse the timestamp and create an \"hour\" feature\ntrain[\"date\"] = pd.to_datetime(train[\"timestamp\"],utc=True)\ntrain[\"hour\"] = train[\"date\"].dt.hour\n\ntest[\"date\"] = pd.to_datetime(test[\"timestamp\"],utc=True)\ntest[\"hour\"] = test[\"date\"].dt.hour\n","metadata":{"execution":{"iopub.status.busy":"2023-09-08T03:58:54.986529Z","iopub.execute_input":"2023-09-08T03:58:54.986924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Seeing some of train data","metadata":{}},{"cell_type":"code","source":"train.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T03:58:04.38321Z","iopub.execute_input":"2023-09-08T03:58:04.384124Z","iopub.status.idle":"2023-09-08T03:58:04.80658Z","shell.execute_reply.started":"2023-09-08T03:58:04.384079Z","shell.execute_reply":"2023-09-08T03:58:04.80507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# *Load RandomforestClassifier*\n\nFit the data into RFC ","metadata":{}},{"cell_type":"code","source":"from 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\"]]\ny_train = train[\"awake\"]\n\nclassifier.fit(X_train, y_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# *Now we will predict some test data to get approx result*","metadata":{}},{"cell_type":"code","source":"X_test = test[[\"anglez\",\"enmo\",\"hour\"]]\ntest[\"awake\"] = classifier.predict(X_test)","metadata":{},"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# 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_count":null,"outputs":[]},{"cell_type":"markdown","source":"# *Submit the dataset of event*","metadata":{}},{"cell_type":"code","source":"sample_submission = test.loc[test[\"event\"] != 0][[\"series_id\",\"step\",\"event\"]].copy().reset_index(drop=True).reset_index(names=\"row_id\")\n# for the moment simply set \"score\" to be 0.7 globally\nsample_submission['score'] = 0.7\nsample_submission.to_csv('submission.csv', index=False)","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"}}