Sleep Health Analysis
Sleep Health Analysis
DESCRIPTION
This Python code utilizes the pandas and
matplotlib libraries to conduct a comprehensive
analysis of sleep health and lifestyle variables. It
introduces a user-friendly menu system for easy
exploration of diverse insights. The analysis
includes compelling visualizations like pie charts
highlighting occupation groups affected by sleep
deprivation, line charts visualizing age groups
experiencing good sleep quality, and bar charts
presenting the most active occupation and age
groups. Additionally, there are informative
stacked bar charts showcasing the correlation
between sleep disorders and BMI categories,
histograms capturing age distribution, and dual-
line graphs contrasting stress and physical
activity levels across different ages. The script
further identifies the prevailing sleep disorder
and compares physical activity to daily steps
through histograms, as well as BMI categories
against physical activity levels using stacked bar
charts. Collectively, these visualizations provide
a comprehensive understanding of sleep
patterns and lifestyle influences within the
dataset, making it an engaging and informative
project.
CSV FILE
Person Sleep
ID Gender Age Occupation Duration
Physical Stres
Quality Activity s BMI
of Sleep Level Level Category
SOURCE
CODE
import pandas as pd
importmatplotlib.pyplotasplt
fromgoogle.colabimport files
uploaded=files.upload()
dfsleep_health=pd.read_csv('Sleep_health_and_lifestyle_dataset.csv')
dfsleep_health
#choice 1
defocc_sd ():
sleep_dep=6
#threshold sd at 6
occupation_sleep_deprived = sleep_deprived['Occupation'].value_counts()
plt.figure(figsize=(8, 8))
plt.pie(occupation_sleep_deprived,
labels=occupation_sleep_deprived.index, startangle=140)
plt.show()
#choice 2
defage_gs():
age_group_gs = good_quality_sleep['Age'].value_counts()
plt.figure(figsize=(10, 6))
plt.xlabel('Age')
plt.ylabel('Number of Individuals')
plt.grid(True)
plt.show()
#choice 3
defocc_age_maxpal():
# Group the data by age and calculate the mean physical activity level for
each age group
# Find the age group with the maximum physical activity level
max_activity_age_group = age_group_activity.idxmax()
plt.figure(figsize=(10, 6))
age_group_activity.plot(kind='bar', color='magenta')
plt.xlabel('Age Group')
plt.legend()
plt.xticks(rotation=45)
plt.show()
#choice 4
defsleep_BMI():
# Group the data by 'BMI Category' and 'Sleep Disorder', and then count
the occurrences
plt.xlabel('Sleep Disorder')
plt.ylabel('Number of People')
plt.legend(title='BMI Category')
plt.tight_layout()
plt.show()
#choice 5
defavg_age():
average_age = dfsleep_health['Age'].mean()
plt.ylabel('Frequency')
plt.title('Age Distribution')
plt.show()
#choice 6
defage_highstress_pa():
plt.figure(figsize=(10, 6))
plt.xlabel('Age')
plt.ylabel('Average Level')
plt.legend()
plt.grid(True)
plt.show()
#choice 7
defsleep_disorder():
most_prevalent_sleep_disorder = sleep_disorder_counts.idxmax()
plt.figure(figsize=(10, 6))
plt.bar(sleep_disorder_counts.index, sleep_disorder_counts.values,
color='skyblue')
plt.xlabel('Sleep Disorder')
plt.ylabel('Count')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
#choice 8
defcomparison():
plt.figure(figsize=(10, 6))
plt.subplot(131)
plt.subplot(133)
plt.tight_layout()
plt.show()
#choice 9
defplot_bmi_vs_activity():
plt.xlabel('BMI Category')
plt.ylabel('Count')
plt.show()
print('Hey.',a,'! Here are some questions for you to test your sleep
health.')
whileTrue:
print("."*222)
print("\t","\t","\t","\t","\t","\t","\t","\t","\t","\t","MENU")
print("."*222)
print("."*222)
print("."*222)
print("1. To display the occupation group who are sleep deprived")
print("2. To display the age group who are getting good quality (7-10) of
sleep")
print("3. To display the age group who has the maximum physical activity
level")
print("6. To display age groups that tend to have higher stress levels or
lower physical activity levels")
ifch==1:
print("THANK YOU" , a)
occ_sd()
elifch==2:
print("THANK YOU" , a)
age_gs()
elifch==3:
print("THANK YOU" , a)
occ_age_maxpal()
elifch==4:
print("THANK YOU" , a)
sleep_BMI()
elifch==5:
print("THANK YOU" , a)
avg_age()
elifch==6:
print("THANK YOU" , a)
age_highstress_pa()
elifch==7:
print("THANK YOU" , a)
sleep_disorder()
elifch==8:
print("THANK YOU" , a)
comparison()
elifch==9:
print("THANK YOU" , a)
plot_bmi_vs_activity()
elifch==10:
print("Thank you for being with us and taking the time to review our
analysis. We hope that the insights provided have been valuable and
informative.")
break
OUTPUT
SCREEN
REFERENCE
S
● NCERT Informatics Practices
● https://www.kaggle.com/
● https://www.pythonforall.com/