0% found this document useful (0 votes)
27 views20 pages

Sleep Health Analysis

Uploaded by

Upama Ganguly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views20 pages

Sleep Health Analysis

Uploaded by

Upama Ganguly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

PROJECT

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

COLUMNS OF CSV FILE

Person Sleep
ID Gender Age Occupation Duration

Physical Stres
Quality Activity s BMI
of Sleep Level Level Category

Blood Heart Daily Sleep


Pressure Rate Steps Disorder

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

sleep_deprived = dfsleep_health[dfsleep_health['Sleep Duration'] <6]

occupation_sleep_deprived = sleep_deprived['Occupation'].value_counts()

# Plot the pie chart

plt.figure(figsize=(8, 8))

plt.pie(occupation_sleep_deprived,
labels=occupation_sleep_deprived.index, startangle=140)

plt.title('Occupation Groups of Sleep-Deprived Individuals')

plt.axis('equal') # Equal aspect ratio ensures the pie chart is circular.

plt.show()

#choice 2

defage_gs():

#individual with good quality of sleep


good_quality_sleep = dfsleep_health[(dfsleep_health['Quality of Sleep']
>= 7) & (dfsleep_health['Quality of Sleep'] <= 10)]

#grouping the individuals by thier age

age_group_gs = good_quality_sleep['Age'].value_counts()

# Create a line chart

plt.figure(figsize=(10, 6))

plt.plot(age_group_gs.index, age_group_gs.values, marker='o')

plt.title('Age Groups with Good Quality Sleep')

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

age_group_activity = dfsleep_health.groupby('Age')['Physical Activity


Level'].mean()

# Find the age group with the maximum physical activity level

max_activity_age_group = age_group_activity.idxmax()

# Plotting the chart

plt.figure(figsize=(10, 6))

age_group_activity.plot(kind='bar', color='magenta')

plt.title('Average Physical Activity Level by Age Group')

plt.xlabel('Age Group')

plt.ylabel('Average Physical Activity Level')


plt.axvline(x=max_activity_age_group, color='red', linestyle='--',
label='Max Activity 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

grouped_data = dfsleep_health.groupby(['BMI Category', 'Sleep Disorder'])


['Person ID'].count()

# Plot the data using a stacked bar chart

grouped_data.plot(kind='bar', stacked=True,figsize=(10, 6))

plt.title('Relationship between Sleep Disorders and BMI Categories')

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()

print("Average Age:", average_age)

# Display age distribution using a histogram

plt.hist(dfsleep_health['Age'], bins=20, edgecolor='black')


plt.xlabel('Age')

plt.ylabel('Frequency')

plt.title('Age Distribution')

plt.show()

#choice 6

defage_highstress_pa():

age_grouped = dfsleep_health.groupby('Age').agg({'Stress Level':


'mean','Physical Activity Level': 'mean'}).reset_index()

plt.figure(figsize=(10, 6))

plt.plot(age_grouped['Age'], age_grouped['Stress Level'], marker='o',


label='Stress Level')

plt.plot(age_grouped['Age'], age_grouped['Physical Activity Level'],


marker='o', label='Physical Activity Level')

plt.title('Average Stress Level and Physical Activity Level by Age Group')

plt.xlabel('Age')

plt.ylabel('Average Level')

plt.legend()

plt.grid(True)

plt.xticks(age_grouped['Age']) # Set x-axis ticks to match age groups

plt.show()

#choice 7

defsleep_disorder():

# Remove rows with Sleep Disorder as "None"

dfsleep_health_filtered = dfsleep_health[dfsleep_health['Sleep Disorder'] !


= 'None']

# Count the occurrences of each sleep disorder


sleep_disorder_counts = dfsleep_health_filtered['Sleep
Disorder'].value_counts()

# Get the most prevalent sleep disorder

most_prevalent_sleep_disorder = sleep_disorder_counts.idxmax()

# Plot the chart

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.title('Prevalent Sleep Disorder')

plt.xticks(rotation=45)

plt.tight_layout()

print(f"Most Prevalent Sleep Disorder excluding the none values:


{most_prevalent_sleep_disorder}")

plt.show()

#choice 8

defcomparison():

plt.figure(figsize=(10, 6))

# Creating a histogram for Physical Activity Level

plt.subplot(131)

plt.hist(dfsleep_health["Physical Activity Level"], color='r', bins=10)

plt.title("Physical Activity Level")

# Creating a histogram for Daily Steps

plt.subplot(133)

plt.hist(dfsleep_health["Daily Steps"], color='y', bins=10)


plt.title("Daily Steps")

plt.tight_layout()

plt.show()

#choice 9

defplot_bmi_vs_activity():

grouped = dfsleep_health.groupby(['BMI Category', 'Physical Activity


Level']).size().unstack()

grouped.plot(kind='bar', stacked=True, figsize=(10, 6))

plt.title('Comparison between BMI Category and Physical Activity Level')

plt.xlabel('BMI Category')

plt.ylabel('Count')

plt.legend(title='Physical Activity Level')

plt.show()

a=input("Enter your name")

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("\t","\t","\t","\t","\t","\t","\t","\t","\t","Warm Greetings from us to you")

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("4. To display the relation between sleep disorders and obese


people")

print("5. To display average age and age distribution of individuals in the


dataset?")

print("6. To display age groups that tend to have higher stress levels or
lower physical activity levels")

print("7. To display the sleep disorder which is most prevalent in the


dataset")

print("8. To display the comparison between physical activity and daily


steps")

print("9. To display the comparison between BMI category and physical


activity")

print("10. Exit ")

ch=int(input("Enter your choice"))

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/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy