{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## The ML Munchers - Child Mind Institute Kaggle Competition","metadata":{"_uuid":"37e105c1-7ad0-46f0-8555-02f3a54aee73","_cell_guid":"029aa51d-9536-42a7-8073-4dad694cd6e0","id":"leS6ESwhLjPE","trusted":true}},{"cell_type":"markdown","source":"### For events: 1 - onset, 0 - wakeup, -1 - neither","metadata":{"_uuid":"19fb6799-6888-449f-aa01-76d9bf980dec","_cell_guid":"493f9d54-f670-4b97-be40-b79b88663809","trusted":true}},{"cell_type":"markdown","source":"## Python library installations","metadata":{"_uuid":"64abe49d-0dff-4c64-bd32-cc13993ea4eb","_cell_guid":"bc37b080-9d7b-4744-ad0d-175d2e7bde0d","id":"x3sUF4-VLwzC","trusted":true}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import average_precision_score\nimport pyarrow.parquet as pq","metadata":{"_uuid":"094d905f-45d7-43f0-ba51-b396c4142f5f","_cell_guid":"014ac155-6305-4b56-9c1e-f304747d2d95","collapsed":false,"id":"pKRJ7WePL6I1","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:35:46.461955Z","iopub.execute_input":"2023-11-06T08:35:46.462370Z","iopub.status.idle":"2023-11-06T08:35:46.467932Z","shell.execute_reply.started":"2023-11-06T08:35:46.462339Z","shell.execute_reply":"2023-11-06T08:35:46.466701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')","metadata":{"_uuid":"7a4ec7de-ff28-4d59-beae-f63a95cce2a1","_cell_guid":"d707bc65-970b-4e09-8d07-95c6d12ac56a","collapsed":false,"executionInfo":{"elapsed":143,"status":"error","timestamp":1697833057734,"user":{"displayName":"Aidan Galia","userId":"05044753196646764953"},"user_tz":420},"id":"n9L7lJYXL-mO","outputId":"209e86a3-9e69-4555-fdf7-27f83e79dfe5","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:35:46.517808Z","iopub.execute_input":"2023-11-06T08:35:46.519008Z","iopub.status.idle":"2023-11-06T08:36:51.207225Z","shell.execute_reply.started":"2023-11-06T08:35:46.518964Z","shell.execute_reply":"2023-11-06T08:36:51.205969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"event = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')","metadata":{"_uuid":"613d9161-f51c-4892-a40c-072ab9c323a7","_cell_guid":"0f8d6274-a2d4-4930-9ecf-50cbadbcbd16","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:36:51.209772Z","iopub.execute_input":"2023-11-06T08:36:51.210236Z","iopub.status.idle":"2023-11-06T08:36:51.240704Z","shell.execute_reply.started":"2023-11-06T08:36:51.210194Z","shell.execute_reply":"2023-11-06T08:36:51.239691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessing","metadata":{"_uuid":"07a66850-6791-4619-9ffc-509b53d75b0f","_cell_guid":"01f05428-baec-4ab7-a3ff-63f7408b7303","trusted":true}},{"cell_type":"code","source":"train.dropna(inplace=True)\n#train.info()","metadata":{"_uuid":"2f386987-dd0a-4848-9b6e-d960e22c5e84","_cell_guid":"53eb695e-f2f9-4aa0-a09b-b527546ee88e","id":"eV9Ki49lUEl8","execution":{"iopub.status.busy":"2023-11-06T08:36:51.242005Z","iopub.execute_input":"2023-11-06T08:36:51.242322Z","iopub.status.idle":"2023-11-06T08:37:26.099626Z","shell.execute_reply.started":"2023-11-06T08:36:51.242294Z","shell.execute_reply":"2023-11-06T08:37:26.098543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"event.dropna(inplace=True)\n#event.info()","metadata":{"_uuid":"fede36d5-3dd8-4421-9de7-df961665fe9e","_cell_guid":"7d42b36f-4b68-4443-aee5-851373d8ad21","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:26.100853Z","iopub.execute_input":"2023-11-06T08:37:26.101174Z","iopub.status.idle":"2023-11-06T08:37:26.113880Z","shell.execute_reply.started":"2023-11-06T08:37:26.101147Z","shell.execute_reply":"2023-11-06T08:37:26.112621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization and Preprocessing Continued","metadata":{"_uuid":"d50da46c-6540-463d-8b72-9eed59d4b4f8","_cell_guid":"ff7c5dbd-154e-486b-93cf-896ee17c0383","trusted":true}},{"cell_type":"code","source":"#Dropping data that we do not need for visualization or training - step from train and night from events\ntrain.drop(['step'], axis=1,inplace=True)\n#train.info()","metadata":{"_uuid":"e3a076d0-78b8-4362-9471-4046cc7f001e","_cell_guid":"269639c6-5ecb-4d84-b5e5-842c07645833","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:26.117442Z","iopub.execute_input":"2023-11-06T08:37:26.118003Z","iopub.status.idle":"2023-11-06T08:37:32.169096Z","shell.execute_reply.started":"2023-11-06T08:37:26.117968Z","shell.execute_reply":"2023-11-06T08:37:32.168005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"event.drop(['night'], axis=1,inplace=True)\n#event.info()","metadata":{"_uuid":"1be167dd-7b52-41b4-8151-b66297d90895","_cell_guid":"c93618ba-f594-4b46-b5c9-987c269fa53b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.170539Z","iopub.execute_input":"2023-11-06T08:37:32.170967Z","iopub.status.idle":"2023-11-06T08:37:32.178376Z","shell.execute_reply.started":"2023-11-06T08:37:32.170932Z","shell.execute_reply":"2023-11-06T08:37:32.176997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### For series id: 0402a003dae9","metadata":{"_uuid":"46a292c5-c435-494b-9e1d-d774551397b5","_cell_guid":"46b3fd85-2748-4081-8a89-7526ffc2daa8","trusted":true}},{"cell_type":"code","source":"'''\ndf1_event = event.drop(event[event['series_id'] != '0402a003dae9'].index, axis=0) \ndisplay(df1_event)\ndf1_series = train.drop(train[train['series_id'] != '0402a003dae9'].index, axis = 0)\ndisplay(df1_series)\ndf1_series.info()\n'''","metadata":{"_uuid":"25da952f-3fc7-48e0-9bd1-bcd06b97b095","_cell_guid":"4b57cc73-673f-4104-a3c3-d007464c4a22","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.179769Z","iopub.execute_input":"2023-11-06T08:37:32.180134Z","iopub.status.idle":"2023-11-06T08:37:32.195010Z","shell.execute_reply.started":"2023-11-06T08:37:32.180105Z","shell.execute_reply":"2023-11-06T08:37:32.193713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n#Enumerating onset and wakeup to create\nfor j in range (0,len(df1_event)):\n    if (df1_event['event'].iloc[j] == 'onset'): \n        df1_event['event'].iloc[j] = 1\n    if (df1_event['event'].iloc[j] == 'wakeup'): \n        df1_event['event'].iloc[j] = 0\n'''","metadata":{"_uuid":"1a62e70c-68a0-40ed-9be4-11acaf238740","_cell_guid":"2711708a-99b1-4b94-85a8-3a781c891a0d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.196561Z","iopub.execute_input":"2023-11-06T08:37:32.196925Z","iopub.status.idle":"2023-11-06T08:37:32.209573Z","shell.execute_reply.started":"2023-11-06T08:37:32.196897Z","shell.execute_reply":"2023-11-06T08:37:32.208680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ndf1_merged = pd.merge(df1_event, df1_series, on=\"timestamp\")\ndisplay(df1_merged)\ndf1_merged.info()\n'''","metadata":{"_uuid":"13f2ee3e-dd7d-4e7f-8171-662ddae0e2c0","_cell_guid":"15b45e67-725c-4db9-bfc5-225df0a11c69","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.211239Z","iopub.execute_input":"2023-11-06T08:37:32.212000Z","iopub.status.idle":"2023-11-06T08:37:32.228937Z","shell.execute_reply.started":"2023-11-06T08:37:32.211958Z","shell.execute_reply":"2023-11-06T08:37:32.227934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df1_merged.drop(['series_id_x','series_id_y'], axis=1, inplace=True)","metadata":{"_uuid":"1dd6eac7-b1ea-4981-89a9-41d189ff092b","_cell_guid":"dfbadbcc-c34e-475c-a410-47375d691c02","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.229925Z","iopub.execute_input":"2023-11-06T08:37:32.230244Z","iopub.status.idle":"2023-11-06T08:37:32.240106Z","shell.execute_reply.started":"2023-11-06T08:37:32.230217Z","shell.execute_reply":"2023-11-06T08:37:32.239039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ndf1_joined = pd.merge(df1_event.drop('series_id',axis=1), df1_series, on=\"timestamp\", how='right')\ndisplay(df1_joined)\ndf1_joined.info()\n'''","metadata":{"_uuid":"78455e7f-ffb3-45d6-a4bf-700065126dd2","_cell_guid":"2c71e1cb-0e71-41df-87ae-da9dd8d687f1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.241084Z","iopub.execute_input":"2023-11-06T08:37:32.241397Z","iopub.status.idle":"2023-11-06T08:37:32.252844Z","shell.execute_reply.started":"2023-11-06T08:37:32.241370Z","shell.execute_reply":"2023-11-06T08:37:32.251848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nfor j in range(0,len(df1_joined)): \n    if(df1_joined['event'].iloc[j] == 1):\n        print(df1_joined.loc[j-10:j+10,:])\n'''","metadata":{"_uuid":"46d9c0ee-b064-4a9a-9f1d-4a2cdc74e0b2","_cell_guid":"8289cece-7b7a-49d2-922f-21e1bc2c6ddf","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.254393Z","iopub.execute_input":"2023-11-06T08:37:32.254782Z","iopub.status.idle":"2023-11-06T08:37:32.265375Z","shell.execute_reply.started":"2023-11-06T08:37:32.254750Z","shell.execute_reply":"2023-11-06T08:37:32.264129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing the relationship between anglez, enmo and the event type\n'''\nnum_interest_cols = ['anglez', 'enmo']\nfor column in num_interest_cols:\n  g = sns.FacetGrid(df1_merged, col='event')\n  g.map(plt.hist, column, bins=20)\n'''","metadata":{"_uuid":"8005e385-0cae-4c20-b766-a697bc299c30","_cell_guid":"cb266f3a-a7d2-477b-858e-612e5c6a3d26","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.267188Z","iopub.execute_input":"2023-11-06T08:37:32.267660Z","iopub.status.idle":"2023-11-06T08:37:32.275923Z","shell.execute_reply.started":"2023-11-06T08:37:32.267600Z","shell.execute_reply":"2023-11-06T08:37:32.274717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing the relationship between anglez, enmo and the step value\n'''\nfor column in num_interest_cols:\n    plt.figure(figsize=(10, 6))\n    plt.scatter(df1_merged[column], df1_merged['step'], alpha=0.5)\n    plt.title(f\"Scatter Plot: step vs. {column}\")\n    plt.xlabel(column)\n    plt.ylabel('step')\n    plt.grid(True)\n    plt.show()\n '''","metadata":{"_uuid":"f25f70e1-f919-4245-8c04-4c1bd30363c3","_cell_guid":"5c3c23c0-fb7b-4ca4-9e42-ce8132d9c161","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.285252Z","iopub.execute_input":"2023-11-06T08:37:32.285866Z","iopub.status.idle":"2023-11-06T08:37:32.293297Z","shell.execute_reply.started":"2023-11-06T08:37:32.285822Z","shell.execute_reply":"2023-11-06T08:37:32.292166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df1_joined['event'] = df1_joined['event'].fillna(-1)\n#display(df1_joined)","metadata":{"_uuid":"574bc43c-3930-4f2a-a48b-309af9f93b6e","_cell_guid":"34c6455b-862a-4d8f-979b-60d8bde5488b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.294757Z","iopub.execute_input":"2023-11-06T08:37:32.295109Z","iopub.status.idle":"2023-11-06T08:37:32.302249Z","shell.execute_reply.started":"2023-11-06T08:37:32.295078Z","shell.execute_reply":"2023-11-06T08:37:32.301089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing the relationship between anglez, enmo, event and the timestamp\n'''\nplt.figure(figsize=(12,8))\n\nax1 = plt.subplot2grid((3,1), (0,0), colspan=1)\nax2 = plt.subplot2grid((3,1), (1,0), colspan=1)\nax3 = plt.subplot2grid((3,1), (2,0), colspan=1)\n\ndf1_joined.plot(x='timestamp',y='anglez', color = 'b',ax=ax1)\ndf1_joined.plot(x='timestamp',y='event', color = 'r',ax=ax2)\ndf1_joined.plot(x='timestamp',y='enmo', color = 'g',ax=ax3)\n'''","metadata":{"_uuid":"2ff64687-a333-446f-9d54-55c156cbcca4","_cell_guid":"1dafd61c-bfb8-4ec7-9bae-5bad91e511d2","collapsed":false,"scrolled":true,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.303729Z","iopub.execute_input":"2023-11-06T08:37:32.304084Z","iopub.status.idle":"2023-11-06T08:37:32.314714Z","shell.execute_reply.started":"2023-11-06T08:37:32.304054Z","shell.execute_reply":"2023-11-06T08:37:32.313353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Random Series ID's:","metadata":{"_uuid":"9ba95845-33c2-40df-b947-8a3c86578f76","_cell_guid":"aa0bb1b2-7195-490f-9318-9e9a4476a3eb","trusted":true}},{"cell_type":"code","source":"# Now we decided to see if our results from 1 series ID and the trends we saw above would carry on to a little larger subset of the data so we decided\n# to use a subset of random series id's and their data to confirm our findings from above\n'''\nimport random\nsubset_size = 3\nsel_id = random.sample(list(train['series_id'].unique()), subset_size)\n#print(sel_id)\n'''","metadata":{"_uuid":"3b0c0e3f-0947-4327-9692-a50346bd1056","_cell_guid":"8383d989-6736-4fa9-ac7a-e62be3b013e1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:32.316744Z","iopub.execute_input":"2023-11-06T08:37:32.317111Z","iopub.status.idle":"2023-11-06T08:37:43.947045Z","shell.execute_reply.started":"2023-11-06T08:37:32.317081Z","shell.execute_reply":"2023-11-06T08:37:43.945898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nftrain = pd.DataFrame(columns=train.columns)\nfor series_id in sel_id:\n    selected_rows = train[train['series_id'] == series_id]\n    ftrain = pd.concat([ftrain, selected_rows], ignore_index=True)\n    '''\n#print(ftrain)","metadata":{"_uuid":"2f313df1-9d14-455a-b1a6-c6ebf1ff0a92","_cell_guid":"9c046787-6830-43a7-9b3e-0728a9575d6b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:37:43.948289Z","iopub.execute_input":"2023-11-06T08:37:43.948609Z","iopub.status.idle":"2023-11-06T08:38:54.766680Z","shell.execute_reply.started":"2023-11-06T08:37:43.948582Z","shell.execute_reply":"2023-11-06T08:38:54.765480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nfevent = pd.DataFrame(columns=event.columns)\nfor series_id in sel_id:\n    selected_rows2 = event[event['series_id'] == series_id]\n    fevent = pd.concat([fevent, selected_rows2], ignore_index=True)\n    '''\n#print(fevent)","metadata":{"_uuid":"f3f912c6-60ab-4767-9b89-9ef1df5f914b","_cell_guid":"3860d742-f54c-4fa4-afc7-c1e4957efb91","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:38:54.768235Z","iopub.execute_input":"2023-11-06T08:38:54.768993Z","iopub.status.idle":"2023-11-06T08:38:54.789983Z","shell.execute_reply.started":"2023-11-06T08:38:54.768949Z","shell.execute_reply":"2023-11-06T08:38:54.789018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Enumerating onset and wakeup to 1 and 0 for easier identification of trends\n'''\nfor j in range (0,len(fevent)):\n    if (fevent['event'].iloc[j] == 'onset'): \n        fevent['event'].iloc[j] = 1\n    if (fevent['event'].iloc[j] == 'wakeup'): \n        fevent['event'].iloc[j] = 0\n        '''","metadata":{"_uuid":"c261c1a9-4317-4f0a-bfd2-7b40682e69e0","_cell_guid":"0957dca6-a192-4adb-a6b1-2e18fd6a583d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:38:54.791806Z","iopub.execute_input":"2023-11-06T08:38:54.792152Z","iopub.status.idle":"2023-11-06T08:38:54.836740Z","shell.execute_reply.started":"2023-11-06T08:38:54.792122Z","shell.execute_reply":"2023-11-06T08:38:54.835942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dfrand_joined = pd.merge(fevent, ftrain, on=['timestamp','series_id'], how='right')\n#display(dfrand_joined)\n#dfrand_joined.info()","metadata":{"_uuid":"aa6b2ecd-2873-4003-8ebb-4a86e956e1e0","_cell_guid":"09e13b4e-b7ad-4b44-ab29-52a72acb04f9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:38:54.838166Z","iopub.execute_input":"2023-11-06T08:38:54.838500Z","iopub.status.idle":"2023-11-06T08:38:54.842782Z","shell.execute_reply.started":"2023-11-06T08:38:54.838472Z","shell.execute_reply":"2023-11-06T08:38:54.841695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dfrand_joined['event'] = dfrand_joined['event'].fillna(-1)\n#display(dfrand_joined)","metadata":{"_uuid":"49843786-c427-4477-a02e-e65a7d605414","_cell_guid":"20fa5ed4-a966-4e28-b8bf-de0067498867","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:38:54.844499Z","iopub.execute_input":"2023-11-06T08:38:54.844957Z","iopub.status.idle":"2023-11-06T08:38:54.854179Z","shell.execute_reply.started":"2023-11-06T08:38:54.844918Z","shell.execute_reply":"2023-11-06T08:38:54.853045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Various visualizations of the features anglez and enmo with relation to our target values\n'''\nplot1 = dfrand_joined[dfrand_joined['series_id'] == sel_id[0]]\nplot2 = dfrand_joined[dfrand_joined['series_id'] == sel_id[1]]\nplot3 = dfrand_joined[dfrand_joined['series_id'] == sel_id[2]]\n'''","metadata":{"_uuid":"afcb986f-2320-44d1-abd2-3c4c2af6e4a3","_cell_guid":"370a52bd-6729-40e7-8af4-fefb242d6003","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:38:54.855842Z","iopub.execute_input":"2023-11-06T08:38:54.856271Z","iopub.status.idle":"2023-11-06T08:38:54.866057Z","shell.execute_reply.started":"2023-11-06T08:38:54.856232Z","shell.execute_reply":"2023-11-06T08:38:54.865058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nplt.figure(figsize=(12,8))\n\nax1 = plt.subplot2grid((3,1), (0,0), colspan=1)\nax2 = plt.subplot2grid((3,1), (1,0), colspan=1)\nax3 = plt.subplot2grid((3,1), (2,0), colspan=1)\n\nplt.suptitle(\"series id: \" + sel_id[0])\n\nplot1.plot(x='timestamp',y='anglez', color = 'b',ax=ax1)\nplot1.plot(x='timestamp',y='event', color = 'r',ax=ax2)\nplot1.plot(x='timestamp',y='enmo', color = 'g',ax=ax3)\n'''","metadata":{"_uuid":"583ca683-7341-4b4c-8c03-e20799bd94af","_cell_guid":"65caf7fe-e868-4fcc-bb1f-ae8f310bc7d1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:38:54.867080Z","iopub.execute_input":"2023-11-06T08:38:54.867406Z","iopub.status.idle":"2023-11-06T08:38:54.877443Z","shell.execute_reply.started":"2023-11-06T08:38:54.867366Z","shell.execute_reply":"2023-11-06T08:38:54.876210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nplt.figure(figsize=(12,8))\n\nax1 = plt.subplot2grid((3,1), (0,0), colspan=1)\nax2 = plt.subplot2grid((3,1), (1,0), colspan=1)\nax3 = plt.subplot2grid((3,1), (2,0), colspan=1)\n\nplt.suptitle(\"series id: \" + sel_id[1])\n\nplot2.plot(x='timestamp',y='anglez', color = 'b',ax=ax1)\nplot2.plot(x='timestamp',y='event', color = 'r',ax=ax2)\nplot2.plot(x='timestamp',y='enmo', color = 'g',ax=ax3)\n'''","metadata":{"_uuid":"86cf470b-16f3-46a8-9ee5-380646365b2c","_cell_guid":"138f21da-b535-40df-b4b5-fe7729af35a1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:38:54.878987Z","iopub.execute_input":"2023-11-06T08:38:54.879353Z","iopub.status.idle":"2023-11-06T08:38:54.888876Z","shell.execute_reply.started":"2023-11-06T08:38:54.879323Z","shell.execute_reply":"2023-11-06T08:38:54.887577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nplt.figure(figsize=(12,8))\n\nax1 = plt.subplot2grid((3,1), (0,0), colspan=1)\nax2 = plt.subplot2grid((3,1), (1,0), colspan=1)\nax3 = plt.subplot2grid((3,1), (2,0), colspan=1)\n\nplt.suptitle(\"series id: \" + sel_id[2])\n\nplot3.plot(x='timestamp',y='anglez', color = 'b',ax=ax1)\nplot3.plot(x='timestamp',y='event', color = 'r',ax=ax2)\nplot3.plot(x='timestamp',y='enmo', color = 'g',ax=ax3)\n'''","metadata":{"_uuid":"e8f209da-f36d-419a-9e10-cf84909f9007","_cell_guid":"7b1be22f-c1a1-438a-b500-834189a8471e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:38:54.890085Z","iopub.execute_input":"2023-11-06T08:38:54.890392Z","iopub.status.idle":"2023-11-06T08:38:54.901908Z","shell.execute_reply.started":"2023-11-06T08:38:54.890366Z","shell.execute_reply":"2023-11-06T08:38:54.900792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nfor column in num_interest_cols:\n    plt.figure(figsize=(10, 6))\n    plt.scatter(dfrand_joined[column], dfrand_joined['step'], alpha=0.5)\n    plt.title(f\"Scatter Plot: step vs. {column}\")\n    plt.xlabel(column)\n    plt.ylabel('step')\n    plt.grid(True)\n    plt.show()\n'''","metadata":{"_uuid":"3b916a1b-49be-4575-b77c-6e389b630c0b","_cell_guid":"dc3b4747-4927-4632-b087-688ad541dc7f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-06T08:38:54.903247Z","iopub.execute_input":"2023-11-06T08:38:54.903587Z","iopub.status.idle":"2023-11-06T08:38:54.914872Z","shell.execute_reply.started":"2023-11-06T08:38:54.903555Z","shell.execute_reply":"2023-11-06T08:38:54.913757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Thus far, we have joined the train_series parquet file and the train_events csv file for a subset of rnadom series IDs based on the timestamps available. This allowed us to visualize the data and confirm our hypothesis that enmo and anglez are the two features that are most correlated to wakeup and onset events. For preprocessing, we removed null values from the events data, dropped the step feature from the train dataset, and the nights feature from the events data, enumerated the events column of the events dataset, joined the datasets such that the events data correlates to the series data based on timestamps, and gives labels for the desired portions such that 1 indicates wakeup, 0 indicates onset, and -1 indicates neither onset nor wakeup. Finally, for visualization, we plotted each of the random series ID's data (anglez, enmo, and event) against timestamp to see the correlation between the three. We also plotted scatterplots of anglez and enmo against step.  For training, we will again drop the undesired columns, merge the data like we've done, enumerate the events, and train it on the merged data without the events category as features and the events category as labels.","metadata":{"_uuid":"f6092d13-e3d1-4abc-bd79-404dd2700aca","_cell_guid":"6d1f3ace-c5a0-4ec5-9f9f-c0debfcabb75","trusted":true}},{"cell_type":"markdown","source":"## Training","metadata":{}},{"cell_type":"code","source":"#Now we are enumerating the values of onset and wakeup in the entire event set so we can use that for training\nfor j in range (0,len(event)):\n    if (event['event'].iloc[j] == 'onset'): \n        event['event'].iloc[j] = 1\n    if (event['event'].iloc[j] == 'wakeup'): \n        event['event'].iloc[j] = 0","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:38:54.916605Z","iopub.execute_input":"2023-11-06T08:38:54.916984Z","iopub.status.idle":"2023-11-06T08:38:59.884423Z","shell.execute_reply.started":"2023-11-06T08:38:54.916945Z","shell.execute_reply":"2023-11-06T08:38:59.879722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Merge the two files\ndf = pd.merge(event, train, on=['timestamp','series_id'], how='right')","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:38:59.885743Z","iopub.execute_input":"2023-11-06T08:38:59.886182Z","iopub.status.idle":"2023-11-06T08:43:15.139249Z","shell.execute_reply.started":"2023-11-06T08:38:59.886143Z","shell.execute_reply":"2023-11-06T08:43:15.138033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#We are taking the indicies of the rows to use for extracting the timestamps and series ids of our test data for use in creating the CSV file later\n#df['rows'] = range(len(df))\n#df['idrows'] = range(len(df))","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:15.140751Z","iopub.execute_input":"2023-11-06T08:43:15.141703Z","iopub.status.idle":"2023-11-06T08:43:15.146779Z","shell.execute_reply.started":"2023-11-06T08:43:15.141661Z","shell.execute_reply":"2023-11-06T08:43:15.145621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(df.head())","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:15.148933Z","iopub.execute_input":"2023-11-06T08:43:15.149378Z","iopub.status.idle":"2023-11-06T08:43:15.160032Z","shell.execute_reply.started":"2023-11-06T08:43:15.149338Z","shell.execute_reply":"2023-11-06T08:43:15.158702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setting up X and y to create our train test split by isolating the data we need from the overall data sets given to us\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_error, accuracy_score\n\ndf = df.dropna(subset=['event'])\ndf = df.dropna(subset=['step'])\n\nX = df[['anglez', 'enmo']]\ny = df[['event']].astype('int')\n","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:15.161511Z","iopub.execute_input":"2023-11-06T08:43:15.161967Z","iopub.status.idle":"2023-11-06T08:43:26.624028Z","shell.execute_reply.started":"2023-11-06T08:43:15.161927Z","shell.execute_reply":"2023-11-06T08:43:26.622867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and test sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)  # 80% training, 20% test","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:26.625473Z","iopub.execute_input":"2023-11-06T08:43:26.626076Z","iopub.status.idle":"2023-11-06T08:43:26.634578Z","shell.execute_reply.started":"2023-11-06T08:43:26.626045Z","shell.execute_reply":"2023-11-06T08:43:26.633204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Here we extract the timestamps and series ids that correlate to our test split of the indicies of test data \n#timestamps_test = df.loc[row_test, 'timestamp'].values\n#id_test = df.loc[id_test, 'series_id'].values","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:26.636222Z","iopub.execute_input":"2023-11-06T08:43:26.636622Z","iopub.status.idle":"2023-11-06T08:43:26.643273Z","shell.execute_reply.started":"2023-11-06T08:43:26.636591Z","shell.execute_reply":"2023-11-06T08:43:26.642054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#X_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:26.644530Z","iopub.execute_input":"2023-11-06T08:43:26.644902Z","iopub.status.idle":"2023-11-06T08:43:26.658777Z","shell.execute_reply.started":"2023-11-06T08:43:26.644874Z","shell.execute_reply":"2023-11-06T08:43:26.657508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:26.661144Z","iopub.execute_input":"2023-11-06T08:43:26.662133Z","iopub.status.idle":"2023-11-06T08:43:26.669659Z","shell.execute_reply.started":"2023-11-06T08:43:26.662089Z","shell.execute_reply":"2023-11-06T08:43:26.668343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:26.671807Z","iopub.execute_input":"2023-11-06T08:43:26.672254Z","iopub.status.idle":"2023-11-06T08:43:26.680919Z","shell.execute_reply.started":"2023-11-06T08:43:26.672211Z","shell.execute_reply":"2023-11-06T08:43:26.679725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a Random Forest Classifier instance\nfrom sklearn.ensemble import RandomForestClassifier\n\nclf = RandomForestClassifier(n_estimators=1000, random_state=42, oob_score=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:26.682365Z","iopub.execute_input":"2023-11-06T08:43:26.683232Z","iopub.status.idle":"2023-11-06T08:43:26.693391Z","shell.execute_reply.started":"2023-11-06T08:43:26.683199Z","shell.execute_reply":"2023-11-06T08:43:26.692187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the data\nclf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:26.694679Z","iopub.execute_input":"2023-11-06T08:43:26.695112Z","iopub.status.idle":"2023-11-06T08:43:43.368160Z","shell.execute_reply.started":"2023-11-06T08:43:26.695073Z","shell.execute_reply":"2023-11-06T08:43:43.366616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n# Predicting the target for test data\ny_pred = clf.predict(X_test)\n\n# Evaluating the classifier\naccuracy = accuracy_score(y_test, y_pred)\n#print(f\"Accuracy: {accuracy}\")\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.369605Z","iopub.execute_input":"2023-11-06T08:43:43.370465Z","iopub.status.idle":"2023-11-06T08:43:43.377097Z","shell.execute_reply.started":"2023-11-06T08:43:43.370433Z","shell.execute_reply":"2023-11-06T08:43:43.375982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.384885Z","iopub.execute_input":"2023-11-06T08:43:43.385233Z","iopub.status.idle":"2023-11-06T08:43:43.389441Z","shell.execute_reply.started":"2023-11-06T08:43:43.385205Z","shell.execute_reply":"2023-11-06T08:43:43.388524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Post-processing - Accuracy Analysis and Visualization","metadata":{}},{"cell_type":"code","source":"# This method helps create a nice visualization for the confusion matrix\n'''\nimport itertools\ndef plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    print(cm)\n\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    fmt = '.2f' if normalize else 'd'\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, format(cm[i, j], fmt),\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    plt.tight_layout()\n    '''","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.390686Z","iopub.execute_input":"2023-11-06T08:43:43.391193Z","iopub.status.idle":"2023-11-06T08:43:43.405300Z","shell.execute_reply.started":"2023-11-06T08:43:43.391164Z","shell.execute_reply":"2023-11-06T08:43:43.403929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nfrom sklearn.metrics import confusion_matrix\ncnf_matrix = confusion_matrix(y_test,y_pred)\nplt.figure()\nplot_confusion_matrix(cnf_matrix, classes=[], normalize=False,title='Confusion matrix')\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.406762Z","iopub.execute_input":"2023-11-06T08:43:43.407109Z","iopub.status.idle":"2023-11-06T08:43:43.421740Z","shell.execute_reply.started":"2023-11-06T08:43:43.407080Z","shell.execute_reply":"2023-11-06T08:43:43.420431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building random forest decision tree\n'''\nimport matplotlib.pyplot as plt\nfrom sklearn.tree import plot_tree\nsleep_feature_names = list(df[['anglez', 'enmo']])\nsleep_class_names = list(df['event'])\n\n\nfig = plt.figure(figsize=(15, 10))\nplot_tree(clf.estimators_[0], \n          feature_names = str(sleep_feature_names),\n          class_names = str(sleep_class_names),\n          filled=True, impurity=True, \n          rounded=True);\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.422933Z","iopub.execute_input":"2023-11-06T08:43:43.423246Z","iopub.status.idle":"2023-11-06T08:43:43.432369Z","shell.execute_reply.started":"2023-11-06T08:43:43.423220Z","shell.execute_reply":"2023-11-06T08:43:43.431242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nfrom sklearn.metrics import recall_score, precision_score, r2_score, confusion_matrix\n\n#test_error_score = r2_score(y_test, y_pred) \n#print(\"R squared Error - Test: \", test_error_score) \n\nprecision = precision_score(y_test, y_pred, average=\"weighted\")\nrecall = recall_score(y_test, y_pred, average=\"weighted\")\n'''\n'''\nprint (\"Precision:\", precision)\nprint (\"Recall:\", recall)\nprint (\"Out of Bag Score:\", clf.oob_score_)\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.433856Z","iopub.execute_input":"2023-11-06T08:43:43.434803Z","iopub.status.idle":"2023-11-06T08:43:43.445350Z","shell.execute_reply.started":"2023-11-06T08:43:43.434771Z","shell.execute_reply":"2023-11-06T08:43:43.444164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Working with test file and Creating CSV","metadata":{}},{"cell_type":"code","source":"test = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.446577Z","iopub.execute_input":"2023-11-06T08:43:43.447142Z","iopub.status.idle":"2023-11-06T08:43:43.468556Z","shell.execute_reply.started":"2023-11-06T08:43:43.447112Z","shell.execute_reply":"2023-11-06T08:43:43.467685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Ypred = clf.predict(test[['anglez','enmo']])","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.469833Z","iopub.execute_input":"2023-11-06T08:43:43.470344Z","iopub.status.idle":"2023-11-06T08:43:43.638860Z","shell.execute_reply.started":"2023-11-06T08:43:43.470314Z","shell.execute_reply":"2023-11-06T08:43:43.637560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This function does a moving average on the predicted values over a window, smoothing out the values to enable easier step calculation\ndef get_moving_average(y_pred):\n    window_size = 5\n    vals = pd.Series(y_pred)\n    window = vals.rolling(window_size).mean()\n    window = window.apply(lambda x: 1 if x > 0.5 else 0)\n    return window","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.640154Z","iopub.execute_input":"2023-11-06T08:43:43.640493Z","iopub.status.idle":"2023-11-06T08:43:43.646945Z","shell.execute_reply.started":"2023-11-06T08:43:43.640466Z","shell.execute_reply":"2023-11-06T08:43:43.645782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_smooth = get_moving_average(Ypred)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.648830Z","iopub.execute_input":"2023-11-06T08:43:43.649507Z","iopub.status.idle":"2023-11-06T08:43:43.660020Z","shell.execute_reply.started":"2023-11-06T08:43:43.649466Z","shell.execute_reply":"2023-11-06T08:43:43.658911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculating the steps from the timestamps obtained earlier by converting them to seconds and then dividing by 5 since every step is 5 seconds\ntimediff = []\nsteps = pd.DataFrame({'step': [0]})\n\ndater = [str(ts) for ts in test['timestamp']]\ntime = pd.to_datetime(test['timestamp'], format='%Y-%m-%dT%H:%M:%S%z')","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.661401Z","iopub.execute_input":"2023-11-06T08:43:43.661733Z","iopub.status.idle":"2023-11-06T08:43:43.681068Z","shell.execute_reply.started":"2023-11-06T08:43:43.661704Z","shell.execute_reply":"2023-11-06T08:43:43.679411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diffsec = []\nsteps = []\nfor i in range(len(time)):\n    diffsec = abs((time.iloc[0] - time.iloc[i]).total_seconds())\n    steps.append(diffsec / 5)    ","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.682717Z","iopub.execute_input":"2023-11-06T08:43:43.683546Z","iopub.status.idle":"2023-11-06T08:43:43.708277Z","shell.execute_reply.started":"2023-11-06T08:43:43.683506Z","shell.execute_reply":"2023-11-06T08:43:43.707246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculation of confidence scores for CSV file\nscores = clf.predict_proba(test[['anglez','enmo']])[:, 1]","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.710038Z","iopub.execute_input":"2023-11-06T08:43:43.710393Z","iopub.status.idle":"2023-11-06T08:43:43.882301Z","shell.execute_reply.started":"2023-11-06T08:43:43.710325Z","shell.execute_reply":"2023-11-06T08:43:43.881116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting back to onset and wakeup\nfor j in range (0,len(y_pred_smooth)):\n    if (y_pred_smooth.iloc[j] == 1): \n        y_pred_smooth.iloc[j] = 'onset'\n    if (y_pred_smooth.iloc[j] == 0): \n        y_pred_smooth.iloc[j] = 'wakeup'","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.884005Z","iopub.execute_input":"2023-11-06T08:43:43.884432Z","iopub.status.idle":"2023-11-06T08:43:43.927401Z","shell.execute_reply.started":"2023-11-06T08:43:43.884391Z","shell.execute_reply":"2023-11-06T08:43:43.925942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rounded_steps = [round(step) for step in steps]\nrounded_steps = list(map(int, rounded_steps))","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.930677Z","iopub.execute_input":"2023-11-06T08:43:43.931534Z","iopub.status.idle":"2023-11-06T08:43:43.938431Z","shell.execute_reply.started":"2023-11-06T08:43:43.931489Z","shell.execute_reply":"2023-11-06T08:43:43.937148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#len(scores)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.939697Z","iopub.execute_input":"2023-11-06T08:43:43.940125Z","iopub.status.idle":"2023-11-06T08:43:43.948331Z","shell.execute_reply.started":"2023-11-06T08:43:43.940085Z","shell.execute_reply":"2023-11-06T08:43:43.947075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission CSV file creation\nsubmission = pd.DataFrame({\n    'series_id': test['series_id'],\n    'step': rounded_steps,\n    'event': y_pred_smooth,\n    'score': scores\n})\nsubmission.insert(0, 'row_id', range(len(test['series_id'])))\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T08:43:43.950203Z","iopub.execute_input":"2023-11-06T08:43:43.950689Z","iopub.status.idle":"2023-11-06T08:43:43.967755Z","shell.execute_reply.started":"2023-11-06T08:43:43.950628Z","shell.execute_reply":"2023-11-06T08:43:43.966457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Rationale and Results","metadata":{}},{"cell_type":"markdown","source":"Overall, we realized that anglez and enmo were decent predictors for the event type so we did not do much preprocessing on them this time around. We decided to use random forests because we have a classification problem at hand with two different classes. We first thought of using SVM but felt that the data set was really big and would be hard to use SVM, sticking to random forests instead. Our results were showing that we have around 56% accuracy when it comes to predicting the values correctly. We plan on improving this in future iterations by doing a moving average of anglez and enmo to create better features to build our model upon and see if our predictions improve. ","metadata":{}}]}