Sleep Analysis Using Python (Part 1)

Man dreaming of sleep analysis
Photo by Julie Johnson on Unsplash

In this post I show how you can process and perform an initial analysis on your sleep data using python and jupyter notebooks. While this post will be relatively straightforward I’ll build on it incorporating other data over time. I have the Samsung Galaxy S8 Plus and the Samsung Gear Sport, so I’ll be using data from the Samsung Health App.

Set Up

For this analysis I used a jupyter notebook. It’s definitely a little overkill to use jupyter for this level of analysis (it would be relatively simple to use excel) but I hope to show the full power of jupyter over time as I continue these analyses. I’m generally bullish on jupyter overtaking excel as the premier data analysis tool over the next few years, and if you haven’t used it in the past, it’s one of the easiest ways to get started with python. I recommend downloading Anaconda, it’s free and comes with python, R, jupyter notebooks, and makes setting up new environments extremely easy.

I’ve included the step by jupyter notebook below, which can also be downloaded here.


While you can see the full results in the notebook, here’s the main result of my sleep analysis: my average sleep over the past year and a half as a 28 day rolling average. You can see that the average shot up around late February as I went on vacation, and since I’ve been back I’ve been more conscientious of my sleep.

Recently I’ve been slipping a bit, so I definitely want to be proactive and set a goal of going to bed at 9:30pm each night and waking up at 5:30am every day. While it may seem excessively early it gives me time to work out in the morning. In college I yo-yo’d between waking up early and waking up late, and I found that was more disruptive to my sleep cycle. My goal is not only to sleep for ~8 hours every night, but also to fall asleep and wake up at consistent times.

Leave a Reply

Your email address will not be published.