{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":6507308,"sourceType":"datasetVersion","datasetId":3706667}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Detect Sleep States - Machine Learing","metadata":{}},{"cell_type":"code","source":"import sklearn\nimport numpy as np\nimport pandas as pd\nfrom sklearn.ensemble import RandomForestClassifier\nimport gc\nfrom itertools import groupby","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:19:04.741515Z","iopub.execute_input":"2024-04-08T13:19:04.741938Z","iopub.status.idle":"2024-04-08T13:19:04.747851Z","shell.execute_reply.started":"2024-04-08T13:19:04.741911Z","shell.execute_reply":"2024-04-08T13:19:04.746493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read & Process Data","metadata":{}},{"cell_type":"code","source":"train_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\ntrain_events.dropna(inplace=True)\nprint(train_events)\n\n\ntrain = pd.read_parquet(\"/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train_multi.parquet\")\nprint(train.head())","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:19:06.027162Z","iopub.execute_input":"2024-04-08T13:19:06.027592Z","iopub.status.idle":"2024-04-08T13:19:18.208497Z","shell.execute_reply.started":"2024-04-08T13:19:06.027564Z","shell.execute_reply":"2024-04-08T13:19:18.207198Z"},"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']).apply(lambda t: t.tz_localize(None))\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\"]","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:19:18.227083Z","iopub.execute_input":"2024-04-08T13:19:18.227419Z","iopub.status.idle":"2024-04-08T13:19:18.243167Z","shell.execute_reply.started":"2024-04-08T13:19:18.227391Z","shell.execute_reply":"2024-04-08T13:19:18.242380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = make_features(train)\n\n\nX_train = train[features]\ny_train = train[\"awake\"]\n\ntrain.to_parquet('/kaggle/working/train_make_features.parquet')\n\ndel train\ngc.collect()\n\nprint(X_train)\nprint(y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier = RandomForestClassifier(n_estimators=60,\n                                    min_samples_leaf=300,\n                                    n_jobs=-1)\n\nclassifier.fit(X_train, y_train)\n\ndel X_train, y_train\ngc.collect();","metadata":{"execution":{"iopub.status.busy":"2024-04-08T12:40:20.033935Z","iopub.execute_input":"2024-04-08T12:40:20.034437Z","iopub.status.idle":"2024-04-08T13:14:16.847973Z","shell.execute_reply.started":"2024-04-08T12:40:20.034395Z","shell.execute_reply":"2024-04-08T13:14:16.846363Z"},"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\ntest.to_parquet('/kaggle/working/test_make_features.parquet')\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":"2024-04-08T13:14:16.850656Z","iopub.execute_input":"2024-04-08T13:14:16.851080Z","iopub.status.idle":"2024-04-08T13:14:16.977936Z","shell.execute_reply.started":"2024-04-08T13:14:16.851049Z","shell.execute_reply":"2024-04-08T13:14:16.976583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"smoothing_length = 2*230\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":"2024-04-08T13:14:16.979430Z","iopub.execute_input":"2024-04-08T13:14:16.979786Z","iopub.status.idle":"2024-04-08T13:14:17.385899Z","shell.execute_reply.started":"2024-04-08T13:14:16.979758Z","shell.execute_reply":"2024-04-08T13:14:17.384218Z"},"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":"2024-04-08T13:14:17.387122Z","iopub.status.idle":"2024-04-08T13:14:17.387524Z","shell.execute_reply.started":"2024-04-08T13:14:17.387341Z","shell.execute_reply":"2024-04-08T13:14:17.387356Z"},"trusted":true},"execution_count":null,"outputs":[]}]}