{"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":"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":"2026-09-16T00:50:55.822344Z","iopub.execute_input":"2026-09-16T00:50:55.822775Z","iopub.status.idle":"2026-09-16T00:50:57.991079Z","shell.execute_reply.started":"2026-09-16T00:50:55.822751Z","shell.execute_reply":"2026-09-16T00:50:57.989862Z"},"trusted":true},"outputs":[],"execution_count":null},{"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>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":"2026-09-16T00:50:57.992796Z","iopub.execute_input":"2026-09-16T00:50:57.993247Z","iopub.status.idle":"2026-09-16T00:50:58.371688Z","shell.execute_reply.started":"2026-09-16T00:50:57.993213Z","shell.execute_reply":"2026-09-16T00:50:58.370754Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_series.head(50000).to_csv('sample_series.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-16T00:50:58.372713Z","iopub.execute_input":"2026-09-16T00:50:58.372945Z","iopub.status.idle":"2026-09-16T00:51:00.30082Z","shell.execute_reply.started":"2026-09-16T00:50:58.372928Z","shell.execute_reply":"2026-09-16T00:51:00.299119Z"}},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:00.302034Z","iopub.execute_input":"2026-09-16T00:51:00.302301Z","iopub.status.idle":"2026-09-16T00:51:00.348999Z","shell.execute_reply.started":"2026-09-16T00:51:00.302266Z","shell.execute_reply":"2026-09-16T00:51:00.347703Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"no_NaN_series = series_has_NaN[~series_has_NaN].index.tolist()","metadata":{"execution":{"iopub.status.busy":"2026-09-16T00:51:00.351601Z","iopub.execute_input":"2026-09-16T00:51:00.351921Z","iopub.status.idle":"2026-09-16T00:51:00.357455Z","shell.execute_reply.started":"2026-09-16T00:51:00.351894Z","shell.execute_reply":"2026-09-16T00:51:00.356322Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:00.359065Z","iopub.execute_input":"2026-09-16T00:51:00.359457Z","iopub.status.idle":"2026-09-16T00:51:00.373468Z","shell.execute_reply.started":"2026-09-16T00:51:00.359428Z","shell.execute_reply":"2026-09-16T00:51:00.37202Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:00.374599Z","iopub.execute_input":"2026-09-16T00:51:00.374909Z","iopub.status.idle":"2026-09-16T00:51:00.669608Z","shell.execute_reply.started":"2026-09-16T00:51:00.374882Z","shell.execute_reply":"2026-09-16T00:51:00.668816Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:00.670811Z","iopub.execute_input":"2026-09-16T00:51:00.671127Z","iopub.status.idle":"2026-09-16T00:51:00.677684Z","shell.execute_reply.started":"2026-09-16T00:51:00.6711Z","shell.execute_reply":"2026-09-16T00:51:00.676521Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:00.679042Z","iopub.execute_input":"2026-09-16T00:51:00.679659Z","iopub.status.idle":"2026-09-16T00:51:07.606763Z","shell.execute_reply.started":"2026-09-16T00:51:00.679601Z","shell.execute_reply":"2026-09-16T00:51:07.605687Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:07.607711Z","iopub.execute_input":"2026-09-16T00:51:07.607916Z","iopub.status.idle":"2026-09-16T00:51:30.020376Z","shell.execute_reply.started":"2026-09-16T00:51:07.607897Z","shell.execute_reply":"2026-09-16T00:51:30.019448Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:30.021802Z","iopub.execute_input":"2026-09-16T00:51:30.022131Z","iopub.status.idle":"2026-09-16T00:51:31.871655Z","shell.execute_reply.started":"2026-09-16T00:51:30.022098Z","shell.execute_reply":"2026-09-16T00:51:31.870721Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:31.87262Z","iopub.execute_input":"2026-09-16T00:51:31.872863Z","iopub.status.idle":"2026-09-16T00:51:34.484486Z","shell.execute_reply.started":"2026-09-16T00:51:31.872841Z","shell.execute_reply":"2026-09-16T00:51:34.483194Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:34.485493Z","iopub.execute_input":"2026-09-16T00:51:34.485734Z","iopub.status.idle":"2026-09-16T00:51:54.304022Z","shell.execute_reply.started":"2026-09-16T00:51:34.485714Z","shell.execute_reply":"2026-09-16T00:51:54.302762Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:54.306999Z","iopub.execute_input":"2026-09-16T00:51:54.307274Z","iopub.status.idle":"2026-09-16T00:51:55.657172Z","shell.execute_reply.started":"2026-09-16T00:51:54.307254Z","shell.execute_reply":"2026-09-16T00:51:55.655943Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:55.658611Z","iopub.execute_input":"2026-09-16T00:51:55.658895Z","iopub.status.idle":"2026-09-16T00:51:58.282043Z","shell.execute_reply.started":"2026-09-16T00:51:55.658872Z","shell.execute_reply":"2026-09-16T00:51:58.280418Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:51:58.283446Z","iopub.execute_input":"2026-09-16T00:51:58.283741Z","iopub.status.idle":"2026-09-16T00:52:02.626981Z","shell.execute_reply.started":"2026-09-16T00:51:58.283715Z","shell.execute_reply":"2026-09-16T00:52:02.625799Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:52:02.628211Z","iopub.execute_input":"2026-09-16T00:52:02.62858Z","iopub.status.idle":"2026-09-16T00:52:09.34632Z","shell.execute_reply.started":"2026-09-16T00:52:02.628548Z","shell.execute_reply":"2026-09-16T00:52:09.345471Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:52:09.347229Z","iopub.execute_input":"2026-09-16T00:52:09.347456Z","iopub.status.idle":"2026-09-16T00:55:31.133901Z","shell.execute_reply.started":"2026-09-16T00:52:09.347436Z","shell.execute_reply":"2026-09-16T00:55:31.132861Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:55:31.135165Z","iopub.execute_input":"2026-09-16T00:55:31.135431Z","iopub.status.idle":"2026-09-16T00:55:31.148981Z","shell.execute_reply.started":"2026-09-16T00:55:31.135407Z","shell.execute_reply":"2026-09-16T00:55:31.148068Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:55:31.15077Z","iopub.execute_input":"2026-09-16T00:55:31.151209Z","iopub.status.idle":"2026-09-16T00:55:31.183591Z","shell.execute_reply.started":"2026-09-16T00:55:31.151179Z","shell.execute_reply":"2026-09-16T00:55:31.18191Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2026-09-16T00:55:31.185024Z","iopub.execute_input":"2026-09-16T00:55:31.185308Z","iopub.status.idle":"2026-09-16T00:55:31.191772Z","shell.execute_reply.started":"2026-09-16T00:55:31.185286Z","shell.execute_reply":"2026-09-16T00:55:31.190442Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2026-09-16T00:55:31.193114Z","iopub.execute_input":"2026-09-16T00:55:31.193451Z","iopub.status.idle":"2026-09-16T00:55:31.211593Z","shell.execute_reply.started":"2026-09-16T00:55:31.193426Z","shell.execute_reply":"2026-09-16T00:55:31.210158Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:55:31.212951Z","iopub.execute_input":"2026-09-16T00:55:31.213301Z","iopub.status.idle":"2026-09-16T00:55:31.276019Z","shell.execute_reply.started":"2026-09-16T00:55:31.213272Z","shell.execute_reply":"2026-09-16T00:55:31.274976Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:55:31.277222Z","iopub.execute_input":"2026-09-16T00:55:31.277575Z","iopub.status.idle":"2026-09-16T00:55:41.826752Z","shell.execute_reply.started":"2026-09-16T00:55:31.277547Z","shell.execute_reply":"2026-09-16T00:55:41.825383Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['timestamp'] = pd.to_datetime(train['timestamp'])\ntrain.set_index('timestamp', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2026-09-16T00:55:41.827729Z","iopub.execute_input":"2026-09-16T00:55:41.827951Z","iopub.status.idle":"2026-09-16T00:55:41.839487Z","shell.execute_reply.started":"2026-09-16T00:55:41.827933Z","shell.execute_reply":"2026-09-16T00:55:41.837941Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Missing Values\ntrain.isnull().sum()\n","metadata":{"execution":{"iopub.status.busy":"2026-09-16T00:55:41.840631Z","iopub.execute_input":"2026-09-16T00:55:41.840879Z","iopub.status.idle":"2026-09-16T00:55:41.872041Z","shell.execute_reply.started":"2026-09-16T00:55:41.84086Z","shell.execute_reply":"2026-09-16T00:55:41.871013Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2026-09-16T00:55:41.873683Z","iopub.execute_input":"2026-09-16T00:55:41.874045Z","iopub.status.idle":"2026-09-16T00:55:52.883292Z","shell.execute_reply.started":"2026-09-16T00:55:41.874014Z","shell.execute_reply":"2026-09-16T00:55:52.882164Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":{}}]}