{"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 pandas as pd\nimport numpy as np\nfrom itertools import groupby\nimport gc\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T21:07:31.165329Z","iopub.execute_input":"2023-09-26T21:07:31.165835Z","iopub.status.idle":"2023-09-26T21:07:31.625207Z","shell.execute_reply.started":"2023-09-26T21:07:31.165799Z","shell.execute_reply":"2023-09-26T21:07:31.624050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_parquet(\"/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train.parquet\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T22:45:42.743817Z","iopub.execute_input":"2023-09-26T22:45:42.744207Z","iopub.status.idle":"2023-09-26T22:45:54.563091Z","shell.execute_reply.started":"2023-09-26T22:45:42.744179Z","shell.execute_reply":"2023-09-26T22:45:54.562100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_features(df):\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    \n    periods = 20\n    df[\"anglez\"] = abs(df[\"anglez\"])\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_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    return df\n\nfeatures = [\"hour\",\n            \"anglez\",\n            \"anglez_rolling_mean\",\n            \"anglez_rolling_max\",\n            \"anglez_rolling_std\",\n            \"anglez_diff\",\n            \"anglez_diff_rolling_mean\",\n            \"anglez_diff_rolling_max\",\n            \"enmo\",\n            \"enmo_rolling_mean\",\n            \"enmo_rolling_max\",\n            \"enmo_rolling_std\",\n            \"enmo_diff\",\n            \"enmo_diff_rolling_mean\",\n            \"enmo_diff_rolling_max\",\n           ]","metadata":{"execution":{"iopub.status.busy":"2023-09-26T21:07:40.794430Z","iopub.execute_input":"2023-09-26T21:07:40.794885Z","iopub.status.idle":"2023-09-26T21:07:40.815760Z","shell.execute_reply.started":"2023-09-26T21:07:40.794844Z","shell.execute_reply":"2023-09-26T21:07:40.814443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train   = make_features(train)\n\nX_train = train[features]\ny_train = train[\"awake\"]\n\n# save some memory\ndel train\ngc.collect();","metadata":{"execution":{"iopub.status.busy":"2023-09-26T21:07:40.819017Z","iopub.execute_input":"2023-09-26T21:07:40.819360Z","iopub.status.idle":"2023-09-26T21:09:05.904845Z","shell.execute_reply.started":"2023-09-26T21:07:40.819331Z","shell.execute_reply":"2023-09-26T21:09:05.903687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-26T21:09:05.906310Z","iopub.execute_input":"2023-09-26T21:09:05.906735Z","iopub.status.idle":"2023-09-26T21:09:05.915231Z","shell.execute_reply.started":"2023-09-26T21:09:05.906693Z","shell.execute_reply":"2023-09-26T21:09:05.914233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nfrom sklearn.ensemble import RandomForestClassifier\nclassifier = RandomForestClassifier(n_estimators=50,\n                                    min_samples_leaf=300,\n                                    random_state=42,n_jobs=-1)\n\nclassifier.fit(X_train, y_train)\n\n# save some memory\ndel X_train, y_train\ngc.collect();","metadata":{"execution":{"iopub.status.busy":"2023-09-26T21:09:05.916842Z","iopub.execute_input":"2023-09-26T21:09:05.917145Z","iopub.status.idle":"2023-09-26T21:33:15.631906Z","shell.execute_reply.started":"2023-09-26T21:09:05.917118Z","shell.execute_reply":"2023-09-26T21:33:15.630548Z"},"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\")\n\ntest  = make_features(test)\n\nX_test = test[features]\n\ntest[\"not_awake\"] = classifier.predict_proba(X_test)[:,0]\ntest[\"awake\"]     = classifier.predict_proba(X_test)[:,1]","metadata":{"execution":{"iopub.status.busy":"2023-09-26T21:33:15.634087Z","iopub.execute_input":"2023-09-26T21:33:15.634880Z","iopub.status.idle":"2023-09-26T21:33:15.745094Z","shell.execute_reply.started":"2023-09-26T21:33:15.634830Z","shell.execute_reply":"2023-09-26T21:33:15.743602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# smoothing the predictions\nsmoothing_length = 20\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# 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-26T22:39:42.119545Z","iopub.execute_input":"2023-09-26T22:39:42.120041Z","iopub.status.idle":"2023-09-26T22:39:42.137365Z","shell.execute_reply.started":"2023-09-26T22:39:42.120005Z","shell.execute_reply":"2023-09-26T22:39:42.136055Z"},"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-26T23:31:17.934140Z","iopub.execute_input":"2023-09-26T23:31:17.934507Z","iopub.status.idle":"2023-09-26T23:31:17.945871Z","shell.execute_reply.started":"2023-09-26T23:31:17.934479Z","shell.execute_reply":"2023-09-26T23:31:17.944938Z"},"trusted":true},"execution_count":null,"outputs":[]}]}