{"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":8074671,"sourceType":"datasetVersion","datasetId":4765062}],"dockerImageVersionId":30684,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# DetectSleepStates_ML2","metadata":{}},{"cell_type":"code","source":"import sklearn\nimport numpy as np\nimport pandas as pd\nfrom sklearn.ensemble import RandomForestClassifier\nfrom xgboost.sklearn import XGBClassifier\nfrom sklearn.model_selection import train_test_split\nimport gc\nfrom itertools import groupby\nfrom sklearn.metrics import accuracy_score\nimport matplotlib.pyplot as plt\nimport gc\nfrom memory_profiler import profile","metadata":{"execution":{"iopub.status.busy":"2024-04-12T09:47:02.920735Z","iopub.execute_input":"2024-04-12T09:47:02.921424Z","iopub.status.idle":"2024-04-12T09:47:06.098893Z","shell.execute_reply.started":"2024-04-12T09:47:02.921376Z","shell.execute_reply":"2024-04-12T09:47:06.097645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@profile\ndef 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","metadata":{"execution":{"iopub.status.busy":"2024-04-12T09:47:06.101146Z","iopub.execute_input":"2024-04-12T09:47:06.101656Z","iopub.status.idle":"2024-04-12T09:47:06.109092Z","shell.execute_reply.started":"2024-04-12T09:47:06.101623Z","shell.execute_reply":"2024-04-12T09:47:06.107876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_parquet(\"/kaggle/input/detectsleepstates-dataprocess/train_make_features.parquet\")\n\nprint(train.shape)\nprint(train.head())\ntrain = train.drop(train[train[\"awake\"]== 2].index, axis= 0)\nprint(train.head())","metadata":{"execution":{"iopub.status.busy":"2024-04-12T09:47:06.110189Z","iopub.execute_input":"2024-04-12T09:47:06.110903Z","iopub.status.idle":"2024-04-12T09:47:17.273245Z","shell.execute_reply.started":"2024-04-12T09:47:06.110872Z","shell.execute_reply":"2024-04-12T09:47:17.271795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# features = [\"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\"]\nfeatures = [\"hour\",\n            \"anglez\",\n            \"enmo\"]\n\nprint(train.shape)\n# train[train[\"awake\"]==2][\"awake\"]=0\n\nX = train[features]\ny = train[\"awake\"]\nprint((train[\"awake\"]==0).sum())\nprint((train[\"awake\"]==1).sum())\nscale_pos_weight = (train[\"awake\"]==0).sum() / (train[\"awake\"]==1).sum()\nprint(scale_pos_weight)\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, shuffle=False)\n\nxgb_classifier = XGBClassifier(n_estimators=5,\n                               max_depth=5,\n                               reg_alpha=1,\n                               learning_rate=0.1,\n                               scale_pos_weight=scale_pos_weight)\nxgb_classifier.fit(X_train, y_train)\ntrain_predict = xgb_classifier.predict(X_train)\nval_predict = xgb_classifier.predict(X_val)\ntrain_acc = accuracy_score(y_train, train_predict)\nval_acc = accuracy_score(y_val, val_predict)\nprint(f\"Train Acc | {train_acc}\")\nprint(f\"Val Acc | {val_acc}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-12T09:47:17.274884Z","iopub.execute_input":"2024-04-12T09:47:17.275219Z","iopub.status.idle":"2024-04-12T09:47:26.131994Z","shell.execute_reply.started":"2024-04-12T09:47:17.275191Z","shell.execute_reply":"2024-04-12T09:47:26.130787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train[\"awake\"].unique())\n\nprint('*' * 50)\nprint(y_val.value_counts())\nprint('*' * 50)\nprint(np.bincount(val_predict))\nprint('*' * 50)","metadata":{"execution":{"iopub.status.busy":"2024-04-12T09:47:26.135415Z","iopub.execute_input":"2024-04-12T09:47:26.135729Z","iopub.status.idle":"2024-04-12T09:47:26.256715Z","shell.execute_reply.started":"2024-04-12T09:47:26.135703Z","shell.execute_reply":"2024-04-12T09:47:26.255335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train, X_train, X_val, y_train, y_val, X, y, train_predict, val_predict\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-04-12T09:47:26.257959Z","iopub.execute_input":"2024-04-12T09:47:26.258301Z","iopub.status.idle":"2024-04-12T09:47:26.442625Z","shell.execute_reply.started":"2024-04-12T09:47:26.258272Z","shell.execute_reply":"2024-04-12T09:47:26.441293Z"},"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\")\nprint(test.shape)\ntest = make_features(test)","metadata":{"execution":{"iopub.status.busy":"2024-04-12T09:47:26.444261Z","iopub.execute_input":"2024-04-12T09:47:26.444701Z","iopub.status.idle":"2024-04-12T09:47:26.653965Z","shell.execute_reply.started":"2024-04-12T09:47:26.444670Z","shell.execute_reply":"2024-04-12T09:47:26.652658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def plot_hist(data):\n#     plt.figure()\n#     plt.hist(data)\n#     plt.show()\n\nX_test = test[features]\ntest = test.drop(features, axis=1)\ngc.collect()\npredict = xgb_classifier.predict_proba(X_test)\nprint(predict.shape)\ntest[\"not_awake\"] = predict[:,0]\ntest[\"awake\"] = predict[:,1]\nprint(test.head())\n# print(test[\"awake\"].mean())\n\n\nsmoothing_length = 10\ntest[\"smooth\"] = (test['not_awake'] - test['not_awake'].min()) / (test['not_awake'].max() - test['not_awake'].min())\ntest[\"smooth\"] = test[\"smooth\"].rolling(smoothing_length,center=True).mean().bfill().ffill()\n# plot_hist(test[\"smooth\"])\n# re-binarize\n# print(test[\"smooth\"].mean())\n# print(test[\"smooth\"])\n# test[\"score\"]  = 1-test[\"smooth\"]\ntest[\"score\"] = test[\"awake\"].rolling(smoothing_length,center=True).mean().bfill().ffill()\n\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)\nprint(test[\"event\"])\n\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-04-12T09:47:26.655256Z","iopub.execute_input":"2024-04-12T09:47:26.655633Z","iopub.status.idle":"2024-04-12T09:47:26.865398Z","shell.execute_reply.started":"2024-04-12T09:47:26.655604Z","shell.execute_reply":"2024-04-12T09:47:26.864220Z"},"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)\n\nprint(sample_submission)","metadata":{"execution":{"iopub.status.busy":"2024-04-12T09:47:26.867034Z","iopub.execute_input":"2024-04-12T09:47:26.867375Z","iopub.status.idle":"2024-04-12T09:47:26.885827Z","shell.execute_reply.started":"2024-04-12T09:47:26.867327Z","shell.execute_reply":"2024-04-12T09:47:26.884653Z"},"trusted":true},"execution_count":null,"outputs":[]}]}