{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\npd.set_option('display.max_columns', None)\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.ensemble import GradientBoostingClassifier\nfrom itertools import groupby","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-16T09:54:48.403993Z","iopub.execute_input":"2023-09-16T09:54:48.404485Z","iopub.status.idle":"2023-09-16T09:54:49.351012Z","shell.execute_reply.started":"2023-09-16T09:54:48.404415Z","shell.execute_reply":"2023-09-16T09:54:49.349922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ts = pd.read_parquet('/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train_multi.parquet')\nts.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-16T09:54:49.352846Z","iopub.execute_input":"2023-09-16T09:54:49.353250Z","iopub.status.idle":"2023-09-16T09:54:55.810005Z","shell.execute_reply.started":"2023-09-16T09:54:49.353226Z","shell.execute_reply":"2023-09-16T09:54:55.808777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def f_engineering(df):\n    \n    df['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.tz_localize(None))\n    df[\"hour\"] = df[\"timestamp\"].dt.hour\n    \n    df['hour_sin'] = np.sin(2 * np.pi * df['hour']/24.0)\n    df['hour_cos'] = np.cos(2 * np.pi * df['hour']/24.0)\n    \n    df['weekday'] = df['timestamp'].dt.weekday\n\n    periods = 20\n    \n    df[\"anglez_diff\"] = df.groupby('series_id')['anglez'].diff(periods=periods).fillna(method=\"bfill\").astype('float16')\n    df[\"enmo_diff\"] = df.groupby('series_id')['enmo'].diff(periods=periods).fillna(method=\"bfill\").astype('float16')\n    df[\"anglez_rolling_mean\"] = df[\"anglez\"].rolling(periods, center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_rolling_mean\"] = df[\"enmo\"].rolling(periods, center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"anglez_rolling_max\"] = df[\"anglez\"].rolling(periods, center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_rolling_max\"] = df[\"enmo\"].rolling(periods, center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"anglez_rolling_min\"] = df[\"anglez\"].rolling(periods, center=True).min().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_rolling_min\"] = df[\"enmo\"].rolling(periods, center=True).min().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"anglez_rolling_std\"] = df[\"anglez\"].rolling(periods, center=True).std().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_rolling_std\"] = df[\"enmo\"].rolling(periods, center=True).std().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"anglez_diff_rolling_mean\"] = df[\"anglez_diff\"].rolling(periods, center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_diff_rolling_mean\"] = df[\"enmo_diff\"].rolling(periods, center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"anglez_diff_rolling_max\"] = df[\"anglez_diff\"].rolling(periods, center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_diff_rolling_max\"] = df[\"enmo_diff\"].rolling(periods, center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    \n    df['enmo1p'] = df['enmo'] + 1\n    return df\n\n\nts = f_engineering(ts)","metadata":{"execution":{"iopub.status.busy":"2023-09-16T09:54:55.811866Z","iopub.execute_input":"2023-09-16T09:54:55.812494Z","iopub.status.idle":"2023-09-16T09:57:54.159612Z","shell.execute_reply.started":"2023-09-16T09:54:55.812462Z","shell.execute_reply":"2023-09-16T09:57:54.158301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = [\n    \"hour\",\n    \"hour_sin\",\n    \"hour_cos\",\n    \"weekday\",\n    \"anglez\",\n    \"anglez_diff\",\n    \"anglez_rolling_mean\",\n    \"anglez_rolling_max\",\n    \"anglez_rolling_std\",\n    \"anglez_diff_rolling_mean\",\n    \"anglez_diff_rolling_max\",\n    \"enmo\",\n    \"enmo1p\",\n    \"enmo_diff\",\n    \"enmo_rolling_mean\",\n    \"enmo_rolling_max\",\n    \"enmo_rolling_std\",\n    \"enmo_diff_rolling_mean\",\n    \"enmo_diff_rolling_max\",\n]\n\nxtr, ytr = ts[features], ts.awake","metadata":{"execution":{"iopub.status.busy":"2023-09-16T09:57:54.160803Z","iopub.execute_input":"2023-09-16T09:57:54.161149Z","iopub.status.idle":"2023-09-16T09:57:55.726666Z","shell.execute_reply.started":"2023-09-16T09:57:54.161119Z","shell.execute_reply":"2023-09-16T09:57:55.725606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del ts","metadata":{"execution":{"iopub.status.busy":"2023-09-16T09:57:55.728835Z","iopub.execute_input":"2023-09-16T09:57:55.729091Z","iopub.status.idle":"2023-09-16T09:57:55.739688Z","shell.execute_reply.started":"2023-09-16T09:57:55.729070Z","shell.execute_reply":"2023-09-16T09:57:55.738474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nclf = GradientBoostingClassifier(\n    random_state=0,\n    verbose = 1\n)\n\nclf.fit(xtr, ytr)","metadata":{"execution":{"iopub.status.busy":"2023-09-16T09:59:49.159949Z","iopub.execute_input":"2023-09-16T09:59:49.160239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.score(xtr, ytr)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del xtr, ytr","metadata":{"execution":{"iopub.status.busy":"2023-09-16T09:57:56.120095Z","iopub.status.idle":"2023-09-16T09:57:56.120547Z","shell.execute_reply.started":"2023-09-16T09:57:56.120333Z","shell.execute_reply":"2023-09-16T09:57:56.120349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')\ntest = f_engineering(test)","metadata":{"execution":{"iopub.status.busy":"2023-09-16T09:57:56.121722Z","iopub.status.idle":"2023-09-16T09:57:56.122034Z","shell.execute_reply.started":"2023-09-16T09:57:56.121886Z","shell.execute_reply":"2023-09-16T09:57:56.121900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xts = test[features]\n\npredicted_proba = clf.predict_proba(xts)\ntest[\"awake\"] = predicted_proba[:, 1] \ntest[\"not_awake\"] = predicted_proba[:, 0]","metadata":{"execution":{"iopub.status.busy":"2023-09-16T09:57:56.123127Z","iopub.status.idle":"2023-09-16T09:57:56.123409Z","shell.execute_reply.started":"2023-09-16T09:57:56.123273Z","shell.execute_reply":"2023-09-16T09:57:56.123286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsmoothing_length = 2*250\ntest[\"score\"] = test[\"awake\"].rolling(smoothing_length, center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\")\ntest[\"smooth\"] = test[\"not_awake\"].rolling(smoothing_length, center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\")\n\ntest[\"smooth\"] = test[\"smooth\"].round()\n\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-16T09:57:56.125064Z","iopub.status.idle":"2023-09-16T09:57:56.125594Z","shell.execute_reply.started":"2023-09-16T09:57:56.125412Z","shell.execute_reply":"2023-09-16T09:57:56.125427Z"},"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-16T09:57:56.126614Z","iopub.status.idle":"2023-09-16T09:57:56.127302Z","shell.execute_reply.started":"2023-09-16T09:57:56.127142Z","shell.execute_reply":"2023-09-16T09:57:56.127158Z"},"trusted":true},"execution_count":null,"outputs":[]}]}