Skip to content

Sleep Health and Lifestyle

This synthetic dataset contains sleep and cardiovascular metrics as well as lifestyle factors of close to 400 fictive persons.

The workspace is set up with one CSV file, data.csv, with the following columns:

  • Person ID
  • Gender
  • Age
  • Occupation
  • Sleep Duration: Average number of hours of sleep per day
  • Quality of Sleep: A subjective rating on a 1-10 scale
  • Physical Activity Level: Average number of minutes the person engages in physical activity daily
  • Stress Level: A subjective rating on a 1-10 scale
  • BMI Category
  • Blood Pressure: Indicated as systolic pressure over diastolic pressure
  • Heart Rate: In beats per minute
  • Daily Steps
  • Sleep Disorder: One of None, Insomnia or Sleep Apnea

Check out the guiding questions or the scenario described below to get started with this dataset! Feel free to make this workspace yours by adding and removing cells, or editing any of the existing cells.

Source: Kaggle

🌎 Some guiding questions to help you explore this data:

  1. Which factors could contribute to a sleep disorder?
  2. Does an increased physical activity level result in a better quality of sleep?
  3. Does the presence of a sleep disorder affect the subjective sleep quality metric?

📊 Visualization ideas

  • Boxplot: show the distribution of sleep duration or quality of sleep for each occupation.
  • Show the link between age and sleep duration with a scatterplot. Consider including information on the sleep disorder.

🔍 Scenario: Automatically identify potential sleep disorders

This scenario helps you develop an end-to-end project for your portfolio.

Background: You work for a health insurance company and are tasked to identify whether or not a potential client is likely to have a sleep disorder. The company wants to use this information to determine the premium they want the client to pay.

Objective: Construct a classifier to predict the presence of a sleep disorder based on the other columns in the dataset.

Check out our Linear Classifiers course (Python) or Supervised Learning course (R) for a quick introduction to building classifiers.

You can query the pre-loaded CSV files using SQL directly. Here’s a sample query:

Spinner
DataFrameas
df
variable
SELECT *
FROM 'data.csv'
LIMIT 10
import pandas as pd

sleep_data = pd.read_csv('data.csv')
sleep_data.head()
  1. Which factors could contribute to a sleep disorder?
  • Blood Pressure
  • Age
  • BMI Category
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import re

# Read the CSV file into a pandas dataframe
data = pd.read_csv("data.csv")

# Assign a unique number to each 'Occupation' in data
occupation_mapping = {occupation: index for index, occupation in enumerate(data['Occupation'].unique())}
data['Occupation'] = data['Occupation'].map(occupation_mapping)

# Assign a unique number to each 'BMI Category' in data
bmi_category_mapping = {category: index for index, category in enumerate(data['BMI Category'].unique())}
data['BMI Category'] = data['BMI Category'].map(bmi_category_mapping)

# Assign females a value of 2 and males a value of 1 in the gender column
data['Gender'] = data['Gender'].map({'Female': 2, 'Male': 1})

# Split the 'Blood Pressure' column into two columns based on '/'
data[['BloodPressure1', 'BloodPressure2']] = data['Blood Pressure'].str.split(r'/', expand=True)
# Remove leading/trailing whitespaces from the new columns
data['BloodPressure1'] = data['BloodPressure1'].str.strip()
data['BloodPressure2'] = data['BloodPressure2'].str.strip()

# Remove the 'Blood Pressure' column from data
data = data.drop('Blood Pressure', axis=1)

# Update the 'Sleep Disorder' column in data
data['Sleep Disorder'] = data['Sleep Disorder'].apply(lambda x: 0 if pd.isna(x) else 1)

# Convert columns with non-numeric values to numeric format
for column in data.columns:
    try:
        data[column] = pd.to_numeric(data[column], errors='coerce')
    except ValueError:
        continue

# Calculate the correlation matrix
correlation_matrix = data.corr()

# Sort the correlation matrix by the 'Sleep Disorder' column in descending order
sorted_correlation_matrix = correlation_matrix['Sleep Disorder'].sort_values(ascending=False)

# Plot the sorted correlation matrix as a heatmap
sns.heatmap(correlation_matrix.loc[sorted_correlation_matrix.index, sorted_correlation_matrix.index], annot=True, cmap='coolwarm')
plt.show()
  1. Does an increased physical activity level result in a better quality of sleep?
  1. Does the presence of a sleep disorder affect the subjective sleep quality metric?
  1. Boxplot: show the distribution of sleep duration or quality of sleep for each occupation.
  1. Show the link between age and sleep duration with a scatterplot. Consider including information on the sleep disorder.

Objective: Construct a classifier to predict the presence of a sleep disorder based on the other columns in the dataset.