{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-11T18:56:36.558840Z","iopub.execute_input":"2023-09-11T18:56:36.559263Z","iopub.status.idle":"2023-09-11T18:56:39.051713Z","shell.execute_reply.started":"2023-09-11T18:56:36.559229Z","shell.execute_reply":"2023-09-11T18:56:39.050666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#important functions\n# Function to calculate time spent between two ISO 8601 timestamps\ndef calculate_time_spent(start_timestamp, end_timestamp):\n    start_time = pd.to_datetime(start_timestamp)\n    end_time = pd.to_datetime(end_timestamp)\n    time_spent = end_time - start_time\n    return time_spent\n\ndef fill_empty_cells_with_next_non_empty(df, column_name):\n    \"\"\"\n    Fill empty cells in a DataFrame column with the first non-empty cell found in the next rows.\n\n    Parameters:\n    df (DataFrame): The DataFrame containing the column to be filled.\n    column_name (str): The name of the column to fill.\n\n    Returns:\n    DataFrame: The DataFrame with empty cells filled.\n    \"\"\"\n    # Iterate over the column in reverse order\n    for i in range(len(df) - 1, 0, -1):\n        if pd.isna(df.loc[i, column_name]):\n            # Find the first non-empty cell in the next rows\n            for j in range(i + 1, len(df)):\n                if not pd.isna(df.loc[j, column_name]):\n                    # Fill the empty cell with the first non-empty value found\n                    df.at[i, column_name] = df.loc[j, column_name]\n                    break\n    return df\n","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:38:30.502074Z","iopub.execute_input":"2023-09-11T19:38:30.502543Z","iopub.status.idle":"2023-09-11T19:38:30.520020Z","shell.execute_reply.started":"2023-09-11T19:38:30.502504Z","shell.execute_reply":"2023-09-11T19:38:30.518586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_1 = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\ndf_train_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\ndf_test_1 = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-09-11T18:56:39.053640Z","iopub.execute_input":"2023-09-11T18:56:39.054094Z","iopub.status.idle":"2023-09-11T18:57:31.632120Z","shell.execute_reply.started":"2023-09-11T18:56:39.054065Z","shell.execute_reply":"2023-09-11T18:57:31.630444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-09-11T18:57:31.633808Z","iopub.execute_input":"2023-09-11T18:57:31.634169Z","iopub.status.idle":"2023-09-11T18:57:31.645740Z","shell.execute_reply.started":"2023-09-11T18:57:31.634139Z","shell.execute_reply":"2023-09-11T18:57:31.644423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### train_series.parquet - Series to be used as training data. Each series is a continuous recording of accelerometer data for a single subject spanning many days.\n    - series_id - Unique identifier for each accelerometer series.\n    - step - An integer timestep for each observation within a series.\n    - timestamp - A corresponding datetime with ISO 8601 format %Y-%m-%dT%H:%M:%S%z.\n    - anglez - As calculated and described by the GGIR package, z-angle is a metric derived from individual accelerometer components that is commonly used in sleep detection, and refers to the angle of the arm relative to the vertical axis of the body\n    - enmo - As calculated and described by the GGIR package, ENMO is the Euclidean Norm Minus One of all accelerometer signals, with negative values rounded to zero. While no standard measure of acceleration exists in this space, this is one of the several commonly computed features","metadata":{}},{"cell_type":"code","source":"df_train_1","metadata":{"execution":{"iopub.status.busy":"2023-09-11T18:57:31.647510Z","iopub.execute_input":"2023-09-11T18:57:31.647898Z","iopub.status.idle":"2023-09-11T18:57:31.677477Z","shell.execute_reply.started":"2023-09-11T18:57:31.647867Z","shell.execute_reply":"2023-09-11T18:57:31.676397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### train_events.csv - Sleep logs for series in the training set recording onset and wake events.\n    - series_id - Unique identifier for each series of accelerometer data in train_series.parquet.\n    - night - An enumeration of potential onset / wakeup event pairs. At most one pair of events can occur for each night.\n    - event - The type of event, whether onset or wakeup.\n    - step and timestamp - The recorded time of occurence of the event in the accelerometer series.","metadata":{}},{"cell_type":"code","source":"df_train_events","metadata":{"execution":{"iopub.status.busy":"2023-09-11T18:57:31.680647Z","iopub.execute_input":"2023-09-11T18:57:31.681078Z","iopub.status.idle":"2023-09-11T18:57:31.700613Z","shell.execute_reply.started":"2023-09-11T18:57:31.681044Z","shell.execute_reply":"2023-09-11T18:57:31.699301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_1","metadata":{"execution":{"iopub.status.busy":"2023-09-11T18:57:31.702213Z","iopub.execute_input":"2023-09-11T18:57:31.702705Z","iopub.status.idle":"2023-09-11T18:57:31.724709Z","shell.execute_reply.started":"2023-09-11T18:57:31.702661Z","shell.execute_reply":"2023-09-11T18:57:31.723531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample","metadata":{"execution":{"iopub.status.busy":"2023-09-11T18:57:31.726943Z","iopub.execute_input":"2023-09-11T18:57:31.727376Z","iopub.status.idle":"2023-09-11T18:57:31.744890Z","shell.execute_reply.started":"2023-09-11T18:57:31.727343Z","shell.execute_reply":"2023-09-11T18:57:31.743539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = df_train_1","metadata":{"execution":{"iopub.status.busy":"2023-09-11T18:57:31.746158Z","iopub.execute_input":"2023-09-11T18:57:31.746538Z","iopub.status.idle":"2023-09-11T18:57:31.756646Z","shell.execute_reply.started":"2023-09-11T18:57:31.746506Z","shell.execute_reply":"2023-09-11T18:57:31.755477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1","metadata":{"execution":{"iopub.status.busy":"2023-09-11T18:57:59.863601Z","iopub.execute_input":"2023-09-11T18:57:59.864034Z","iopub.status.idle":"2023-09-11T18:57:59.884680Z","shell.execute_reply.started":"2023-09-11T18:57:59.864001Z","shell.execute_reply":"2023-09-11T18:57:59.883343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Now, I think we should look into each column more","metadata":{}},{"cell_type":"code","source":"df1.groupby('series_id')['series_id'].count()","metadata":{"execution":{"iopub.status.busy":"2023-09-11T18:59:03.222497Z","iopub.execute_input":"2023-09-11T18:59:03.223897Z","iopub.status.idle":"2023-09-11T18:59:30.821206Z","shell.execute_reply.started":"2023-09-11T18:59:03.223842Z","shell.execute_reply":"2023-09-11T18:59:30.819903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfu1 = df1[df1['series_id'] == '038441c925bb']\ndfu1","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:12:10.587103Z","iopub.execute_input":"2023-09-11T19:12:10.587663Z","iopub.status.idle":"2023-09-11T19:12:34.077998Z","shell.execute_reply.started":"2023-09-11T19:12:10.587621Z","shell.execute_reply":"2023-09-11T19:12:34.076828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dft1 = df_test_1","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:04:15.873787Z","iopub.execute_input":"2023-09-11T19:04:15.874179Z","iopub.status.idle":"2023-09-11T19:04:15.879292Z","shell.execute_reply.started":"2023-09-11T19:04:15.874150Z","shell.execute_reply":"2023-09-11T19:04:15.877989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dft1.groupby('series_id')['series_id'].count()","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:04:57.918705Z","iopub.execute_input":"2023-09-11T19:04:57.919130Z","iopub.status.idle":"2023-09-11T19:04:57.929635Z","shell.execute_reply.started":"2023-09-11T19:04:57.919097Z","shell.execute_reply":"2023-09-11T19:04:57.928233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see that the first user in training and test set are the same! Are all three users present in the training set? Let' see.","metadata":{}},{"cell_type":"code","source":"dfu2 = df1[df1['series_id'] == '03d92c9f6f8a']","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:12:34.083105Z","iopub.execute_input":"2023-09-11T19:12:34.083577Z","iopub.status.idle":"2023-09-11T19:12:57.503588Z","shell.execute_reply.started":"2023-09-11T19:12:34.083524Z","shell.execute_reply":"2023-09-11T19:12:57.502334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfu2","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:13:21.102772Z","iopub.execute_input":"2023-09-11T19:13:21.103151Z","iopub.status.idle":"2023-09-11T19:13:21.122177Z","shell.execute_reply.started":"2023-09-11T19:13:21.103116Z","shell.execute_reply":"2023-09-11T19:13:21.120977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfu3 = df1[df1['series_id'] == '0402a003dae9']\ndfu3","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:12:57.505614Z","iopub.execute_input":"2023-09-11T19:12:57.505979Z","iopub.status.idle":"2023-09-11T19:13:21.100730Z","shell.execute_reply.started":"2023-09-11T19:12:57.505939Z","shell.execute_reply":"2023-09-11T19:13:21.099367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Yes they are! \n\n## Let's try to make a user specefic model!","metadata":{}},{"cell_type":"markdown","source":"### For user 1 (id: 038441c925bb)","metadata":{}},{"cell_type":"code","source":"dfu1","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:14:59.576458Z","iopub.execute_input":"2023-09-11T19:14:59.577031Z","iopub.status.idle":"2023-09-11T19:14:59.598396Z","shell.execute_reply.started":"2023-09-11T19:14:59.576990Z","shell.execute_reply":"2023-09-11T19:14:59.596910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfu1events = df_train_events[df_train_events['series_id'] == '038441c925bb']\ndfu1events","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:41:09.066226Z","iopub.execute_input":"2023-09-11T19:41:09.066743Z","iopub.status.idle":"2023-09-11T19:41:09.098952Z","shell.execute_reply.started":"2023-09-11T19:41:09.066702Z","shell.execute_reply":"2023-09-11T19:41:09.097759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfu1events = dfu1events.dropna()\ndfu1events","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:41:53.229486Z","iopub.execute_input":"2023-09-11T19:41:53.230676Z","iopub.status.idle":"2023-09-11T19:41:53.256240Z","shell.execute_reply.started":"2023-09-11T19:41:53.230627Z","shell.execute_reply":"2023-09-11T19:41:53.254907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfu11 = pd.merge(dfu1,dfu1events, how = 'left')\ndfu11","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:27:58.006403Z","iopub.execute_input":"2023-09-11T19:27:58.006860Z","iopub.status.idle":"2023-09-11T19:27:58.441540Z","shell.execute_reply.started":"2023-09-11T19:27:58.006824Z","shell.execute_reply":"2023-09-11T19:27:58.440415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfu11.groupby('night')['night'].count()","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:28:33.834141Z","iopub.execute_input":"2023-09-11T19:28:33.834594Z","iopub.status.idle":"2023-09-11T19:28:33.850544Z","shell.execute_reply.started":"2023-09-11T19:28:33.834544Z","shell.execute_reply":"2023-09-11T19:28:33.849000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Working on event column","metadata":{}},{"cell_type":"code","source":"dfu12 = dfu11.copy()\ndfu12['event'] = dfu12['event'].fillna('awake')\ndfu12","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:30:37.071974Z","iopub.execute_input":"2023-09-11T19:30:37.072440Z","iopub.status.idle":"2023-09-11T19:30:37.187605Z","shell.execute_reply.started":"2023-09-11T19:30:37.072400Z","shell.execute_reply":"2023-09-11T19:30:37.186053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let's fill the night with values before wake up as the user only slept once.","metadata":{}},{"cell_type":"code","source":"dfu13 = dfu12.copy()\ndfu13['night'] = dfu13['night'].fillna(method = 'bfill')\ndfu13['night'] = dfu13['night'].fillna(method = 'ffill')\ndfu13","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:51:26.857876Z","iopub.execute_input":"2023-09-11T19:51:26.858312Z","iopub.status.idle":"2023-09-11T19:51:26.953172Z","shell.execute_reply.started":"2023-09-11T19:51:26.858281Z","shell.execute_reply":"2023-09-11T19:51:26.952026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Working on timestamp","metadata":{}},{"cell_type":"code","source":"dfu14 = dfu13.copy()\ndfu14['timestamp'] = dfu14['timestamp'].apply(lambda x: x.split('T')) # split n T\ndfu14['date'] = dfu14['timestamp'].str.get(0)\ndfu14['time_of_day'] = dfu14['timestamp'].str.get(1)\ndfu14['time_of_day'] = dfu14['time_of_day'].apply(lambda x: x[:2])\ndfu14 = dfu14.drop(['timestamp'], axis = 'columns')\ndfu14","metadata":{"execution":{"iopub.status.busy":"2023-09-11T19:57:22.711192Z","iopub.execute_input":"2023-09-11T19:57:22.711659Z","iopub.status.idle":"2023-09-11T19:57:25.064210Z","shell.execute_reply.started":"2023-09-11T19:57:22.711621Z","shell.execute_reply":"2023-09-11T19:57:25.062846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Change night again based on date","metadata":{}},{"cell_type":"code","source":"dates = dfu14['date'].values\ndates","metadata":{"execution":{"iopub.status.busy":"2023-09-11T20:08:13.069914Z","iopub.execute_input":"2023-09-11T20:08:13.070602Z","iopub.status.idle":"2023-09-11T20:08:13.077084Z","shell.execute_reply.started":"2023-09-11T20:08:13.070548Z","shell.execute_reply":"2023-09-11T20:08:13.075991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-09-11T20:12:43.901757Z","iopub.execute_input":"2023-09-11T20:12:43.902183Z","iopub.status.idle":"2023-09-11T20:12:43.910627Z","shell.execute_reply.started":"2023-09-11T20:12:43.902148Z","shell.execute_reply":"2023-09-11T20:12:43.909261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#a function that inputs df and counts number of days\ndef add_day_column(df):\n    # Convert the \"date\" column to datetime objects\n    df['date'] = pd.to_datetime(df['date'])\n\n    # Create a new column \"day\" based on the rank of unique dates\n    df['night'] = df['date'].rank(method='dense').astype(int)\n    return df\n\n# Example usage:\ndata = {'date': ['2023-09-01', '2023-09-02', '2023-09-01', '2023-09-03', '2023-09-02']}\ndf = pd.DataFrame(data)\ndf = add_day_column(df)\nprint(df)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-11T20:29:58.032448Z","iopub.execute_input":"2023-09-11T20:29:58.032927Z","iopub.status.idle":"2023-09-11T20:29:58.047119Z","shell.execute_reply.started":"2023-09-11T20:29:58.032891Z","shell.execute_reply":"2023-09-11T20:29:58.045867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfu15 = dfu14.copy()\ndfu15 = add_day_column(dfu15)\ndfu15","metadata":{"execution":{"iopub.status.busy":"2023-09-11T20:30:01.623948Z","iopub.execute_input":"2023-09-11T20:30:01.625028Z","iopub.status.idle":"2023-09-11T20:30:01.880167Z","shell.execute_reply.started":"2023-09-11T20:30:01.624973Z","shell.execute_reply":"2023-09-11T20:30:01.878920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfu15.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-09-11T20:33:33.071487Z","iopub.execute_input":"2023-09-11T20:33:33.071986Z","iopub.status.idle":"2023-09-11T20:33:33.207333Z","shell.execute_reply.started":"2023-09-11T20:33:33.071944Z","shell.execute_reply":"2023-09-11T20:33:33.206133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfu15.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-09-11T20:34:32.463160Z","iopub.execute_input":"2023-09-11T20:34:32.463668Z","iopub.status.idle":"2023-09-11T20:34:32.474118Z","shell.execute_reply.started":"2023-09-11T20:34:32.463620Z","shell.execute_reply":"2023-09-11T20:34:32.472663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def numerical_targets(x):\n    if x == 'awake':\n        return 0\n    if x == 'onset':\n        return 1\n    if x == 'wakeup':\n        return 2","metadata":{"execution":{"iopub.status.busy":"2023-09-11T20:40:35.350097Z","iopub.execute_input":"2023-09-11T20:40:35.350642Z","iopub.status.idle":"2023-09-11T20:40:35.357931Z","shell.execute_reply.started":"2023-09-11T20:40:35.350594Z","shell.execute_reply":"2023-09-11T20:40:35.356398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finalizing the input for this user","metadata":{}},{"cell_type":"code","source":"df = dfu15.copy()\ndf['event'] = df['event'].apply(numerical_targets)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:45:56.478079Z","iopub.execute_input":"2023-09-11T21:45:56.479225Z","iopub.status.idle":"2023-09-11T21:45:56.832759Z","shell.execute_reply.started":"2023-09-11T21:45:56.479185Z","shell.execute_reply":"2023-09-11T21:45:56.831537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop(['series_id','date','night'], axis = 'columns')\ndf","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:45:59.457029Z","iopub.execute_input":"2023-09-11T21:45:59.457831Z","iopub.status.idle":"2023-09-11T21:45:59.491650Z","shell.execute_reply.started":"2023-09-11T21:45:59.457777Z","shell.execute_reply":"2023-09-11T21:45:59.490320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#training functions\ndef single_user_process(dfu,dfuevents):\n    dfuevents = dfuevents.dropna()\n    \n    dfu11 = pd.merge(dfu,dfu1events, how = 'left')\n    \n    dfu12 = dfu11.copy()\n    dfu12['event'] = dfu12['event'].fillna('awake')\n    \n    dfu13 = dfu12.copy()\n    dfu13['night'] = dfu13['night'].fillna(method = 'bfill')\n    dfu13['night'] = dfu13['night'].fillna(method = 'ffill')\n    \n    dfu14 = dfu13.copy()\n    dfu14['timestamp'] = dfu14['timestamp'].apply(lambda x: x.split('T')) # split n T\n    dfu14['date'] = dfu14['timestamp'].str.get(0)\n    dfu14['time_of_day'] = dfu14['timestamp'].str.get(1)\n    dfu14['time_of_day'] = dfu14['time_of_day'].apply(lambda x: x[:2])\n    dfu14 = dfu14.drop(['timestamp'], axis = 'columns')\n    \n    #a function that inputs df and counts number of days\n    def add_day_column(df):\n        # Convert the \"date\" column to datetime objects\n        df['date'] = pd.to_datetime(df['date'])\n\n        # Create a new column \"day\" based on the rank of unique dates\n        df['night'] = df['date'].rank(method='dense').astype(int)\n        return df\n\n    dfu15 = dfu14.copy()\n    dfu15 = add_day_column(dfu15)\n    \n    def numerical_targets(x):\n        if x == 'awake':\n            return 0\n        if x == 'onset':\n            return 1\n        if x == 'wakeup':\n            return 2\n    \n    df = dfu15.copy()\n    df['event'] = df['event'].apply(numerical_targets)\n    \n    df = df.drop(['series_id','date','night'], axis = 'columns')\n    \n    return df\n\ndef process_by_user(df_train,df_events,series_id):\n    dfu = df_train[df_train['series_id'] == series_id]\n    dfuevents = df_events[df_events['series_id'] == series_id]\n    \n    prepared_data = single_user_process(dfu,dfuevents)\n    \n    return prepared_data","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:46:27.238769Z","iopub.execute_input":"2023-09-11T21:46:27.239240Z","iopub.status.idle":"2023-09-11T21:46:27.254128Z","shell.execute_reply.started":"2023-09-11T21:46:27.239206Z","shell.execute_reply":"2023-09-11T21:46:27.252974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test function:\ndef single_user_process_t(dfu):\n    #dfuevents = dfuevents.dropna()\n    \n    #dfu11 = pd.merge(dfu,dfuevents, how = 'left')\n    \n    #dfu12 = dfu11.copy()\n    #dfu12['event'] = dfu12['event'].fillna('awake')\n    \n    #dfu13 = dfu12.copy()\n    #dfu13['night'] = dfu13['night'].fillna(method = 'bfill')\n    #dfu13['night'] = dfu13['night'].fillna(method = 'ffill')\n    \n    dfu14 = dfu.copy()\n    dfu14['timestamp'] = dfu14['timestamp'].apply(lambda x: x.split('T')) # split n T\n    dfu14['date'] = dfu14['timestamp'].str.get(0)\n    dfu14['time_of_day'] = dfu14['timestamp'].str.get(1)\n    dfu14['time_of_day'] = dfu14['time_of_day'].apply(lambda x: x[:2])\n    dfu14 = dfu14.drop(['timestamp'], axis = 'columns')\n    \n    #a function that inputs df and counts number of days\n    def add_day_column(df):\n        # Convert the \"date\" column to datetime objects\n        df['date'] = pd.to_datetime(df['date'])\n\n        # Create a new column \"day\" based on the rank of unique dates\n        df['night'] = df['date'].rank(method='dense').astype(int)\n        return df\n\n    dfu15 = dfu14.copy()\n    dfu15 = add_day_column(dfu15)\n    \n    def numerical_targets(x):\n        if x == 'awake':\n            return 0\n        if x == 'onset':\n            return 1\n        if x == 'wakeup':\n            return 2\n    \n    df = dfu15.copy()\n    #df['event'] = df['event'].apply(numerical_targets)\n    \n    df = df.drop(['series_id','date','night'], axis = 'columns')\n    \n    return df\n\ndef process_by_user_t(df, series_id):\n    dfu = df[df['series_id'] == series_id]\n    #dfuevents = df_events[df_events['series_id'] == series_id]\n    \n    prepared_data = single_user_process_t(dfu)\n    \n    return prepared_data","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:46:52.678954Z","iopub.execute_input":"2023-09-11T21:46:52.679410Z","iopub.status.idle":"2023-09-11T21:46:52.692793Z","shell.execute_reply.started":"2023-09-11T21:46:52.679373Z","shell.execute_reply":"2023-09-11T21:46:52.691506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.drop(['event'],axis = 'columns')\ny = df['event']","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:46:59.737601Z","iopub.execute_input":"2023-09-11T21:46:59.738094Z","iopub.status.idle":"2023-09-11T21:46:59.758787Z","shell.execute_reply.started":"2023-09-11T21:46:59.738059Z","shell.execute_reply":"2023-09-11T21:46:59.757654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:47:02.038069Z","iopub.execute_input":"2023-09-11T21:47:02.038519Z","iopub.status.idle":"2023-09-11T21:47:02.057151Z","shell.execute_reply.started":"2023-09-11T21:47:02.038482Z","shell.execute_reply":"2023-09-11T21:47:02.055675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:47:05.918198Z","iopub.execute_input":"2023-09-11T21:47:05.918637Z","iopub.status.idle":"2023-09-11T21:47:05.926796Z","shell.execute_reply.started":"2023-09-11T21:47:05.918603Z","shell.execute_reply":"2023-09-11T21:47:05.925986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.5,random_state=10)","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:53:19.145342Z","iopub.execute_input":"2023-09-11T21:53:19.146310Z","iopub.status.idle":"2023-09-11T21:53:19.233196Z","shell.execute_reply.started":"2023-09-11T21:53:19.146258Z","shell.execute_reply":"2023-09-11T21:53:19.231862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\nrfc = RandomForestClassifier()\n\nrfc.fit(X_train,y_train)\n\nrfc.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:53:21.241776Z","iopub.execute_input":"2023-09-11T21:53:21.242180Z","iopub.status.idle":"2023-09-11T21:53:46.413908Z","shell.execute_reply.started":"2023-09-11T21:53:21.242151Z","shell.execute_reply":"2023-09-11T21:53:46.412830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(rfc.predict(X_test))","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:53:52.517001Z","iopub.execute_input":"2023-09-11T21:53:52.517452Z","iopub.status.idle":"2023-09-11T21:53:54.082083Z","shell.execute_reply.started":"2023-09-11T21:53:52.517416Z","shell.execute_reply":"2023-09-11T21:53:54.080830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dft1.groupby('series_id')['series_id'].count()","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:51:14.705139Z","iopub.execute_input":"2023-09-11T21:51:14.705557Z","iopub.status.idle":"2023-09-11T21:51:14.716167Z","shell.execute_reply.started":"2023-09-11T21:51:14.705524Z","shell.execute_reply":"2023-09-11T21:51:14.715027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:48:17.678661Z","iopub.execute_input":"2023-09-11T21:48:17.679081Z","iopub.status.idle":"2023-09-11T21:48:17.694616Z","shell.execute_reply.started":"2023-09-11T21:48:17.679049Z","shell.execute_reply":"2023-09-11T21:48:17.693280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = process_by_user_t(dft1,'03d92c9f6f8a')\ntest","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:51:30.321506Z","iopub.execute_input":"2023-09-11T21:51:30.321941Z","iopub.status.idle":"2023-09-11T21:51:30.351323Z","shell.execute_reply.started":"2023-09-11T21:51:30.321909Z","shell.execute_reply":"2023-09-11T21:51:30.350121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testl = pd.DataFrame()\n\nfor col in X.columns:\n    testl[col] = test[col]\n","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:43:51.038121Z","iopub.execute_input":"2023-09-11T21:43:51.038552Z","iopub.status.idle":"2023-09-11T21:43:51.049435Z","shell.execute_reply.started":"2023-09-11T21:43:51.038520Z","shell.execute_reply":"2023-09-11T21:43:51.048045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user1 = rfc.predict(test)\nuser1","metadata":{"execution":{"iopub.status.busy":"2023-09-11T21:51:34.833278Z","iopub.execute_input":"2023-09-11T21:51:34.834542Z","iopub.status.idle":"2023-09-11T21:51:34.856018Z","shell.execute_reply.started":"2023-09-11T21:51:34.834494Z","shell.execute_reply":"2023-09-11T21:51:34.854760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}