Our journey begins with lots of details about how individuals sleep, their well being, what they do of their each day lives, and extra. Think about it like an enormous map on a pc that exhibits all of the alternative ways individuals act and the way their our bodies work. Inside this info are hints that would assist us perceive stress higher.
# Importing the dataset
df = pd.read_csv("Sleep_health_and_lifestyle_dataset.csv")
df.form #(374,13)
However earlier than we begin our investigation, we have to get our knowledge prepared. This implies fixing any errors, altering phrases into numbers, and ensuring all the things is sensible. It’s a bit like wiping a unclean microscope lens clear so we are able to see all of the vital particulars clearly.
# Dropping pointless columns
df.drop(columns=['Person ID','Occupation'], inplace=True)# Changing categorical variables into numerical ones
df['Gender'] = df['Gender'].map({'Male':1,'Feminine':0})
df['BMI Category'] = df['BMI Category'].map({'Regular':1,'Regular Weight':1,'Obese':2,'Overweight':3,'UnderWeight':0})
# Splitting Blood Strain into Systolic and Diastolic columns
df['SBP'] = ''
df['DBP'] = ''
for i in vary(len(df)):
okay = df['Blood Pressure'][i].cut up("/")[0]
j = df['Blood Pressure'][i].cut up("/")[1]
df['SBP'][i] = okay
df['DBP'][i] = j
df.drop(columns='Blood Strain', inplace=True)
# One-hot encoding for Sleep Dysfunction column
one_hot_encoded = pd.get_dummies(df['Sleep Disorder'], prefix='Sleep_Disorder', dtype=int)
df = pd.concat([df, one_hot_encoded], axis=1)
df.drop(columns='Sleep Dysfunction', inplace=True)
Now that our knowledge is all set, we cut up it into two teams: a coaching set and a testing set. The coaching set is like our lab, the place our fashions learn to acknowledge stress. The testing set is the place we put our fashions to the take a look at in real-life conditions to see how nicely they work.
# Dividing the information into coaching and testing units
X = df.drop(columns='Stress Degree')
y = df['Stress Level']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2)
To verify our evaluation is honest, we use a software known as MinMaxScaler. It helps preserve all the things balanced by placing all of the options on a fair taking part in area. This stops anybody factor from having an excessive amount of say in our fashions. Consider it like ensuring each participant on a sports activities workforce has an equal probability to attain a aim.
# Scaling the options utilizing MinMaxScaler
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)
Meet the LazyClassifier — It could take a look at many alternative fashions without having lots of work from us. This protects us effort and time, serving to us give attention to essentially the most promising paths to discover. It’s like having a sensible mentor who exhibits us the way in which ahead in our seek for solutions.
# LazyClassifier for mannequin choice
from lazypredict.Supervised import LazyClassifier
clf = LazyClassifier(verbose=0, ignore_warnings=True, custom_metric=None)
fashions, predictions = clf.match(X_train, X_test, y_train, y_test)
print(fashions)
After pondering rigorously, we discovered our greatest mannequin: the ExtraTreesClassifier. It’s like having a super-smart good friend who’s actually good at determining stress ranges. This mannequin provides us actually correct predictions, like an knowledgeable serving to us navigate by means of sophisticated knowledge.
# Coaching the ExtraTreesClassifier mannequin
from sklearn.ensemble import ExtraTreesClassifier
mannequin = ExtraTreesClassifier()
mannequin.match(X_train, y_train)
y_pred = mannequin.predict(X_test)# Evaluating mannequin efficiency
from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_pred) #0.9866666666666667
Similar to heroes want a protected place to remain, our fashions want a house too. That’s the place Pickle is available in — it’s a easy however robust software for saving and storing our educated fashions. With Pickle on our workforce, we all know our precious insights are stored protected for later use, serving to us as we proceed our journey to know wellness.
# Saving the educated mannequin and scaler utilizing Pickle
import pickle
with open('scaler.pkl', 'wb') as f:
pickle.dump(scaler, f)with open('mannequin.pkl', 'wb') as f:
pickle.dump(mannequin, f)
As we close to the top of our journey, it’s time to make our mannequin helpful. Deployment means making it accessible to be used in apps or methods the place it might help individuals in managing stress. However that’s not the top of the story. We’ll preserve watching the way it performs, making changes and enhancements primarily based on suggestions and new info. Our goal? To proceed making a constructive impression on individuals’s lives, step-by-step.
Streamlit deployment code :
import streamlit as st
import pandas as pd
import pickle# Load scaler and mannequin utilizing pickle
with open('scaler.pkl', 'rb') as f:
scaler = pickle.load(f)
with open('mannequin.pkl', 'rb') as f:
mannequin = pickle.load(f)
# Streamlit app
st.markdown("""
<div fashion='text-align: heart;'>
<h2>About This Stress Predictor App</h2>
<p>This stress predictor app is designed to supply an estimation of stress ranges primarily based on enter parameters. It is vital to notice that the mannequin used on this app might not be 100% correct, and stress ranges can fluctuate vastly between people and conditions.</p>
<p>The expected stress stage supplied by this app is on a scale from 3 to eight, the place 3 signifies low stress and eight signifies excessive stress. Nevertheless, it is important to interpret the outcomes with warning and search skilled recommendation if mandatory.</p>
</div>
""", unsafe_allow_html=True)
# Enter kind to take person enter
st.sidebar.header('Enter Affected person Data')
gender = st.sidebar.selectbox('Gender', ['Male','Female'])
age = st.sidebar.number_input('Age', min_value=0, max_value=120, worth=30)
sleep_duration = st.sidebar.number_input('Sleep Length', min_value=0, max_value=24, worth=7)
quality_of_sleep = st.sidebar.slider('High quality of Sleep', min_value=1, max_value=10, worth=5)
physical_activity_level = st.sidebar.slider('Bodily Exercise Degree', min_value=1, max_value=10, worth=5)
bmi_category = st.sidebar.selectbox('BMI Class', ['Normal Weight','Under Weight', 'Overweight', 'Obese'])
blood_pressure = st.sidebar.text_input('Blood Strain (sbp/dbp)',worth="120/80")
heart_rate = st.sidebar.number_input('Coronary heart Fee', min_value=0, max_value=300, worth=70)
daily_steps = st.sidebar.number_input('Day by day Steps', min_value=0, worth=5000)
sleep_disorder = st.sidebar.selectbox('sleep problem', ['NaN', 'Sleep Apnea', 'Insomnia'])
# Make prediction
if st.sidebar.button('Predict'):
# Put together enter knowledge
input_data = pd.DataFrame({
'Gender': [1 if gender == 'Male' else 0],
'Age': [age],
'Sleep Length': [sleep_duration],
'High quality of Sleep': [quality_of_sleep],
'Bodily Exercise Degree': [physical_activity_level],
'BMI Class': [bmi_category],
'Blood Strain': [blood_pressure],
'Coronary heart Fee': [heart_rate],
'Day by day Steps': [daily_steps],
'Sleep Dysfunction':[sleep_disorder]
})
df = pd.DataFrame(input_data)
df['BMI Category'] = df['BMI Category'].map({'Below Weight':0,'Regular Weight':1,'Obese':2,'Overweight':3})
df['SBP'] = ''
df['DBP'] = ''
for i in vary(len(df)):
okay = df['Blood Pressure'][i].cut up("/")[0]
j = df['Blood Pressure'][i].cut up("/")[1]
df['SBP'][i] = okay
df['DBP'][i] = j
df.drop(columns='Blood Strain',inplace=True)
df['Sleep_Disorder_Insomnia']=''
df['Sleep_Disorder_Sleep Apnea']=''
if(sleep_disorder=='Insomnia'):
df['Sleep_Disorder_Insomnia']= 1
df['Sleep_Disorder_Sleep Apnea']=0
elif(sleep_disorder=='Sleep Apnea'):
df['Sleep_Disorder_Sleep Apnea']=1
df['Sleep_Disorder_Insomnia'] = 0
else:
df['Sleep_Disorder_Sleep Apnea']=0
df['Sleep_Disorder_Insomnia'] = 0
df.drop(columns='Sleep Dysfunction',inplace=True)
# Scale enter knowledge
input_data_scaled = scaler.remodel(df)
# Make prediction
prediction = mannequin.predict(input_data_scaled)
stress_level = prediction[0]
# Show prediction consequence
st.markdown(f"<h3 fashion='text-align: heart;'>Predicted Stress Degree: {stress_level}</h3>", unsafe_allow_html=True)
Subsequent i’m planning to gather the actual on-site knowledge like amassing the individuals knowledge from the medical clinics.