Stress Detection through Sleep — K-Nearest Neighbor(KNN)
Using K-Nearest Neighbor(KNN) model we would be making a Human Stress Detection model which will predict stress level through sleep
As in the previous article, I have given you an introduction to K-Nearest Neighbor(KNN) now I will tell you how to make a K-Nearest Neighbor(KNN) model in this article with some lines of codes.
if you want to know about the K-Nearest Neighbor(KNN) model click on this link —
so let’s start
the first step is we need to download the dataset and then apply the dataset to the model. you can download or copy data from the URL —
https://raw.githubusercontent.com/aviralb13/gitcodes/main/datas/sleep.csv
Importing the libraries
Now we will import pandas and NumPyas shown below. If your system does not have these libraries installed, you may get them using the pip command.
Data preparation
Now we’ll read the data using Pandas and save it in a variable called data so we don’t have to call it again and again. Using the head command, we can view the first 5 components of the data; if you want to see more, enter the number inside the bracket.
Defining X and Y
And now I have created a list in which I think it will be the deciding factor for predicting the stress level i.e. snoring rate, respiration rate, body temperature, limb movement, blood oxygen, eye movement, sleeping hours, and heart rate and assign it to variable features now I will pass features in the dataset and store it as x and the stress level as y.
Making the model
Splitting the Dataset
We must first import the test train split from sklearn model selection before splitting our model dataset into a train and test dataset.
Model
We’ll now import KNeighborsClassifier from sklearn.neighbours to create a KNN model In this case, we’re utilising KNN to classify our values. as well as calling our model classifier and fit our training dataset to the model.
Predicting
nowhere I have made a variable named prediction which will predict the values of our test data.
we also can predict entered values simply by adding the values to the list manually.
here I have entered values
snoring_rate=97.376
respiration_rat=27.376
body_temperature=86.72
limb_movemen=17.688
blood_oxygen=84.064
eye_movement=101.72
sleeping_hours=0
heart_rate=78.44
this means the stress level is 4 for the particular inputs
Accuracy
Now we’ll look at our model’s accuracy. Our model is 100 percent accurate, which means it correctly predicted 100 values out of a possible 100. Which is excellent.
source code
you can go check the link for full code
Conclusion
in the article I have given you information and codes on how to make a Stress Detection model with source code I would be making more exciting models for you so stay connected.