{"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":"# standard\nimport numpy as np\nimport pandas as pd\nimport time\n# plots\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')\nfrom itertools import groupby\n# model\nfrom sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:51:01.550058Z","iopub.execute_input":"2023-09-11T06:51:01.551283Z","iopub.status.idle":"2023-09-11T06:51:01.558213Z","shell.execute_reply.started":"2023-09-11T06:51:01.551240Z","shell.execute_reply":"2023-09-11T06:51:01.556834Z"},"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_test  = pd.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet\")","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:45:23.378894Z","iopub.execute_input":"2023-09-11T06:45:23.379970Z","iopub.status.idle":"2023-09-11T06:45:33.813124Z","shell.execute_reply.started":"2023-09-11T06:45:23.379921Z","shell.execute_reply":"2023-09-11T06:45:33.811915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for df in (df_train, df_test):\n    # parse the timestamp and create an \"hour\" feature\n    df[\"timestamp\"] = pd.to_datetime(df[\"timestamp\"],utc=True)\n    df[\"hour\"] = df[\"timestamp\"].dt.hour\n    df[\"minute\"] = df[\"timestamp\"].dt.minute\n    # feature cross\n    df[\"anglez_times_enmo\"] = abs(df[\"anglez\"]) * df[\"enmo\"]\n    # \"rolling\" features\n    periods = 50\n    df[\"anglez_diff\"] = df.groupby('series_id')['anglez'].diff(periods=periods).fillna(method=\"bfill\")\n    df[\"enmo_diff\"]   = df.groupby('series_id')['enmo'].diff(periods=periods).fillna(method=\"bfill\")\n    df[\"anglez_rolling\"] = df[\"anglez\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\")\n    df[\"enmo_rolling\"]   = df[\"enmo\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\")\n    df[\"anglez_diff_rolling\"] = df[\"anglez_diff\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\")\n    df[\"enmo_diff_rolling\"]   = df[\"enmo_diff\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\")\n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:46:11.841639Z","iopub.execute_input":"2023-09-11T06:46:11.842114Z","iopub.status.idle":"2023-09-11T06:48:10.215439Z","shell.execute_reply.started":"2023-09-11T06:46:11.842080Z","shell.execute_reply":"2023-09-11T06:48:10.214215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = [\"hour\",\"anglez_times_enmo\",\"minute\",\n           \"anglez\",\"anglez_diff\",\"anglez_rolling\",\"anglez_diff_rolling\",\n           \"enmo\",\"enmo_diff\",\"enmo_rolling\",\"enmo_diff_rolling\"]","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:48:10.217662Z","iopub.execute_input":"2023-09-11T06:48:10.218097Z","iopub.status.idle":"2023-09-11T06:48:10.225665Z","shell.execute_reply.started":"2023-09-11T06:48:10.218056Z","shell.execute_reply":"2023-09-11T06:48:10.224193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = df_train[features]\ny_train = df_train[\"awake\"]","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:48:53.381548Z","iopub.execute_input":"2023-09-11T06:48:53.381989Z","iopub.status.idle":"2023-09-11T06:48:53.982851Z","shell.execute_reply.started":"2023-09-11T06:48:53.381958Z","shell.execute_reply":"2023-09-11T06:48:53.981635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier(metric='minkowski',\n                     metric_params=None, n_jobs=-1, n_neighbors=5, p=2,\n                     weights='uniform')","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:48:55.811482Z","iopub.execute_input":"2023-09-11T06:48:55.811876Z","iopub.status.idle":"2023-09-11T06:48:55.818259Z","shell.execute_reply.started":"2023-09-11T06:48:55.811846Z","shell.execute_reply":"2023-09-11T06:48:55.817125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:48:59.054136Z","iopub.execute_input":"2023-09-11T06:48:59.055100Z","iopub.status.idle":"2023-09-11T06:50:10.495387Z","shell.execute_reply.started":"2023-09-11T06:48:59.055047Z","shell.execute_reply":"2023-09-11T06:50:10.493964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = df_test[features]\ndf_test[\"score\"] = knn.predict_proba(X_test)[:,1]","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:50:10.497246Z","iopub.execute_input":"2023-09-11T06:50:10.497637Z","iopub.status.idle":"2023-09-11T06:50:10.628462Z","shell.execute_reply.started":"2023-09-11T06:50:10.497605Z","shell.execute_reply":"2023-09-11T06:50:10.627443Z"},"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-11T06:51:09.308600Z","iopub.execute_input":"2023-09-11T06:51:09.309031Z","iopub.status.idle":"2023-09-11T06:51:09.324457Z","shell.execute_reply.started":"2023-09-11T06:51:09.308999Z","shell.execute_reply":"2023-09-11T06:51:09.322917Z"},"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\")\nsample_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:51:13.555745Z","iopub.execute_input":"2023-09-11T06:51:13.556190Z","iopub.status.idle":"2023-09-11T06:51:13.574214Z","shell.execute_reply.started":"2023-09-11T06:51:13.556157Z","shell.execute_reply":"2023-09-11T06:51:13.572854Z"},"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"}}