{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":152619799,"sourceType":"kernelVersion"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### editing in progress","metadata":{}},{"cell_type":"markdown","source":"# 対象ID","metadata":{}},{"cell_type":"code","source":"# 学習用\n# series_id: c7b2155a4a47\nTRAIN_ID = 206\n\n# テスト用\n# series_id: b750c8c1556c\nTEST_ID = 185","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 前処理","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport pandas.tseries.offsets as offsets\nimport numpy as np\nimport pyarrow as pa\nimport pyarrow.parquet as pq\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport gc\nimport time\nimport IPython.display\nimport collections\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import LSTM, Dense, Conv1D, Conv2D, Conv3D, Dropout\nfrom tensorflow.keras.utils import to_categorical, timeseries_dataset_from_array\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import BinaryCrossentropy\n\nfrom sklearn.preprocessing import PolynomialFeatures\nfrom sklearn.linear_model import LinearRegression\n\nfrom scipy.stats import norm, ttest_ind\n\nfrom datetime import timedelta\nfrom scipy.stats import norm\nfrom pathlib import Path\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-06T01:16:12.022272Z","iopub.execute_input":"2023-12-06T01:16:12.022674Z","iopub.status.idle":"2023-12-06T01:16:12.302526Z","shell.execute_reply.started":"2023-12-06T01:16:12.022644Z","shell.execute_reply":"2023-12-06T01:16:12.301393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def graphs(df, id_map, start=0, end=0):\n    if not end:\n        df = df[df['id_map']==id_map].copy().reset_index(drop=True)\n    else:\n        df = df[df['id_map']==id_map].copy().reset_index(drop=True).iloc[start:end]\n    steps = df[df['time'].apply(lambda x: str(x)[-8:]=='00:00:00')]['step']\n    flag = len(df) != df['step'].iat[-1]\n\n    _, axes = plt.subplots(2, 1, figsize=(20,6))\n    axes[0].set_title(id_map)\n    a = 'anglez' if 'anglez' in df.columns else 'a_b_var'\n    e = 'enmo' if 'enmo' in df.columns else 'e_b_mean'        \n    for ax, col in zip(axes.ravel(), [a, e]):\n        if 'state' in df.columns:\n            if col == 'anglez':\n                df_state = df['state'].apply(lambda x: 50 if x==1 else -50 if x==0 else 0)\n            else:\n                df_state = df['state'].apply(lambda x: 2 if x==1 else 0 if x==0 else 1)\n        X = df.index if flag else df['step']\n        ax.plot(X, df[col], linewidth=0.5, marker='', color='0.5', label=col)\n        if 'state' in df.columns:\n            ax.plot(X, df_state, linewidth=1, marker='', color='b', label='state')\n        for step in steps:\n            X = df[df['step']==step].index[0] if flag else step\n            ax.axvline(x=X, linewidth=1, color='r', alpha=0.5)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:06:36.531113Z","iopub.execute_input":"2023-12-06T00:06:36.531633Z","iopub.status.idle":"2023-12-06T00:06:36.543421Z","shell.execute_reply.started":"2023-12-06T00:06:36.531607Z","shell.execute_reply":"2023-12-06T00:06:36.542377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def time_to_sec(x):\n    return x.timestamp() % SEC_OF_DAY\n\ndef time_to_step(x):\n    return (x.timestamp() % SEC_OF_DAY) / 5","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:06:36.544775Z","iopub.execute_input":"2023-12-06T00:06:36.545130Z","iopub.status.idle":"2023-12-06T00:06:36.578562Z","shell.execute_reply.started":"2023-12-06T00:06:36.545096Z","shell.execute_reply":"2023-12-06T00:06:36.577725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HOME = Path('/kaggle')\nORIGIN = HOME / 'input/child-mind-institute-detect-sleep-states'\nCUSTOM = HOME / 'input/zzz-eda-dataset'\nWORKING = HOME / 'working'\n\nEVENT_MAP = {'onset':0, 'wakeup':1}\n\nEXP = ['anglez', 'enmo', 'a_b_var', 'a_a_var', 'e_b_mean', 'e_a_mean', 'pd']\nOBJ = ['state']\n\nSTEP_OF_WARMUP = 12 * 60 * 6\nSTEP_OF_RATE = 2\nSTEP_OF_STRIDE = 6\nSTEP_OF_MOVING = 12 * 60 * 6\n\nSEC_OF_DAY  = 60 * 60 * 24\nSEC_OF_HOUR = 60 * 60\n\nSTEP_OF_DAY   = 12 * 60 * 24\nSTEP_OF_HOUR  = 12 * 60\nSTEP_OF_30MIN = 12 * 30","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:06:36.580653Z","iopub.execute_input":"2023-12-06T00:06:36.580911Z","iopub.status.idle":"2023-12-06T00:06:36.593614Z","shell.execute_reply.started":"2023-12-06T00:06:36.580889Z","shell.execute_reply":"2023-12-06T00:06:36.592697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ndf = pq.read_table(CUSTOM / 'custom_train_series.parquet').to_pandas()\ndf_test = pq.read_table(ORIGIN / 'test_series.parquet').to_pandas()\ndf_event = pd.read_csv(CUSTOM / 'custom_train_events.csv', index_col=0)\ndf_sample = pd.read_csv(ORIGIN / 'sample_submission.csv', index_col=0)\ndf_id = pd.read_csv(CUSTOM / 'custom_train_stat_groupby_id.csv', index_col=0)\ndf_mean = pd.read_csv(CUSTOM / 'custom_train_series_mean.csv', index_col=0)\ndf_metric = pd.read_csv(CUSTOM / 'metric.csv', index_col=0)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:06:36.594571Z","iopub.execute_input":"2023-12-06T00:06:36.594820Z","iopub.status.idle":"2023-12-06T00:07:12.081133Z","shell.execute_reply.started":"2023-12-06T00:06:36.594798Z","shell.execute_reply":"2023-12-06T00:07:12.080111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 本番テストデータのIDリストが事前のIDリストに含まれていない場合\nfor i in df_test['series_id'].unique():\n    if not i in df_id['series_id'].values:\n        id_map = len(df_id)\n        df_id.loc[id_map] = df_map.iloc[17] # id_map=17 はeventデータが欠損値\n        df_id.loc[df_id.index==id_map, 'series_id'] = i\n        df_id.loc[df_id.index==id_map, 'id_map'] = id_map","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:07:12.082375Z","iopub.execute_input":"2023-12-06T00:07:12.082674Z","iopub.status.idle":"2023-12-06T00:07:12.090919Z","shell.execute_reply.started":"2023-12-06T00:07:12.082649Z","shell.execute_reply":"2023-12-06T00:07:12.090110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_pre(df):\n    df['time'] = df['timestamp'].apply(lambda x: x[:-5]).astype('datetime64[ns]')\n    df = df.merge(df_id[['series_id', 'id_map']], on='series_id', how=\"left\").drop(['series_id', 'timestamp'], axis=1)\n    df.info()\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:07:12.092090Z","iopub.execute_input":"2023-12-06T00:07:12.092624Z","iopub.status.idle":"2023-12-06T00:07:12.117900Z","shell.execute_reply.started":"2023-12-06T00:07:12.092587Z","shell.execute_reply":"2023-12-06T00:07:12.117050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = data_pre(df_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:07:12.119134Z","iopub.execute_input":"2023-12-06T00:07:12.119456Z","iopub.status.idle":"2023-12-06T00:07:12.156245Z","shell.execute_reply.started":"2023-12-06T00:07:12.119427Z","shell.execute_reply":"2023-12-06T00:07:12.155516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# テストデータのIDが事前のIDリストに含まれていない場合\n\n# 24時間周期で anglez と enmo が全く同じ値を取る区間を特定する。\n# 正しく記録できていないケースが多い。\n\ndef null_range(id_map, df=df_test):\n    r = []\n    df = df[df['id_map']==id_map].reset_index(drop=True).copy()\n    if len(df) < STEP_OF_DAY:\n        return r\n    df['a_s'] = df['anglez'].shift(STEP_OF_DAY)\n    df['e_s'] = df['enmo'].shift(STEP_OF_DAY)\n    df['flag'] = (df['anglez']==df['a_s']) & (df['enmo']==df['e_s'])\n    running = df['flag'].groupby(df['flag'].diff().ne(0).cumsum()).cumsum()\n    i = len(df)-1\n    if running[i] >= STEP_OF_DAY:\n        s = running[-running[i]:].index[0]\n        e = i + 1\n        r.append(range(s,e))\n        i = s - 1\n    while i >= 0:\n        if running[i] < STEP_OF_DAY:\n            i -= 1\n        else:\n            e = i + 1\n            s = e - STEP_OF_DAY + 1\n            r.append(range(s,e))\n            i = s - 1\n    return r\n\n# ID別の24時間周期の anglez と enmo の平均値を求めて、df_mean に集約\nwith tqdm(df_id['id_map'][277:]) as t: # 追加分のみ処理\n    for i in t:\n#         r_list = null_range(i)\n#         df_tmp = df_test[df_test['id_map']==i]\n#         for r in r_list:\n#             df_tmp = df_tmp[~df_tmp['step'].isin(r)]\n#         df_tmp['time'] = df_tmp['time'].apply(time_to_step).astype('int16')\n#         IPython.display.clear_output()\n#         df_tmp = df_tmp[['time','id_map','anglez','enmo']].groupby('time').mean()\n#         if len(df_tmp) != STEP_OF_DAY:\n#             raise ValueError(\"24時間分のデータがない\")\n        df_tmp = df_mean[df_mean['id_map']==17]\n        df_tmp.loc[:, 'id_map'] = i\n        df_mean = pd.concat([df_mean, df_tmp])\n    df_mean['id_map'] = df_mean['id_map'].astype('int16')\n    df_mean = df_mean.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:07:12.157365Z","iopub.execute_input":"2023-12-06T00:07:12.158108Z","iopub.status.idle":"2023-12-06T00:07:12.420958Z","shell.execute_reply.started":"2023-12-06T00:07:12.158074Z","shell.execute_reply":"2023-12-06T00:07:12.420087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 移動平均/分散","metadata":{}},{"cell_type":"code","source":"def moving_var(x_array, term=None, w=STEP_OF_MOVING):\n    w = min(w, len(x_array)//4)\n    m_init = np.mean(x_array[:w]); mean_list = [m_init] #平均の初期値\n    v_init = np.var(x_array[:w]); var_list = [v_init] #分散の初期値\n    for i in range(len(x_array)-w): # 更新式\n        mean_list.append(mean_list[-1] + (-x_array[i] +x_array[i+w])/w)\n        var_list.append(var_list[-1]+mean_list[-2]**2 +(x_array[i+w]**2 - x_array[i]**2)/w  -mean_list[-1]**2)\n    if term == 'before':\n        mean_list = mean_list + [mean_list[-1] for _ in range(w-1)]        \n        var_list = var_list + [var_list[-1] for _ in range(w-1)]\n    if term == 'after':\n        mean_list = [mean_list[0] for _ in range(w-1)] + mean_list\n        var_list = [var_list[0] for _ in range(w-1)] + var_list\n    return mean_list, var_list","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:07:12.425669Z","iopub.execute_input":"2023-12-06T00:07:12.426014Z","iopub.status.idle":"2023-12-06T00:07:12.435732Z","shell.execute_reply.started":"2023-12-06T00:07:12.425986Z","shell.execute_reply":"2023-12-06T00:07:12.434924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tmp = df[(df['id_map']==0)&(df['state']!=-1)].reset_index(drop=True)\ndf_tmp","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:07:12.436576Z","iopub.execute_input":"2023-12-06T00:07:12.436833Z","iopub.status.idle":"2023-12-06T00:07:12.769340Z","shell.execute_reply.started":"2023-12-06T00:07:12.436810Z","shell.execute_reply":"2023-12-06T00:07:12.768393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"graphs(df, 0)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:07:12.770468Z","iopub.execute_input":"2023-12-06T00:07:12.770780Z","iopub.status.idle":"2023-12-06T00:07:16.949363Z","shell.execute_reply.started":"2023-12-06T00:07:12.770755Z","shell.execute_reply":"2023-12-06T00:07:16.948432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m1, v1 = moving_var(df_tmp['anglez'], 'after', 12*60)\nm3, v3 = moving_var(df_tmp['anglez'], 'after', 12*60*3)\nm6, v6 = moving_var(df_tmp['anglez'], 'after', 12*60*6)\n\n_, axes = plt.subplots(3, 1, figsize=(20,8))\naxes[0].plot(v1)\naxes[1].plot(v3)\naxes[2].plot(v6)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:07:16.950727Z","iopub.execute_input":"2023-12-06T00:07:16.951338Z","iopub.status.idle":"2023-12-06T00:07:51.914002Z","shell.execute_reply.started":"2023-12-06T00:07:16.951305Z","shell.execute_reply":"2023-12-06T00:07:51.913116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m1, v1 = moving_var(df_tmp['enmo'], 'after', 12*60)\nm3, v3 = moving_var(df_tmp['enmo'], 'after', 12*60*3)\nm6, v6 = moving_var(df_tmp['enmo'], 'after', 12*60*6)\n\n_, axes = plt.subplots(3, 1, figsize=(20,8))\naxes[0].plot(m1)\naxes[1].plot(m3)\naxes[2].plot(m6)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:07:51.915263Z","iopub.execute_input":"2023-12-06T00:07:51.915591Z","iopub.status.idle":"2023-12-06T00:08:26.412877Z","shell.execute_reply.started":"2023-12-06T00:07:51.915565Z","shell.execute_reply":"2023-12-06T00:08:26.411945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 学習データの加工","metadata":{}},{"cell_type":"code","source":"def add_pd(df, id_map, df_mean=df_mean):\n    df_tmp = df_mean[df_mean['id_map']==id_map][['time', 'w_pd', 'o_pd']].rename(columns={'time':'t'})\n    df['t'] = df['time'].apply(time_to_step).astype('int64')\n    df = df.merge(df_tmp, on='t').sort_values('step').drop('t', axis=1)\n    df['pd'] = df['w_pd'] - df['o_pd']\n    df = df.drop(['o_pd', 'w_pd'], axis=1)\n    return df\n\ndef add_warmup(df, id_map, warmup, col, df_mean=df_mean):\n    ts = df['time'].iat[0]\n    ts = ts.timestamp() % SEC_OF_DAY / 5\n    \n    df_tmp = df_mean[df_mean['id_map']==id_map]\n    df_tmp = pd.concat([df_tmp[df_tmp['time']>=ts], df_tmp[df_tmp['time']<ts]])    \n    df_tmp = df_tmp.iloc[-warmup:,:].reset_index(drop=True)\n    df_tmp['step'] = df_tmp.index - warmup\n    df_tmp['pd'] = df_tmp['w_pd'] - df_tmp['o_pd']\n    df_tmp = df_tmp.drop(['o_pd', 'w_pd'], axis=1)\n\n    ts = df['time'].iat[0]\n    ts = [ts + timedelta(seconds=-5*(i+1)) for i in reversed(range(warmup))]\n    df_tmp['time'] = ts\n    df = pd.concat([df_tmp[col], df[col]])\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:08:26.414121Z","iopub.execute_input":"2023-12-06T00:08:26.414455Z","iopub.status.idle":"2023-12-06T00:08:26.426016Z","shell.execute_reply.started":"2023-12-06T00:08:26.414426Z","shell.execute_reply":"2023-12-06T00:08:26.425118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_test_extract(id_map, warmup, n_val=2, n_test=2, df=df, df_id=df_id):\n    df = df[(df['id_map']==id_map) & (df['state']!=-1)].copy() if 'state' in df.columns else df.copy()\n    df = add_pd(df, id_map).reset_index(drop=True)\n    \n    night   = df_id[df_id['id_map']==id_map]['night'].iat[0]\n    n_train = night - n_test - n_val\n    steps = df_event[(df_event['id_map']==id_map) & (df_event['step']!=-1)]['step']\n\n    if n_test:\n        end    = steps.iat[(n_train+n_val)*2]\n        start  = steps.iat[(n_train+n_val)*2+1]\n        s_test = (end + start) // 2\n    else:\n        s_test = df['step'].iat[-1] + 1\n        \n    if n_val:\n        end   = steps.iat[n_train*2]\n        start = steps.iat[n_train*2+1]\n        s_val = (end + start) // 2\n    else:\n        s_val = s_test\n\n    col = ['id_map', 'step', 'time'] + EXP + (OBJ if 'state' in df.columns else [])\n    df = df[col]\n        \n    df_tmp = df[df['step']<s_val].reset_index(drop=True)\n    train = add_warmup(df_tmp, id_map, warmup, col)\n    df_tmp = df[(df['step']>=s_val) & (df['step']<s_test)].reset_index(drop=True)\n    val = add_warmup(df_tmp, id_map, warmup, col) if n_val else df.head(0)\n    df_tmp = df[df['step']>=s_test].reset_index(drop=True)\n    test  = add_warmup(df_tmp, id_map, warmup, col) if n_test else df.head(0)\n    \n    IPython.display.clear_output()\n    print(f'night: {night}')\n    print(f'train: {n_train}')\n    print(f'val  : {n_val}')\n    print(f'test : {n_test}')\n    \n    return train, val, test","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:08:26.427228Z","iopub.execute_input":"2023-12-06T00:08:26.427593Z","iopub.status.idle":"2023-12-06T00:08:26.448033Z","shell.execute_reply.started":"2023-12-06T00:08:26.427558Z","shell.execute_reply":"2023-12-06T00:08:26.447303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_window(hour):\n    rolling = 12 * 60 * hour // STEP_OF_STRIDE\n    warmup = STEP_OF_WARMUP + rolling * STEP_OF_STRIDE\n    return rolling, warmup","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:08:26.449005Z","iopub.execute_input":"2023-12-06T00:08:26.449255Z","iopub.status.idle":"2023-12-06T00:08:26.467202Z","shell.execute_reply.started":"2023-12-06T00:08:26.449232Z","shell.execute_reply":"2023-12-06T00:08:26.466330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w1 = 12 * 5 // STEP_OF_STRIDE # 12  # 移動分散の幅（5分）\nw2 = 12 * 60 * 12 // STEP_OF_STRIDE # 移動分散の移動平均の幅（12時間）\n\nwarmup = STEP_OF_WARMUP + (w1 + w2) * STEP_OF_STRIDE # stride処理前のstepに対してのwarmup\nprint(f'warmup: {warmup}\\nw1: {w1}\\nw2: {w2}')","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:08:26.468359Z","iopub.execute_input":"2023-12-06T00:08:26.468700Z","iopub.status.idle":"2023-12-06T00:08:26.481505Z","shell.execute_reply.started":"2023-12-06T00:08:26.468668Z","shell.execute_reply":"2023-12-06T00:08:26.480652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rolling, warmup = calc_window(12)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:08:26.482552Z","iopub.execute_input":"2023-12-06T00:08:26.482786Z","iopub.status.idle":"2023-12-06T00:08:26.495769Z","shell.execute_reply.started":"2023-12-06T00:08:26.482765Z","shell.execute_reply":"2023-12-06T00:08:26.494902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_map = TRAIN_ID\ntrain, val, test = train_test_extract(id_map, warmup, 5, 5)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:08:26.496890Z","iopub.execute_input":"2023-12-06T00:08:26.497146Z","iopub.status.idle":"2023-12-06T00:08:29.301500Z","shell.execute_reply.started":"2023-12-06T00:08:26.497123Z","shell.execute_reply":"2023-12-06T00:08:29.300546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('original\\n')\ngraphs(df, id_map)\n\nprint('\\ncustom\\n')\ngraphs(df[df['state']!=-1], id_map)\n\nprint('\\ntrain\\n')\ngraphs(train, id_map)\n\nprint('\\nval\\n')\ngraphs(val, id_map)\n\nprint('\\ntest\\n')\ngraphs(test, id_map)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:08:29.302663Z","iopub.execute_input":"2023-12-06T00:08:29.302972Z","iopub.status.idle":"2023-12-06T00:08:46.884272Z","shell.execute_reply.started":"2023-12-06T00:08:29.302934Z","shell.execute_reply":"2023-12-06T00:08:46.883346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:08:46.885387Z","iopub.execute_input":"2023-12-06T00:08:46.885693Z","iopub.status.idle":"2023-12-06T00:08:46.901689Z","shell.execute_reply.started":"2023-12-06T00:08:46.885667Z","shell.execute_reply":"2023-12-06T00:08:46.900806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_map = TEST_ID\ntest, _, _ = train_test_extract(id_map, warmup, 0, 0)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:08:46.902906Z","iopub.execute_input":"2023-12-06T00:08:46.903695Z","iopub.status.idle":"2023-12-06T00:08:48.637929Z","shell.execute_reply.started":"2023-12-06T00:08:46.903662Z","shell.execute_reply":"2023-12-06T00:08:48.637038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('original\\n')\ngraphs(df, id_map)\n\nprint('\\ncustom\\n')\ngraphs(df[df['state']!=-1], id_map)\n\nprint('\\ntest\\n')\ngraphs(test, id_map)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:08:48.639126Z","iopub.execute_input":"2023-12-06T00:08:48.639482Z","iopub.status.idle":"2023-12-06T00:09:02.723464Z","shell.execute_reply.started":"2023-12-06T00:08:48.639449Z","shell.execute_reply":"2023-12-06T00:09:02.722536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def std_arr(arr):\n    return (arr - arr.mean()) / arr.std()\n\ndef std_df(df, cols=EXP):\n    df = df.copy()\n    for col in cols:\n        mean = df[col].mean()\n        std = df[col].std()\n        df[col] = (df[col] - mean) / std\n    return df\n\ndef make_dataset(df, stride=STEP_OF_STRIDE, rate=STEP_OF_RATE, warmup=STEP_OF_WARMUP):\n    warmup = warmup // STEP_OF_RATE\n    ds = timeseries_dataset_from_array(\n        data=df[EXP],\n        targets=df[OBJ][warmup:] if OBJ[0] in df.columns else None,\n        sequence_length=warmup,\n        sampling_rate=rate,\n        sequence_stride=stride\n    )\n    return ds\n\ndef class_weight(df):\n    weight = len(df[df['state']==1]) / len(df[df['state']==0])\n    return {0: weight, 1: 1}\n\ndef handle_history(history, id_map):\n    df = pd.DataFrame.from_dict(history)\n    df['epoch'] = df.index\n    df['id_map'] = id_map\n    return df[['id_map','epoch','loss','accuracy','val_loss','val_accuracy']]\n\ndef prediction(model, df, stride=STEP_OF_STRIDE):\n    ds = make_dataset(std_df(df), stride)\n    pred = model.predict(ds).flatten()\n    pred = pd.Series(pred, index=range(0, len(pred)*stride, stride))\n    plt.figure(figsize=(20,6))\n    plt.plot(pred)\n    plt.plot(df.iloc[STEP_OF_WARMUP:,:]['state'].reset_index(drop=True))\n    plt.show()\n    return pred","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:09:02.724704Z","iopub.execute_input":"2023-12-06T00:09:02.725196Z","iopub.status.idle":"2023-12-06T00:09:02.738029Z","shell.execute_reply.started":"2023-12-06T00:09:02.725020Z","shell.execute_reply":"2023-12-06T00:09:02.737267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# t検定\ndef handle_ttest(d, k):\n    n = len(d)\n    p_values = [[0] * n for _ in range(n)]\n    for i in range(n):\n        for j in range(n):\n            if i == j: continue\n            x1 = d[k[i]]['val_accuracy']\n            x2 = d[k[j]]['val_accuracy']\n            r = ttest_ind(x1, x2, alternative='two-sided', equal_var=False)\n            p_values[i][j] = r.pvalue\n    return p_values","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:22:24.920698Z","iopub.execute_input":"2023-12-06T01:22:24.921442Z","iopub.status.idle":"2023-12-06T01:22:24.927840Z","shell.execute_reply.started":"2023-12-06T01:22:24.921412Z","shell.execute_reply":"2023-12-06T01:22:24.926931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def handle_model(id_map, key=False ,model=False):\n    if not model:\n        model = Sequential([\n            LSTM(128, return_sequences=False),\n            Dropout(0.1),\n            Dense(units=1, activation='sigmoid')\n        ])\n    model.compile(optimizer=Adam(),\n        loss=BinaryCrossentropy(from_logits=False),\n        metrics=[\"accuracy\"])\n    \n    early_stopping = EarlyStopping(monitor='val_accuracy', patience=5, mode='max')\n\n    key = key if key else 'w'\n    checkpoint_callback = ModelCheckpoint(\n        filepath=f'/kaggle/working/id_{id_map:03d}_{key}.tf',\n        monitor='val_accuracy',\n        mode='max',\n        save_best_only=True,\n        save_weights_only=False,\n        verbose=1\n    )\n    \n    return model, early_stopping, checkpoint_callback","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:10:38.379925Z","iopub.execute_input":"2023-12-06T00:10:38.380566Z","iopub.status.idle":"2023-12-06T00:10:38.387615Z","shell.execute_reply.started":"2023-12-06T00:10:38.380535Z","shell.execute_reply":"2023-12-06T00:10:38.386693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ウォームアップの長さ別の比較","metadata":{}},{"cell_type":"code","source":"def GridSearch_warmup(hour):\n    stride = STEP_OF_STRIDE\n    rate   = STEP_OF_RATE\n    warmup = 12 * 60 * hour\n    \n    l = 10\n    with tqdm(df_id[df_id['night']>=l].itertuples()) as t:\n        for row in t:\n            i_time = time.time()\n            id_map = row.id_map\n            n_test = row.night - l\n\n            if id_map > 20: break\n\n            print(f'hour : {hour}')\n            print(f'id   : {id_map}')\n            train, val, test = train_test_extract(id_map, warmup, 2, n_test)\n            ds_train = make_dataset(std_df(train), stride, rate, warmup)\n            ds_val   = make_dataset(std_df(val), stride, rate, warmup)\n\n            model, early_stopping, checkpoint_callback = handle_model(id_map)\n            history = model.fit(ds_train, epochs=3,\n                      validation_data=ds_val,\n                      callbacks=[early_stopping], # checkpoint_callback\n                      class_weight=class_weight(df[df['id_map']==id_map]))\n\n            df_tmp = handle_history(history.history, id_map)\n            df_history = pd.concat([df_history, df_tmp]) if id_map else df_tmp\n\n            i_time = int(time.time() - i_time)\n            df_tmp = df_tmp[df_tmp.index==df_tmp['val_accuracy'].idxmax()]\n            df_tmp['time'] = i_time\n            df_results = pd.concat([df_results, df_tmp]) if id_map else df_tmp\n            IPython.display.clear_output()\n\n    df_history = df_history.reset_index(drop=True)\n    df_results = df_results.reset_index(drop=True)\n    df_history.to_csv(f'history_{hour:02d}.csv')\n    df_results.to_csv(f'results_{hour:02d}.csv')\n    del df_tmp, df_history, df_results; gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T00:34:50.567284Z","iopub.execute_input":"2023-12-05T00:34:50.567982Z","iopub.status.idle":"2023-12-05T00:34:50.578219Z","shell.execute_reply.started":"2023-12-05T00:34:50.567951Z","shell.execute_reply":"2023-12-05T00:34:50.577382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hour_list = [1,3,6,9]\nwith tqdm(hour_list) as t:\n    for hour in t:\n        GridSearch_warmup(hour)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T00:09:14.113915Z","iopub.execute_input":"2023-12-05T00:09:14.114664Z","iopub.status.idle":"2023-12-05T00:28:48.791107Z","shell.execute_reply.started":"2023-12-05T00:09:14.114627Z","shell.execute_reply":"2023-12-05T00:28:48.790140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ac_list = []\nresults = {}\nfor hour in hour_list:\n    results[hour] = pd.read_csv(WORKING / f'results_{hour:02d}.csv', index_col=0)\n    ac_list.append(results[hour]['val_accuracy'].mean())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p_values = handle_ttest(results, hour_list)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,4))\nfor i, (hour, result) in enumerate(results.items()):\n    plt.scatter([i+1 for _ in range(len(result))], result['val_accuracy'], alpha=0.2)\nplt.xticks([0.5,1,2,3,4,4.5], ['','1h','3h','6h','9h',''])\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val = [i+0.5 for i in range(len(hour_list))]\nxlabel, ylabel = [], []\nfor hour, ac in zip(hour_list, ac_list):\n    xlabel.append(f'{hour}h\\n\\nac={ac:.2f}')\n    ylabel.append(f'{hour}h')\n\nax = sns.heatmap(p_values, annot=True, cmap='Reds')\nax.invert_yaxis()\nplt.xticks(val, xlabel)\nplt.yticks(val, ylabel)\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 中間層の設定別の比較","metadata":{}},{"cell_type":"code","source":"def GridSearch_layer(key, model):\n    \n    l = 10\n    with tqdm(df_id[df_id['night']>=l].itertuples()) as t:\n        for row in t:\n            i_time = time.time()\n            id_map = row.id_map\n            n_test = row.night - l\n\n            if id_map > 20: break\n\n            print(f'key  : {key}')\n            print(f'id   : {id_map}')\n            train, val, test = train_test_extract(id_map, warmup, 2, n_test)\n            ds_train = make_dataset(std_df(train))\n            ds_val   = make_dataset(std_df(val))\n\n            model, early_stopping, checkpoint_callback = handle_model(id_map, key, model)\n            history = model.fit(ds_train, epochs=3,\n                      validation_data=ds_val,\n                      callbacks=[early_stopping], # checkpoint_callback\n                      class_weight=class_weight(df[df['id_map']==id_map]))\n\n            df_tmp = handle_history(history.history, id_map)\n            df_history = pd.concat([df_history, df_tmp]) if id_map else df_tmp\n\n            i_time = int(time.time() - i_time)\n            df_tmp = df_tmp[df_tmp.index==df_tmp['val_accuracy'].idxmax()]\n            df_tmp['time'] = i_time\n            df_results = pd.concat([df_results, df_tmp]) if id_map else df_tmp\n            IPython.display.clear_output()\n\n    df_history = df_history.reset_index(drop=True)\n    df_results = df_results.reset_index(drop=True)\n    df_history.to_csv(f'history_{key}.csv')\n    df_results.to_csv(f'results_{key}.csv')\n    del df_tmp, df_history, df_results; gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:09:25.296084Z","iopub.execute_input":"2023-12-06T00:09:25.296766Z","iopub.status.idle":"2023-12-06T00:09:25.307337Z","shell.execute_reply.started":"2023-12-06T00:09:25.296734Z","shell.execute_reply":"2023-12-06T00:09:25.306454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = {}\n\nmodels['s1'] = Sequential([\n    LSTM(32, return_sequences=False),\n    Dropout(0.1),\n    Dense(units=1, activation='sigmoid')\n    ])\n\nmodels['s2'] = Sequential([\n    LSTM(64, return_sequences=False),\n    Dropout(0.1),\n    Dense(units=1, activation='sigmoid')\n    ])\n\nmodels['s3'] = Sequential([\n    LSTM(128, return_sequences=False),\n    Dropout(0.1),\n    Dense(units=1, activation='sigmoid')\n    ])\n\nmodels['d1'] = Sequential([\n    LSTM(32, return_sequences=True),\n    Dropout(0.1),\n    LSTM(32, return_sequences=False),\n    Dropout(0.1),\n    Dense(units=1, activation='sigmoid')\n    ])\n\nmodels['d2'] = Sequential([\n    LSTM(64, return_sequences=True),\n    Dropout(0.1),\n    LSTM(64, return_sequences=False),\n    Dropout(0.1),\n    Dense(units=1, activation='sigmoid')\n    ])\n\nmodels['d3'] = Sequential([\n    LSTM(128, return_sequences=True),\n    Dropout(0.1),\n    LSTM(128, return_sequences=False),\n    Dropout(0.1),\n    Dense(units=1, activation='sigmoid')\n    ])","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:09:25.671545Z","iopub.execute_input":"2023-12-06T00:09:25.672387Z","iopub.status.idle":"2023-12-06T00:09:31.308580Z","shell.execute_reply.started":"2023-12-06T00:09:25.672353Z","shell.execute_reply":"2023-12-06T00:09:31.307478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tqdm(models.items()) as t:\n    for key, model in t:\n        GridSearch_layer(key, model)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T00:10:52.746719Z","iopub.execute_input":"2023-12-06T00:10:52.747605Z","iopub.status.idle":"2023-12-06T00:59:22.211653Z","shell.execute_reply.started":"2023-12-06T00:10:52.747571Z","shell.execute_reply":"2023-12-06T00:59:22.210685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ac_list = []\nresults = {}\nfor key in models:\n    results[key] = pd.read_csv(WORKING / f'results_{key}.csv', index_col=0)\n    ac_list.append(results[key]['val_accuracy'].mean())","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:03:05.865970Z","iopub.execute_input":"2023-12-06T01:03:05.866358Z","iopub.status.idle":"2023-12-06T01:03:05.883157Z","shell.execute_reply.started":"2023-12-06T01:03:05.866327Z","shell.execute_reply":"2023-12-06T01:03:05.882267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p_values = handle_ttest(results, list(models.keys()))","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:23:41.577459Z","iopub.execute_input":"2023-12-06T01:23:41.577871Z","iopub.status.idle":"2023-12-06T01:23:41.593309Z","shell.execute_reply.started":"2023-12-06T01:23:41.577839Z","shell.execute_reply":"2023-12-06T01:23:41.592179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,4))\nfor i, (key, result) in enumerate(results.items()):\n    plt.scatter([i for _ in range(len(result))], result['val_accuracy'], alpha=0.2)\nx = [i for i in range(len(models))]\nplt.xticks(x, results.keys())\nplt.xlim(min(x)-0.5, max(x)+0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:15:05.071398Z","iopub.execute_input":"2023-12-06T01:15:05.071799Z","iopub.status.idle":"2023-12-06T01:15:05.340896Z","shell.execute_reply.started":"2023-12-06T01:15:05.071768Z","shell.execute_reply":"2023-12-06T01:15:05.339694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val = [i+0.5 for i in range(len(models))]\nxlabel, ylabel = [], []\nfor key, ac in zip(models.keys(), ac_list):\n    xlabel.append(f'{key}\\n\\nac={ac:.2f}')\n    ylabel.append(f'{key}')\n\nax = sns.heatmap(p_values, annot=True, cmap='Reds')\nax.invert_yaxis()\nplt.xticks(val, xlabel)\nplt.yticks(val, ylabel)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:22:39.135462Z","iopub.execute_input":"2023-12-06T01:22:39.136319Z","iopub.status.idle":"2023-12-06T01:22:39.428923Z","shell.execute_reply.started":"2023-12-06T01:22:39.136285Z","shell.execute_reply":"2023-12-06T01:22:39.427854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}