Elena Morozova














Sign up
How to improve your sleep
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    SleepInc: Helping you find better sleep 😴

    📖 Background

    Your client is SleepInc, a sleep health company that recently launched a sleep-tracking app called SleepScope. The app monitors sleep patterns and collects users' self-reported data on lifestyle habits. SleepInc wants to identify lifestyle, health, and demographic factors that strongly correlate with poor sleep quality. They need your help to produce visualizations and a summary of findings for their next board meeting! They need these to be easily digestible for a non-technical audience!

    💾 The data

    SleepInc has provided you with an anonymized dataset of sleep and lifestyle metrics for 374 individuals. This dataset contains average values for each person calculated over the past six months.

    The dataset includes 13 columns covering sleep duration, quality, disorders, exercise, stress, diet, demographics, and other factors related to sleep health.

    ColumnDescription
    Person IDAn identifier for each individual.
    GenderThe gender of the person (Male/Female).
    AgeThe age of the person in years.
    OccupationThe occupation or profession of the person.
    Sleep Duration (hours)The average number of hours the person sleeps per day.
    Quality of Sleep (scale: 1-10)A subjective rating of the quality of sleep, ranging from 1 to 10.
    Physical Activity Level (minutes/day)The average number of minutes the person engages in physical activity daily.
    Stress Level (scale: 1-10)A subjective rating of the stress level experienced by the person, ranging from 1 to 10.
    BMI CategoryThe BMI category of the person (e.g., Underweight, Normal, Overweight).
    Blood Pressure (systolic/diastolic)The average blood pressure measurement of the person, indicated as systolic pressure over diastolic pressure.
    Heart Rate (bpm)The average resting heart rate of the person in beats per minute.
    Daily StepsThe average number of steps the person takes per day.
    Sleep DisorderThe presence or absence of a sleep disorder in the person (None, Insomnia, Sleep Apnea).

    Acknowledgments: Laksika Tharmalingam, Kaggle: https://www.kaggle.com/datasets/uom190346a/sleep-health-and-lifestyle-dataset (this is a fictitious dataset)

    import pandas as pd
    raw_data = pd.read_csv('sleep_health_data.csv')
    raw_data

    💪 Challenge

    Leverage this sleep data to analyze the relationship between lifestyle, health, demographic factors, and sleep quality. Your goal is to identify factors that correlate with poor sleep health.

    Some examples:

    • Examine relationships between several factors like gender, occupation, physical activity, stress levels, and sleep quality/duration. Create visualizations to present your findings.
    • Produce recommendations on ways people can improve sleep health based on the patterns in the data.
    • Develop an accessible summary of study findings and recommendations for improving sleep health for non-technical audiences.

    ⌛️ Time is ticking. Good luck!

    raw_data.info()
    # import libraries
    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    raw_data.describe()

    Summary

    The portrait of an ideal sleeper:

    • A woman
    • middle-aged (45-59 years old)
    • an engineer
    • normal weight
    • normal or high activity level
    • not stressed
    • with normal heart rate and blood pressure

    The portrait of a bad sleeper:

    • A man
    • adult (27-44 years old)
    • a salesperson
    • overweight or obese
    • low activity level
    • with high stress level
    • with high heart rate and blood pressure

    People with high heart rate and high blood pressure sleep worse.

    • High heart rate is more typical in men, more in adult persons than in middle-aged ones, in the medical professions, in obese individuals and in those with a low activity level.
    • High blood pressure is more typical in women, more in middle-aged persons than in adult ones, in those who work with people, in obese and overweight individuals and in those with a low or very high activity level.

    For a better sleep, look at what you can change of the above!

    Preprocessing

    sf=raw_data.copy()
    
    # Separate Systolic Blood Pressure and create Blood Pressure categories
    sf['Systolic BP']=sf['Blood Pressure'].str.split('/',expand=True)[0].astype('int')
    sf['BP_cat']=pd.cut(sf['Systolic BP'],bins=[0,120,129,139,150],labels=['Normal','Elevated','High','Very High'])
    print('Blood Pressure categories:')
    print(pd.DataFrame(sf['BP_cat'].value_counts()),'\n')
    
    # Create Heart Rate categories
    sf['HR_cat']=pd.cut(sf['Heart Rate'],bins=[0,80,90],labels=['Normal','High'])
    print('Heart Rate categories:')
    print(pd.DataFrame(sf['HR_cat'].value_counts()), '\n')
    
    # Look at gender distribution
    print('Genders:')
    print(pd.DataFrame(sf['Gender'].value_counts()), '\n')
    
    # Correct names of BMI Category and look at the distribution
    sf['BMI Category']=sf['BMI Category'].str.replace('Normal Weight','Normal')
    print('BMI categories:')
    print(pd.DataFrame(sf['BMI Category'].value_counts()),'\n')
    
    # Create Age categories
    sf['Age_cat']=pd.cut(sf['Age'],bins=[0,44,60],labels=['Adult','Middle Age'])
    print('Age categories:')
    print(pd.DataFrame(sf['Age_cat'].value_counts()))

    Sleep patterns

    We have tree columns that tell us about sleep patterns:

    • Sleep Duration
    • Quality of Sleep
    • Sleep Disorder