Health

How I Hacked My Infant Twins’ Sleep Using Machine Learning And Data Science

Sleep training, Mr. Robot-style.

by Tyler Lund
Updated: 
Originally Published: 
Data Science Improving Infant Twins' Sleep
flickr / Donnie Ray Jones

The following was syndicated from Medium for The Fatherly Forum, a community of parents and influencers with insights about work, family, and life. If you’d like to join the Forum, drop us a line at TheForum@Fatherly.com.

A month ago, I tried A/B testing to see how different “treatments” or input parameters might result in longer sleep for our twins — and of course by proxy, us. Through this, I found that sleep patterns were fairly erratic and didn’t find much that correlated strongly to increased sleep. As time went on, they started more largely on their own naturally. However, now that they have reached 4 months, they’ve begun the apparently common but rarely discussed sleep regression phase. I once again found myself desperate for more sleep. Was I, as one commented on the previous post pointed out, “looking to find anything no matter how desperate for more sleep?” Yes. Yes I was. Hence, I turned to another computer science technique, machine learning.

Machine learning is a field within computer science that provides methods for “teaching” computers or programs without giving them discrete instructions. In normal programming, I would give the computer a sequence of commands to execute, throwing in logic to make determinations on what to do based on input, but the program could never go off of these well defined rails. Though this description sounds closer to the Terminator than what it actually looks like in practice, machine learning allows one to take a set of “learning” data and use that for the basis of making predictions. Though it is becoming increasingly popular across the technology industry, it has mostly been used for looking at purchasing behavior and recommendations, AI, and perhaps most interestingly, to categorize and identify pictures and art — largely by Google. I couldn’t find many instances of it being applied to parenting.

Thanks to my wife’s accounting background and our type A personalities, we have detailed records on feeding and sleep behavior for the boys. Remember this spreadsheet?

Using this data, I set out to find the optimal combination of input parameters, in this case I looked at total food consumed in the day, time of last feeding, and amount of last feeding, to determine what would result in the longest night time sleep for the boys. And best of all I could let the computer do the hard work. Plus, we had twice as much data on these patterns because we of course have not one but 2 boys.

There are several great libraries for machine learning available across programming languages. Even though I primarily use Java and Javascript for work, I chose the Python library sklearn, as I have familiarity with Python and this seemed the perfect application for the flexible scripting language. There are many great tutorials out there for this library and good documentation as well.

You can find my code on github if interested.

Exporting the data we collected from the spreadsheet, I extracted just the past month of data points. Because of the rapid growth and developmental changes infants go through, I felt one month’s data —  times 2 infants  — was a good balance of recency and enough data to make a prediction.

Putting It Together And Coding It Up

I also got some stats that were interesting including that on average we fed the boys 27.5 oz per day, put them to bed most often at 7:25, fed them 5.22 oz at bedtime, and they slept 9 hours on average. It also turned out that the boys slept over 10 hours 75 percent of the time. Not too bad. But I knew with some more work we could improve this.

Using this data, I was able to plot each parameter against the sleep time.

You can see that there already isn’t a strong correlation, at least linear between most of these. The closest is the somewhat linear —  with lots of outliers —  relationship between total food and hours of sleep. Oddly more food seems to result in less sleep.

There are many different types of machine learning algorithms. These are largely classified into linear and non-linear types. I took the data and ran it through 6 fairly widely used algorithms to see how accurate each could get. Here are the results.

Or in a more pictographic form

Here you can see the Support Vector Machine algo clearly performed the best against the data, though the range is very wide and the mean isn’t much better than the others. None are even at 50 percent which is pretty poor for predicting. This is entirely due to the pseudo-random nature of the data. Even with such results, I decided to forge ahead, hoping to gleam any insight at all.

Using SVM, I trained the algorithm on the input data. Using this, I could now predict how much sleep we’d be getting based on the amount of food taken in a day, when we put them to bed, and how much we fed them at the last feeding. For example, giving them 28 ounces, putting them to bed at 7, and giving 6 ounces at that last feeding would result in a fairly poor 8 hours of sleep.

Conclusions

With the algorithm now trained, I could predict with some certainty how much sleep we would be getting. More importantly, I could look at the trend lines for these and see what type of behavior led to more sleep in an attempt to optimize. Counterintuitively, going to bed earlier, with less food then and throughout the day actually increased sleep. This is likely due to many factors but my theory is that less food means less gas, less stomach shrinkage when hungry, and therefore more restful sleep. How many times have you pigged out and then bizarrely woken up in the middle of the night starving?

Unfortunately, just as with the A/B testing, no one individual input seems to have a strong direct input on sleep. I guess if it did someone would have already discovered it and would make millions. Taken together, machine learning can find some of these trends and associations between variables, which leads to better, more accurate results than A/B testing or trial and error, but the results are still far from great. From this data, at most I could get a 41 percent accuracy rate of predictions. That means more often than not they are wrong. Due to the frequent changes in development and even the differences between the boys, it’s hard to apply data across them. Again, a larger sample size might help, but we’re not aiming for triplets anytime soon.

Still, these results are better than nothing and help demonstrate the promise of the field of machine learning and data science. I for one prefer to make decisions from data over gut feelings and this data even just confirming some of my guesses makes me feel better about our parenting approach.

Tyler Lund is the editor of Dad on the Run.

This article was originally published on