{"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":"![image](https://i.pinimg.com/originals/05/46/ca/0546caeb51e42b4227d0f91a8b6d3e4f.gif)\n\n[Image Source](https://i.pinimg.com/originals/05/46/ca/0546caeb51e42b4227d0f91a8b6d3e4f.gif)","metadata":{}},{"cell_type":"markdown","source":"# <div style=\"color:blue;display:inline-block;border-radius:5px;background-color:#E6FFE6;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:blue;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b> Import Modules</p></div>","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nfrom scipy.stats import gaussian_kde\n\nimport dask.dataframe as dd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor\nfrom sklearn.neighbors import KNeighborsRegressor\nfrom sklearn.metrics import mean_squared_error\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport math\n\nrc = {\n    \"axes.facecolor\": \"#E6FFE6\",\n    \"figure.facecolor\": \"#E6FFE6\",\n    \"axes.edgecolor\": \"#000000\",\n    \"grid.color\": \"#EBEBE7\",\n    \"font.family\": \"serif\",\n    \"axes.labelcolor\": \"#000000\",\n    \"xtick.color\": \"#000000\",\n    \"ytick.color\": \"#000000\",\n    \"grid.alpha\": 0.4\n}\n\nsns.set(rc=rc)\n\nfrom colorama import Style, Fore\nred = Style.BRIGHT + Fore.RED\nblu = Style.BRIGHT + Fore.BLUE\nmgt = Style.BRIGHT + Fore.MAGENTA\ngld = Style.BRIGHT + Fore.YELLOW\nres = Style.RESET_ALL","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:41:09.583473Z","iopub.execute_input":"2023-09-22T06:41:09.583928Z","iopub.status.idle":"2023-09-22T06:41:12.356094Z","shell.execute_reply.started":"2023-09-22T06:41:09.583893Z","shell.execute_reply":"2023-09-22T06:41:12.354345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:blue;display:inline-block;border-radius:5px;background-color:#E6FFE6;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:blue;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Load the Datasets</p></div>\n","metadata":{}},{"cell_type":"code","source":"# Load datasets\ntrain_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\ntrain_series = dd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:41:17.540119Z","iopub.execute_input":"2023-09-22T06:41:17.540910Z","iopub.status.idle":"2023-09-22T06:41:18.090105Z","shell.execute_reply.started":"2023-09-22T06:41:17.540861Z","shell.execute_reply":"2023-09-22T06:41:18.089106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:blue;display:inline-block;border-radius:5px;background-color:#E6FFE6;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:blue;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Data Cleaning and Preprocessing</p></div>\n\n","metadata":{}},{"cell_type":"code","source":"series_has_NaN = train_events.groupby('series_id')['step'].apply(lambda x: x.isnull().any())\nseries_has_NaN.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:41:21.418939Z","iopub.execute_input":"2023-09-22T06:41:21.419426Z","iopub.status.idle":"2023-09-22T06:41:21.482372Z","shell.execute_reply.started":"2023-09-22T06:41:21.419375Z","shell.execute_reply":"2023-09-22T06:41:21.481257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_NaN_series = series_has_NaN[~series_has_NaN].index.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:41:25.268575Z","iopub.execute_input":"2023-09-22T06:41:25.268953Z","iopub.status.idle":"2023-09-22T06:41:25.274903Z","shell.execute_reply.started":"2023-09-22T06:41:25.268924Z","shell.execute_reply":"2023-09-22T06:41:25.273863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop the two truncated events series:\nno_NaN_series.remove('31011ade7c0a') \nno_NaN_series.remove('a596ad0b82aa')","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:41:28.544489Z","iopub.execute_input":"2023-09-22T06:41:28.544987Z","iopub.status.idle":"2023-09-22T06:41:28.551698Z","shell.execute_reply.started":"2023-09-22T06:41:28.544944Z","shell.execute_reply":"2023-09-22T06:41:28.550459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:blue;display:inline-block;border-radius:5px;background-color:#E6FFE6;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:blue;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Exploratory Data Analysis (EDA)</p></div>\n\n","metadata":{}},{"cell_type":"code","source":"# Visualize distribution of events in train.csv\nplt.figure(figsize=(8, 6))\nsns.countplot(data=train_events, x='event')\nplt.title(\"Distribution of Events in Train Data\", fontsize = 14, fontweight = 'bold', color = 'darkgreen')\nplt.savefig('Distribution of Events in Train Data.png')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:41:32.744274Z","iopub.execute_input":"2023-09-22T06:41:32.744745Z","iopub.status.idle":"2023-09-22T06:41:33.220063Z","shell.execute_reply.started":"2023-09-22T06:41:32.744711Z","shell.execute_reply":"2023-09-22T06:41:33.218690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_series(series):\n    train_series = dd.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet\", filters=[('series_id','=',series)])\n    train_events = pd.read_csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv\").query('series_id == @series')\n    \n    # cleaning etc.\n    train_events = train_events.dropna()\n    train_events[\"step\"]  = train_events[\"step\"].astype(\"int\")\n    train_events[\"awake\"] = train_events[\"event\"].replace({\"onset\":1,\"wakeup\":0})\n\n    train_series = train_series.compute()  # Convert Dask DataFrame to pandas DataFrame\n    \n    train = pd.merge(train_series, train_events[['step','awake']], on='step', how='left')\n    train[\"awake\"] = train[\"awake\"].bfill(axis ='rows')\n    \n    train['awake'] = train['awake'].fillna(1) # awake\n    train[\"awake\"] = train[\"awake\"].astype(\"int\")\n    return train\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:41:38.120706Z","iopub.execute_input":"2023-09-22T06:41:38.121152Z","iopub.status.idle":"2023-09-22T06:41:38.132315Z","shell.execute_reply.started":"2023-09-22T06:41:38.121116Z","shell.execute_reply":"2023-09-22T06:41:38.130772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nfrom matplotlib.ticker import MaxNLocator\n\ndef visualize_awake_state(train):\n    plt.figure(figsize=(20, 3))\n    ax = sns.lineplot(data=train, x=\"step\", y=\"awake\", color='blue')\n    ax.set_title('Awake State Over Time', fontsize = 14, fontweight = 'bold', color = 'darkgreen')\n    ax.set_xlabel('Step', fontsize = 12, fontweight = 'bold', color = 'darkblue')\n    ax.set_ylabel('Awake State', fontsize = 12, fontweight = 'bold', color = 'blue')\n\n    # Customize x-axis ticks for better readability\n    ax.xaxis.set_major_locator(MaxNLocator(integer=True))\n    \n    plt.savefig('Awake State Over Time.png')\n\n    plt.show()\n\n# Assuming you have already loaded your data and have 'train' DataFrame\ntrain = get_train_series('08db4255286f')  # Replace with actual series ID\n\nvisualize_awake_state(train)\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:41:42.387958Z","iopub.execute_input":"2023-09-22T06:41:42.388377Z","iopub.status.idle":"2023-09-22T06:41:56.166374Z","shell.execute_reply.started":"2023-09-22T06:41:42.388345Z","shell.execute_reply":"2023-09-22T06:41:56.165168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_anglez_enmo(train):\n    plt.figure(figsize=(20, 3))\n    sns.scatterplot(data=train, x='anglez', y='enmo', hue='awake', palette={0: 'red', 1: 'blue'})\n    plt.title('anglez vs. enmo with Awake State', fontsize = 14, fontweight = 'bold', color = 'darkgreen')\n    plt.savefig('anglez vs. enmo with Awake State.png')\n    plt.show()\n\ntrain = get_train_series('08db4255286f')  # Replace with actual series ID\n\nvisualize_anglez_enmo(train)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:42:00.752736Z","iopub.execute_input":"2023-09-22T06:42:00.753137Z","iopub.status.idle":"2023-09-22T06:42:46.708496Z","shell.execute_reply.started":"2023-09-22T06:42:00.753104Z","shell.execute_reply":"2023-09-22T06:42:46.707508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(20, 3))\nsns.lineplot(data=train, x=\"step\", y=\"enmo\",hue=\"awake\", linewidth = 0.5)\nplt.savefig('step-enmo.png')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:42:51.733773Z","iopub.execute_input":"2023-09-22T06:42:51.734212Z","iopub.status.idle":"2023-09-22T06:42:55.581137Z","shell.execute_reply.started":"2023-09-22T06:42:51.734178Z","shell.execute_reply":"2023-09-22T06:42:55.579444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(20, 3))\nsns.lineplot(data=train, x=\"step\", y=\"anglez\",hue=\"awake\", linewidth = 0.5)\nplt.savefig('step-anglez.png')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:42:59.436795Z","iopub.execute_input":"2023-09-22T06:42:59.437189Z","iopub.status.idle":"2023-09-22T06:43:04.656436Z","shell.execute_reply.started":"2023-09-22T06:42:59.437150Z","shell.execute_reply":"2023-09-22T06:43:04.654893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_anglez_enmo(train):\n    plt.figure(figsize=(20, 3))\n    sns.scatterplot(data=train, x='anglez', y='enmo', hue='awake', palette={0: 'red', 1: 'blue'})\n    plt.title('anglez vs. enmo with Awake State', fontsize = 14, fontweight = 'bold', color = 'darkgreen')\n    plt.savefig('anglez vs. enmo with Awake State-67f5fc60e494.png')\n    plt.show()\n\ntrain = get_train_series('67f5fc60e494')  # Replace with actual series ID\n\nvisualize_anglez_enmo(train)\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:43:09.023378Z","iopub.execute_input":"2023-09-22T06:43:09.023915Z","iopub.status.idle":"2023-09-22T06:43:49.533842Z","shell.execute_reply.started":"2023-09-22T06:43:09.023875Z","shell.execute_reply":"2023-09-22T06:43:49.532516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(20, 3))\nsns.lineplot(data=train, x=\"step\", y=\"enmo\",hue=\"awake\", linewidth = 0.5)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:43:53.187374Z","iopub.execute_input":"2023-09-22T06:43:53.187976Z","iopub.status.idle":"2023-09-22T06:43:56.095733Z","shell.execute_reply.started":"2023-09-22T06:43:53.187930Z","shell.execute_reply":"2023-09-22T06:43:56.094345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(20, 3))\nsns.lineplot(data=train, x=\"step\", y=\"anglez\",hue=\"awake\", linewidth = 0.5)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:44:00.624651Z","iopub.execute_input":"2023-09-22T06:44:00.625140Z","iopub.status.idle":"2023-09-22T06:44:05.303494Z","shell.execute_reply.started":"2023-09-22T06:44:00.625099Z","shell.execute_reply":"2023-09-22T06:44:05.302657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef hexbin_density_plot(train):\n    plt.figure(figsize=(10, 6))\n    hb = plt.hexbin(train['anglez'], train['enmo'], gridsize=50, cmap='inferno', mincnt=1)\n    plt.colorbar(label='Density')\n    plt.xlabel('anglez')\n    plt.ylabel('enmo')\n    plt.title('Hexbin Density Plot for anglez and enmo', fontsize = 14, fontweight = 'bold', color = 'darkgreen')\n    plt.savefig('Hexbin Density Plot for anglez and enmo.png')\n    plt.show()\n\ntrain = get_train_series('67f5fc60e494')  # Replace with actual series ID\n\nhexbin_density_plot(train)\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:44:11.561352Z","iopub.execute_input":"2023-09-22T06:44:11.561831Z","iopub.status.idle":"2023-09-22T06:44:20.255424Z","shell.execute_reply.started":"2023-09-22T06:44:11.561794Z","shell.execute_reply":"2023-09-22T06:44:20.254096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def polar_plot(train):\n    # Convert anglez and enmo to radians\n    anglez_rad = np.deg2rad(train['anglez'])\n    enmo = train['enmo']\n    \n    # Create polar plot\n    fig = plt.figure(figsize=(8, 8))\n    ax = fig.add_subplot(111, polar=True)\n    \n    # Plot the data\n    ax.plot(anglez_rad, enmo, 'o', color='blue')\n    ax.set_rmax(max(enmo) + 1)  # Set maximum radius\n    \n    plt.title('Polar Plot of anglez vs. enmo', fontsize = 14, fontweight = 'bold', color = 'darkgreen')\n    plt.savefig('Polar Plot of anglez and enmo.png')\n    plt.show()\n\ntrain = get_train_series('67f5fc60e494')  # Replace with actual series ID\n\npolar_plot(train)\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-22T06:44:24.698028Z","iopub.execute_input":"2023-09-22T06:44:24.698512Z","iopub.status.idle":"2023-09-22T06:44:36.089626Z","shell.execute_reply.started":"2023-09-22T06:44:24.698464Z","shell.execute_reply":"2023-09-22T06:44:36.088094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:blue;display:inline-block;border-radius:5px;background-color:#E6FFE6;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:blue;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Build Model and Prediction</p></div>\n","metadata":{}},{"cell_type":"code","source":"# Data Preprocessing\ntrain['timestamp'] = pd.to_datetime(train['timestamp'], utc=True)  # Convert to UTC datetime\ntrain['timestamp'] = train['timestamp'].astype(int) // 10**9  # Convert to Unix timestamp\nX = train[['step', 'timestamp', 'anglez', 'enmo']]\ny = train['awake']\n\n# Train-Test Split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Step 3: Model Training and Evaluation\nmodels = {\n    'Linear Regression': LinearRegression(),\n    'Random Forest': RandomForestRegressor(),\n    'Gradient Boosting': GradientBoostingRegressor(),\n    'K-Nearest Neighbors (KNN)': KNeighborsRegressor()\n}\n\nbest_model = None\nbest_rmse = float('inf')\n\nfor name, model in models.items():\n    model.fit(X_train, y_train)\n    y_pred = model.predict(X_test)\n    rmse = mean_squared_error(y_test, y_pred, squared=False)\n    print(f'{name} RMSE: {rmse}')\n    \n    if rmse < best_rmse:\n        best_rmse = rmse\n        best_model = model\n","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:44:42.706689Z","iopub.execute_input":"2023-09-22T06:44:42.707143Z","iopub.status.idle":"2023-09-22T06:48:35.292736Z","shell.execute_reply.started":"2023-09-22T06:44:42.707107Z","shell.execute_reply":"2023-09-22T06:48:35.291040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example new data\nnew_data = pd.DataFrame({\n    'step': [5, 6, 7],\n    'timestamp': ['2018-11-05T10:00:25-0400', '2018-11-05T10:00:30-0400', '2018-11-05T10:00:35-0400'],\n    'anglez': [-34.0, -35.2, -36.1],\n    'enmo': [0.075, 0.088, 0.095]\n})\n\n# Preprocess the timestamp\nnew_data['timestamp'] = pd.to_datetime(new_data['timestamp']).astype(int) // 10**9\n\n# Use the best model for prediction\nprediction = best_model.predict(new_data)\n\nprint(\"Predicted 'awake' values for the new data:\")\nprint(prediction)","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:48:41.241765Z","iopub.execute_input":"2023-09-22T06:48:41.242247Z","iopub.status.idle":"2023-09-22T06:48:41.263080Z","shell.execute_reply.started":"2023-09-22T06:48:41.242204Z","shell.execute_reply":"2023-09-22T06:48:41.261571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:blue;display:inline-block;border-radius:5px;background-color:#E6FFE6;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:blue;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Submission</p></div>\n","metadata":{}},{"cell_type":"code","source":"submission_df = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/sample_submission.csv')\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:48:46.131754Z","iopub.execute_input":"2023-09-22T06:48:46.132145Z","iopub.status.idle":"2023-09-22T06:48:46.159417Z","shell.execute_reply.started":"2023-09-22T06:48:46.132116Z","shell.execute_reply":"2023-09-22T06:48:46.157830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:48:50.772276Z","iopub.execute_input":"2023-09-22T06:48:50.772901Z","iopub.status.idle":"2023-09-22T06:48:50.782174Z","shell.execute_reply.started":"2023-09-22T06:48:50.772855Z","shell.execute_reply":"2023-09-22T06:48:50.781098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:49:22.487520Z","iopub.execute_input":"2023-09-22T06:49:22.488020Z","iopub.status.idle":"2023-09-22T06:49:22.504024Z","shell.execute_reply.started":"2023-09-22T06:49:22.487981Z","shell.execute_reply":"2023-09-22T06:49:22.502577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert 'timestamp' to datetime with timezone\n#train['timestamp'] = pd.to_datetime(train['timestamp'], format='%Y-%m-%dT%H:%M:%S%z', utc=True)\ntrain['timestamp'] = pd.to_datetime(train['timestamp'], unit='s', utc=True)\n\n# Calculate sleep duration, sleep onset, and waking up time\ntrain['sleep_duration'] = train['timestamp'].diff().dt.total_seconds() / 3600  # in hours\ntrain['sleep_onset'] = train['timestamp'].dt.hour + train['timestamp'].dt.minute / 60\ntrain['waking_up_time'] = train['sleep_onset'] + train['sleep_duration']","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:51:43.501480Z","iopub.execute_input":"2023-09-22T06:51:43.502071Z","iopub.status.idle":"2023-09-22T06:51:43.653355Z","shell.execute_reply.started":"2023-09-22T06:51:43.502025Z","shell.execute_reply":"2023-09-22T06:51:43.651919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the variables for density estimation\nvariables = ['sleep_duration', 'sleep_onset', 'waking_up_time']\npercentiles = [5, 25, 75, 95]\n\n# Generate density estimates\ndef generate_density(data):\n    kde = gaussian_kde(data)\n    x_vals = np.linspace(data.min(), data.max(), 1000)\n    density_values = kde(x_vals)\n    return x_vals, density_values\n\n# Plot the density estimates\ndef plot_density(x_vals, density_values, percentiles, title):\n    plt.figure(figsize=(8, 6))\n    plt.plot(x_vals, density_values, label='Density Estimate')\n    for p in percentiles:\n        val = np.percentile(x_vals, p)\n        plt.plot([val, val], [0, max(density_values)], label=f'{p}th percentile', linestyle='--')\n        plt.plot(val, 0, 'ro')\n    plt.title(title)\n    plt.legend()\n    plt.show()\n\nfor var in variables:\n    data = train[var].dropna()\n    x_vals, density_values = generate_density(data)\n    plot_density(x_vals, density_values, percentiles, f'Density Estimate of {var}')","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:51:49.991197Z","iopub.execute_input":"2023-09-22T06:51:49.991595Z","iopub.status.idle":"2023-09-22T06:52:06.884154Z","shell.execute_reply.started":"2023-09-22T06:51:49.991566Z","shell.execute_reply":"2023-09-22T06:52:06.882416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['timestamp'] = pd.to_datetime(train['timestamp'])\ntrain.set_index('timestamp', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:52:14.604571Z","iopub.execute_input":"2023-09-22T06:52:14.605008Z","iopub.status.idle":"2023-09-22T06:52:14.629893Z","shell.execute_reply.started":"2023-09-22T06:52:14.604974Z","shell.execute_reply":"2023-09-22T06:52:14.628519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Missing Values\ntrain.isnull().sum()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:52:17.429808Z","iopub.execute_input":"2023-09-22T06:52:17.430287Z","iopub.status.idle":"2023-09-22T06:52:17.490939Z","shell.execute_reply.started":"2023-09-22T06:52:17.430250Z","shell.execute_reply":"2023-09-22T06:52:17.489140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Distribution of 'awake'\nsns.countplot(x='awake', data=train)\nplt.title('Distribution of Awake', fontsize = 15, fontweight = 'bold', color = 'darkblue')\nplt.show()\n\n# Numeric Variables\nplt.figure(figsize=(10, 4))\nplt.subplot(1, 2, 1)\nsns.histplot(train['anglez'], kde=True)\nplt.title('Distribution of anglez', fontsize = 15, fontweight = 'bold', color = 'darkblue')\n\nplt.subplot(1, 2, 2)\nsns.histplot(train['enmo'], kde=True)\nplt.title('Distribution of enmo', fontsize = 15, fontweight = 'bold', color = 'darkblue')\nplt.tight_layout()\nplt.show()\n\n# Sleep Metrics\nsns.boxplot(data=train[['sleep_duration', 'sleep_onset', 'waking_up_time']], orient='h')\nplt.title('Sleep Metrics', fontsize = 15, fontweight = 'bold', color = 'darkblue')\nplt.show()\n\n# Time Information\ntrain['hour'] = train.index.hour\nsns.countplot(x='hour', data=train)\nplt.title('Hourly Distribution', fontsize = 15, fontweight = 'bold', color = 'darkblue')\nplt.show()\n\n# Correlation\ncorrelation = train[['anglez', 'enmo']].corr()\nsns.heatmap(correlation, annot=True, cmap='coolwarm')\nplt.title('Correlation Heatmap', fontsize = 15, fontweight = 'bold', color = 'darkblue')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-22T06:52:22.781929Z","iopub.execute_input":"2023-09-22T06:52:22.782345Z","iopub.status.idle":"2023-09-22T06:52:41.823417Z","shell.execute_reply.started":"2023-09-22T06:52:22.782311Z","shell.execute_reply":"2023-09-22T06:52:41.822202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\">\"Your positive feedback and upvote mean a lot! It motivates me to create more valuable content and helps others discover it too. Let's build a thriving community of knowledge-sharing. Thank you for your support! 😊\"</div>","metadata":{}}]}