{"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 pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np\nfrom IPython.display import Markdown\nimport gc","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:35:49.283484Z","iopub.execute_input":"2023-09-12T06:35:49.284169Z","iopub.status.idle":"2023-09-12T06:35:49.293061Z","shell.execute_reply.started":"2023-09-12T06:35:49.284124Z","shell.execute_reply":"2023-09-12T06:35:49.291322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This dataset consists of 269 out of 277 series_id in the original data along with binary target \"is_sleeping\"\ntrain_series = pd.read_parquet(\"/kaggle/input/cmi-cleaned-training-data/CMI_cleaned_training_data.parquet\")\n\n# Extract year, date, month and hour from the timestamp column\ntrain_series['year'] = train_series['timestamp'].dt.year\ntrain_series['month'] = train_series['timestamp'].dt.month\ntrain_series['day'] = train_series['timestamp'].dt.day\ntrain_series['hour'] = train_series['timestamp'].dt.hour","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:15:37.354010Z","iopub.execute_input":"2023-09-12T06:15:37.354541Z","iopub.status.idle":"2023-09-12T06:16:26.545370Z","shell.execute_reply.started":"2023-09-12T06:15:37.354506Z","shell.execute_reply":"2023-09-12T06:16:26.544303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Count of unique series_id","metadata":{}},{"cell_type":"code","source":"series_ids = train_series['series_id'].unique().tolist()\nlen(series_ids)","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:02:06.488586Z","iopub.execute_input":"2023-09-12T06:02:06.489275Z","iopub.status.idle":"2023-09-12T06:02:13.714503Z","shell.execute_reply.started":"2023-09-12T06:02:06.489234Z","shell.execute_reply":"2023-09-12T06:02:13.713550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Step count by series_id ","metadata":{}},{"cell_type":"code","source":"with pd.option_context('display.max_rows', None, 'display.max_columns', None):\n    print(train_series.groupby('series_id')['step'].count())","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:02:29.923756Z","iopub.execute_input":"2023-09-12T06:02:29.924243Z","iopub.status.idle":"2023-09-12T06:02:39.142196Z","shell.execute_reply.started":"2023-09-12T06:02:29.924208Z","shell.execute_reply":"2023-09-12T06:02:39.141360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###  Anglez distribution for sleep v/s awake instances","metadata":{}},{"cell_type":"code","source":"plot1 = sns.kdeplot(train_series, x = 'anglez', hue = 'is_sleeping');\nplot1.set(yticklabels=[]); \nplot1.set(ylabel=None);\nplot1.set(title='anglez distribution for sleeping and awake instances');","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:41:17.542082Z","iopub.execute_input":"2023-09-12T06:41:17.542500Z","iopub.status.idle":"2023-09-12T06:47:22.279829Z","shell.execute_reply.started":"2023-09-12T06:41:17.542470Z","shell.execute_reply":"2023-09-12T06:47:22.278368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###  enmo distribution for sleep v/s awake instances","metadata":{}},{"cell_type":"code","source":"plot2 = sns.kdeplot(train_series, x = 'enmo', hue = 'is_sleeping');\nplot2.set(yticklabels=[]);  \nplot2.set(ylabel=None);  \nplot2.set(title='enmo distribution for sleeping and awake instances');  ","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:47:22.282045Z","iopub.execute_input":"2023-09-12T06:47:22.282516Z","iopub.status.idle":"2023-09-12T06:52:55.576067Z","shell.execute_reply.started":"2023-09-12T06:47:22.282481Z","shell.execute_reply":"2023-09-12T06:52:55.574773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Sleep v/s awake instances by year","metadata":{}},{"cell_type":"code","source":"# Function to generate bar plots for binary target\n\ndef plot_bar(width = 0.1, figsize = (15,5) , rotation = 0, **kwargs):\n \n    N = len(kwargs['x'])\n    ind = np.arange(N)\n \n    # Figure size\n    if 'figsize' in kwargs.keys():\n        plt.figure(figsize = kwargs['figsize'])\n    else:\n        plt.figure(figsize = figsize)\n \n    # Width of a bar\n    if 'width' in kwargs.keys():\n        width = kwargs['width'] \n        \n    if 'rotation' in kwargs.keys():\n        rotation = kwargs['rotation']\n       \n        \n    # Plotting\n    for i in range(len(kwargs['y'])):\n        plt.bar(ind + i* width, kwargs[kwargs['y'][i]] , width, label= kwargs['y'][i])\n \n    if 'xlabel' in kwargs.keys():\n        plt.xlabel(kwargs['xlabel'])\n\n    if 'ylabel' in kwargs.keys():\n        plt.ylabel(kwargs['ylabel'])\n   \n    if 'title' in kwargs.keys():\n        plt.title(kwargs['title'])\n \n    plt.xticks(ind + (len(kwargs['y'])* width) / 2, kwargs['x'], rotation = rotation)\n    plt.legend(loc='best')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:03:01.866912Z","iopub.execute_input":"2023-09-12T06:03:01.867300Z","iopub.status.idle":"2023-09-12T06:03:01.880312Z","shell.execute_reply.started":"2023-09-12T06:03:01.867270Z","shell.execute_reply":"2023-09-12T06:03:01.878494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"years = ['2017', '2018', '2019']\nsleeping = train_series.loc[train_series['is_sleeping']==1, 'year'].value_counts().sort_index()\nawake = train_series.loc[train_series['is_sleeping']==0, 'year'].value_counts().sort_index()\n\nparams = {'figsize' : (15,5), 'rotation': 0, 'width' : 0.2,\n      'x': years,  'y': ['sleeping','awake'],\n      'sleeping': sleeping, 'awake': awake,\n      'xlabel' : 'Years',  'ylabel' : 'Count', \n      'title': 'Comparison of sleeping vs awake instances by year'}\n \nplot_bar(**params)","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:03:03.958996Z","iopub.execute_input":"2023-09-12T06:03:03.959481Z","iopub.status.idle":"2023-09-12T06:03:06.600354Z","shell.execute_reply.started":"2023-09-12T06:03:03.959445Z","shell.execute_reply":"2023-09-12T06:03:06.598945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Sleep v/s awake instances by month","metadata":{}},{"cell_type":"code","source":"months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']\nsleeping = train_series.loc[train_series['is_sleeping']==1, 'month'].value_counts().sort_index()\nawake = train_series.loc[train_series['is_sleeping']==0, 'month'].value_counts().sort_index()\n\nparams = {'figsize' : (15,5), 'rotation': 30, 'width' : 0.2,\n      'x': months,  'y': ['sleeping','awake'],\n      'sleeping': sleeping, 'awake': awake,\n      'xlabel' : 'Months',  'ylabel' : 'Count in millions', \n      'title': 'Comparison of sleeping vs awake instances by month'}\n \nplot_bar(**params)","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:03:09.402090Z","iopub.execute_input":"2023-09-12T06:03:09.402527Z","iopub.status.idle":"2023-09-12T06:03:11.778135Z","shell.execute_reply.started":"2023-09-12T06:03:09.402494Z","shell.execute_reply":"2023-09-12T06:03:11.776780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Sleep v/s awake instances by days","metadata":{}},{"cell_type":"code","source":"days = [i for i in range(1, 32)]\nsleeping = train_series.loc[train_series['is_sleeping']==1, 'day'].value_counts().sort_index()\nawake = train_series.loc[train_series['is_sleeping']==0, 'day'].value_counts().sort_index()\n\nparams = {'figsize' : (15,5), 'rotation': 30, 'width' : 0.2,\n      'x': days,  'y': ['sleeping','awake'],\n      'sleeping': sleeping, 'awake': awake,\n      'xlabel' : 'Days',  'ylabel' : 'Count in millions', \n      'title': 'Comparison of sleeping vs awake instances by day'}\n \nplot_bar(**params)","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:03:13.223618Z","iopub.execute_input":"2023-09-12T06:03:13.224077Z","iopub.status.idle":"2023-09-12T06:03:15.782526Z","shell.execute_reply.started":"2023-09-12T06:03:13.224041Z","shell.execute_reply":"2023-09-12T06:03:15.781109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Sleep v/s awake instances by hours","metadata":{}},{"cell_type":"code","source":"hours = [i for i in range(24)]\nsleeping = train_series.loc[train_series['is_sleeping']==1, 'hour'].value_counts().sort_index()\nawake = train_series.loc[train_series['is_sleeping']==0, 'hour'].value_counts().sort_index()\n\nparams = {'figsize' : (15,5), 'rotation': 0, 'width' : 0.2,\n      'x': hours,  'y': ['sleeping','awake'],\n      'sleeping': sleeping, 'awake': awake,\n      'xlabel' : 'Hours',  'ylabel' : 'Count in millions', \n      'title': 'Comparison of sleeping vs awake instances by hours in a day'}\n \nplot_bar(**params)","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:03:15.784983Z","iopub.execute_input":"2023-09-12T06:03:15.785462Z","iopub.status.idle":"2023-09-12T06:03:18.265271Z","shell.execute_reply.started":"2023-09-12T06:03:15.785418Z","shell.execute_reply":"2023-09-12T06:03:18.264178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Scatterplot of anglez vs enmo for sleeping vs awake instances by hours","metadata":{}},{"cell_type":"code","source":"def plot_scatter(data, xlim, ylim, xlabel, ylabel, loc):\n    \n    plt.scatter(data.loc[data['is_sleeping'] == 0, 'anglez'], data.loc[data['is_sleeping'] == 0, 'enmo'], label = 'awake', s=0.2)\n    plt.scatter(data.loc[data['is_sleeping'] == 1, 'anglez'], data.loc[data['is_sleeping'] == 1, 'enmo'], label = 'sleeping', s=0.2)\n\n    plt.legend(loc = loc)\n    plt.xlim(xlim)\n    plt.ylim(ylim)\n    plt.xlabel(xlabel)\n    plt.ylabel(ylabel)\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:39:45.182838Z","iopub.execute_input":"2023-09-12T06:39:45.183599Z","iopub.status.idle":"2023-09-12T06:39:45.192057Z","shell.execute_reply.started":"2023-09-12T06:39:45.183560Z","shell.execute_reply":"2023-09-12T06:39:45.191035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_training_data_by_hour(hour, data):\n    return data[data['hour'] == hour]\n\nhours = [i for i in range(24)]\n\nfor hour in hours:\n    \n    print(f'Hour = {hour} \\n')\n    train = get_training_data_by_hour(hour, train_series)\n\n    plot_scatter(data = train,\n                 xlim = [-90,90],\n                 ylim = [0,1],\n                 xlabel = 'anglez',\n                 ylabel = 'enmo',\n                 loc = 'upper right')\n    \n    print(f'\\n')\n    del train\n    gc.collect();","metadata":{"execution":{"iopub.status.busy":"2023-09-12T06:39:45.878766Z","iopub.execute_input":"2023-09-12T06:39:45.879215Z","iopub.status.idle":"2023-09-12T06:40:48.287187Z","shell.execute_reply.started":"2023-09-12T06:39:45.879184Z","shell.execute_reply":"2023-09-12T06:40:48.286329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plots of 269 anglez and enmo series as a function of step","metadata":{}},{"cell_type":"code","source":"# This code is taken from Carl's notebook - https://www.kaggle.com/code/carlmcbrideellis/zzzs-plots-of-all-277-anglez-and-enmo-series?scriptVersionId=142091431\n\ndef get_training_data_by_series(series, train_series):\n    train = train_series[train_series['series_id'] == series]\n    return train\n\nfor series_id in series_ids:\n    train = get_training_data_by_series(series_id, train_series)\n    display(Markdown('###  anglez for series ' + series_id))\n    fig, ax = plt.subplots(figsize=(20, 3))\n    sns.lineplot(data=train, x=\"step\", y=\"anglez\",hue=\"is_sleeping\", linewidth = 0.5)\n    plt.show();\n    \n    display(Markdown('###  enmo for series ' + series_id))\n    fig, ax = plt.subplots(figsize=(20, 3))\n    sns.lineplot(data=train, x=\"step\", y=\"enmo\",hue=\"is_sleeping\", linewidth = 0.5)\n    plt.show();\n    \n    del train\n    gc.collect();","metadata":{"execution":{"iopub.status.busy":"2023-09-11T18:47:13.998607Z","iopub.execute_input":"2023-09-11T18:47:13.999022Z","iopub.status.idle":"2023-09-11T18:53:03.335170Z","shell.execute_reply.started":"2023-09-11T18:47:13.998989Z","shell.execute_reply":"2023-09-11T18:53:03.333188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Please upvote if you find this notebook useful","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"}}