{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":82611,"databundleVersionId":9553358,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"SEED = 42\n\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns \nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:26.433457Z","iopub.execute_input":"2024-10-16T10:18:26.434007Z","iopub.status.idle":"2024-10-16T10:18:26.440399Z","shell.execute_reply.started":"2024-10-16T10:18:26.433945Z","shell.execute_reply":"2024-10-16T10:18:26.439065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/brist1d/train.csv',header = 0,index_col =0)\ntest = pd.read_csv('/kaggle/input/brist1d/test.csv',header = 0,index_col =0)\nsubmission = pd.read_csv('/kaggle/input/brist1d/sample_submission.csv',header = 0,index_col =0)\ntrain.shape, test.shape, submission.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:26.442458Z","iopub.execute_input":"2024-10-16T10:18:26.442888Z","iopub.status.idle":"2024-10-16T10:18:39.516520Z","shell.execute_reply.started":"2024-10-16T10:18:26.442847Z","shell.execute_reply":"2024-10-16T10:18:39.515257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['p_num'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:39.517855Z","iopub.execute_input":"2024-10-16T10:18:39.518271Z","iopub.status.idle":"2024-10-16T10:18:39.563112Z","shell.execute_reply.started":"2024-10-16T10:18:39.518231Z","shell.execute_reply":"2024-10-16T10:18:39.561656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"G_p01 = train[train['p_num']=='p01'].iloc[:,1:74]\nG_p02 = train[train['p_num']=='p02'].iloc[:,1:74]\nG_p03 = train[train['p_num']=='p03'].iloc[:,1:74]\nG_p04 = train[train['p_num']=='p04'].iloc[:,1:74]\nG_p05 = train[train['p_num']=='p05'].iloc[:,1:74]\nG_p06 = train[train['p_num']=='p06'].iloc[:,1:74]\nG_p10 = train[train['p_num']=='p10'].iloc[:,1:74]\nG_p11 = train[train['p_num']=='p11'].iloc[:,1:74]\nG_p12 = train[train['p_num']=='p12'].iloc[:,1:74]\n\nG_all = train.iloc[:,1:74]","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:39.566326Z","iopub.execute_input":"2024-10-16T10:18:39.567232Z","iopub.status.idle":"2024-10-16T10:18:40.322736Z","shell.execute_reply.started":"2024-10-16T10:18:39.567171Z","shell.execute_reply":"2024-10-16T10:18:40.321288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(nrows=1, ncols=1, figsize=(16,16))\n\nsns.heatmap(G_all.iloc[:,1:].isna(), cmap='Blues')\nax.set_title('Missing Values', fontsize=16)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:40.324752Z","iopub.execute_input":"2024-10-16T10:18:40.325343Z","iopub.status.idle":"2024-10-16T10:18:53.997557Z","shell.execute_reply.started":"2024-10-16T10:18:40.325283Z","shell.execute_reply":"2024-10-16T10:18:53.996273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g_p01 = train[train['p_num']=='p01']['bg+1:00']\ng_p02 = train[train['p_num']=='p02']['bg+1:00']\ng_p03 = train[train['p_num']=='p03']['bg+1:00']\ng_p04 = train[train['p_num']=='p04']['bg+1:00']\ng_p05 = train[train['p_num']=='p05']['bg+1:00']\ng_p06 = train[train['p_num']=='p06']['bg+1:00']\ng_p10 = train[train['p_num']=='p10']['bg+1:00']\ng_p11 = train[train['p_num']=='p11']['bg+1:00']\ng_p12 = train[train['p_num']=='p12']['bg+1:00']\n\ng_all = train['bg+1:00']","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:53.999575Z","iopub.execute_input":"2024-10-16T10:18:54.000109Z","iopub.status.idle":"2024-10-16T10:18:54.700476Z","shell.execute_reply.started":"2024-10-16T10:18:54.000043Z","shell.execute_reply":"2024-10-16T10:18:54.699239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g_p01.mean(), g_p01.std(),g_p02.mean(), g_p02.std(),g_p03.mean(), g_p03.std(),g_p04.mean(), g_p04.std(),g_p05.mean(), g_p05.std(),g_p06.mean(), g_p06.std(),g_p10.mean(), g_p10.std(),g_p11.mean(), g_p11.std(),g_p12.mean(), g_p12.std()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:54.702387Z","iopub.execute_input":"2024-10-16T10:18:54.703047Z","iopub.status.idle":"2024-10-16T10:18:54.721680Z","shell.execute_reply.started":"2024-10-16T10:18:54.702988Z","shell.execute_reply":"2024-10-16T10:18:54.719890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g_all.mean(), g_all.std()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:54.723426Z","iopub.execute_input":"2024-10-16T10:18:54.723921Z","iopub.status.idle":"2024-10-16T10:18:54.735332Z","shell.execute_reply.started":"2024-10-16T10:18:54.723867Z","shell.execute_reply":"2024-10-16T10:18:54.733782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g_all.plot.kde(style=['r-'])","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:54.739505Z","iopub.execute_input":"2024-10-16T10:18:54.739951Z","iopub.status.idle":"2024-10-16T10:18:58.464517Z","shell.execute_reply.started":"2024-10-16T10:18:54.739907Z","shell.execute_reply":"2024-10-16T10:18:58.463071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Indivisual pattern","metadata":{}},{"cell_type":"code","source":"from pandas.tseries.offsets import Minute\nimport re","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:58.466080Z","iopub.execute_input":"2024-10-16T10:18:58.466504Z","iopub.status.idle":"2024-10-16T10:18:58.473048Z","shell.execute_reply.started":"2024-10-16T10:18:58.466453Z","shell.execute_reply":"2024-10-16T10:18:58.471084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train\ndf['time'] = pd.to_datetime(df['time'], format='%H:%M:%S')\nbg_cols = [col for col in df.columns if col.startswith('bg-')]\n\ntime_offsets = {}\nfor col in bg_cols:\n    match = re.search(r'bg-(\\d+):(\\d+)', col)\n    if match:\n        hours, minutes = int(match.group(1)), int(match.group(2))\n        total_minutes = hours * 60 + minutes  \n        offset = -(total_minutes)  \n        time_offsets[col] = offset\n","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:58.474731Z","iopub.execute_input":"2024-10-16T10:18:58.475197Z","iopub.status.idle":"2024-10-16T10:18:58.627461Z","shell.execute_reply.started":"2024-10-16T10:18:58.475155Z","shell.execute_reply":"2024-10-16T10:18:58.625673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"time_series_list = []\n\nfor col, offset in time_offsets.items():\n    temp_df = train[['p_num', 'time']].copy()  \n    temp_df['measurement_time'] = train['time'] + Minute(offset)  \n    temp_df['value'] = train[col] \n    \n    temp_df['original_time'] = train['time'] \n    temp_df['bg_column'] = col  #\n    time_series_list.append(temp_df)\n\ntime_series_data = pd.concat(time_series_list, ignore_index=True).dropna(subset=['value'])","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:18:58.629792Z","iopub.execute_input":"2024-10-16T10:18:58.630410Z","iopub.status.idle":"2024-10-16T10:19:01.016992Z","shell.execute_reply.started":"2024-10-16T10:18:58.630353Z","shell.execute_reply":"2024-10-16T10:19:01.015910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"time_series_data['measurement_time'] = pd.to_datetime(time_series_data['measurement_time'])\ntime_series_data['hour'] = time_series_data['measurement_time'].dt.hour\nhourly_avg_by_pnum = time_series_data.groupby(['p_num', 'hour'])['value'].mean().unstack()\n\nplt.figure(figsize=(12, 8))\nfor p_num in hourly_avg_by_pnum.index:\n    plt.plot(hourly_avg_by_pnum.columns, hourly_avg_by_pnum.loc[p_num], marker='o', label=f'p_num: {p_num}')\n\nplt.title('Average Value by Hour for Each p_num (24-hour cycle)')\nplt.xlabel('Hour of the Day')\nplt.ylabel('Average Value')\nplt.grid(True)\nplt.xticks(range(24))  \nplt.legend(title='p_num')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T10:19:01.018279Z","iopub.execute_input":"2024-10-16T10:19:01.018654Z","iopub.status.idle":"2024-10-16T10:19:03.569789Z","shell.execute_reply.started":"2024-10-16T10:19:01.018615Z","shell.execute_reply":"2024-10-16T10:19:03.568614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# There is a possibility that p_01's lifestyle rhythm is different from the others.\n# Also, be careful of p_06 and p_11!","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}