{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":7234006,"sourceType":"datasetVersion","datasetId":4188949}],"dockerImageVersionId":30626,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-19T00:52:31.160559Z","iopub.execute_input":"2023-12-19T00:52:31.161557Z","iopub.status.idle":"2023-12-19T00:52:31.556788Z","shell.execute_reply.started":"2023-12-19T00:52:31.161509Z","shell.execute_reply":"2023-12-19T00:52:31.555691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom itertools import groupby\nimport gc\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-12-19T08:50:01.153989Z","iopub.execute_input":"2023-12-19T08:50:01.154575Z","iopub.status.idle":"2023-12-19T08:50:16.282250Z","shell.execute_reply.started":"2023-12-19T08:50:01.154539Z","shell.execute_reply":"2023-12-19T08:50:16.281065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_parquet(\"/kaggle/input/zzzs-train/Zzzs_train.parquet\")","metadata":{"execution":{"iopub.status.busy":"2023-12-19T08:53:02.383515Z","iopub.execute_input":"2023-12-19T08:53:02.384584Z","iopub.status.idle":"2023-12-19T08:53:12.893506Z","shell.execute_reply.started":"2023-12-19T08:53:02.384533Z","shell.execute_reply":"2023-12-19T08:53:12.892404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T01:09:17.989319Z","iopub.execute_input":"2023-12-19T01:09:17.990131Z","iopub.status.idle":"2023-12-19T01:09:18.006957Z","shell.execute_reply.started":"2023-12-19T01:09:17.990088Z","shell.execute_reply":"2023-12-19T01:09:18.005836Z"},"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\",\n           ]","metadata":{"execution":{"iopub.status.busy":"2023-12-19T08:54:12.873772Z","iopub.execute_input":"2023-12-19T08:54:12.874241Z","iopub.status.idle":"2023-12-19T08:54:13.104628Z","shell.execute_reply.started":"2023-12-19T08:54:12.874206Z","shell.execute_reply":"2023-12-19T08:54:13.103488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train   = make_features(train)\n\nX = train[features]\ny = train[\"awake\"]\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=None)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T08:54:17.524396Z","iopub.execute_input":"2023-12-19T08:54:17.524825Z","iopub.status.idle":"2023-12-19T08:59:49.166703Z","shell.execute_reply.started":"2023-12-19T08:54:17.524793Z","shell.execute_reply":"2023-12-19T08:59:49.163607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom sklearn.metrics import precision_score, recall_score\n\n\n\n    # Create the model\nmodel = Sequential([\n    Dense(128, activation='relu', input_shape=(X_train.shape[1],)),\n    Dropout(0.2),\n    Dense(64, activation='relu'),\n    Dropout(0.3),\n    Dense(32, activation='relu'),\n  # Added dropout layer\n    Dense(1, activation='sigmoid')\n])\n\n    # Compile the model\nmodel.compile(optimizer='adam',\n                  loss='binary_crossentropy',\n                  metrics=['accuracy'])\n\nes =  tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min',patience=10, verbose=1)\nmc = tf.keras.callbacks.ModelCheckpoint(\"best_model.h5\", monitor= 'val_loss', mode='min', save_best_only=True, verbose=1)\n    # Train the model\nmodel.fit(\n    X_train, y_train,\n    validation_split = 0.3, # -> มันไม่ได้ shuffle ให้เรา\n    batch_size = 128,\n    callbacks =[es,mc],\n    epochs = 8\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T01:16:43.023376Z","iopub.execute_input":"2023-12-19T01:16:43.023852Z","iopub.status.idle":"2023-12-19T01:39:54.415638Z","shell.execute_reply.started":"2023-12-19T01:16:43.023812Z","shell.execute_reply":"2023-12-19T01:39:54.414347Z"},"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[\"awake\"] = model.predict(X_test)[:,0]\ntest[\"not_awake\"] = 1 - model.predict(X_test)[:,0]","metadata":{"execution":{"iopub.status.busy":"2023-12-19T01:40:17.007742Z","iopub.execute_input":"2023-12-19T01:40:17.008285Z","iopub.status.idle":"2023-12-19T01:40:17.410566Z","shell.execute_reply.started":"2023-12-19T01:40:17.008238Z","shell.execute_reply":"2023-12-19T01:40:17.409565Z"},"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":"2023-12-19T01:40:30.574120Z","iopub.execute_input":"2023-12-19T01:40:30.574564Z","iopub.status.idle":"2023-12-19T01:40:30.589635Z","shell.execute_reply.started":"2023-12-19T01:40:30.574525Z","shell.execute_reply":"2023-12-19T01:40:30.588501Z"},"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-12-19T01:40:45.391305Z","iopub.execute_input":"2023-12-19T01:40:45.391774Z","iopub.status.idle":"2023-12-19T01:40:45.409331Z","shell.execute_reply.started":"2023-12-19T01:40:45.391736Z","shell.execute_reply":"2023-12-19T01:40:45.408130Z"},"trusted":true},"execution_count":null,"outputs":[]}]}