Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Predict Outcomes with Regression Functions in JavaScript

    Tyler ClarkTyler Clark

    Regression involves analyzing multiple sources of data and predicting a response based off the results of the data. It takes using the k-nearest neighbors algorithm to the next level by choosing a N number of closest neighbors and taking the average between them. Let’s write a function that tries to predict how many pies our restaurant should make today!



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: Let's say that I own a restaurant that sells pies and lately I've been having trouble making enough pies for the day. Sometimes I make too much and sometimes too little. For the past few days I've been keeping track of the weather, if it's a weekend or not, and how many pies I sold that day.

    If it was a weekend, I put the number one. If not, it's a zero. The smaller the temperature, the colder it was that day, and the total number of pies I sold. Using this data, I want to calculate how many pies I should prepare for today.

    In order to do this, we first need to have a function that will calculate the distance between today's data and our previous results array of data. This calc distance function implements using Euclidean distance to calculate the distance, or in other words, puts a number to how similar today's data is with our previous results.

    Let's begin writing this function by destructuring the first and second results we'll get back from our calc neighbors function we need to create. Passing through our previous results and today's information, we'll say it is a weekend and the temperature outside is mild. Let's create our calc neighbors function, which we know takes results and let's destructure today's information.

    We're going to return the results of reducing over our results array, which inside of here we're going to call our calc distance function over the features that we've defined, which include is it a weekend and what's today's temperature.

    Once we have those features outlined we're going to return a new array each time, spreading what we've currently accumulated but adding a new object with a dis property that shows our calculated distance.

    At the end here, we're going to sort so that we get the smallest distance object first. This way when we destructure first and second on line 19 we're getting the two closest neighbors.

    Again, we're looping over each of our data sets and calculating how similar today's information is against the previous data. We return a sorted array that puts the closest neighbors, meaning the most similar to today's data first. This algorithm of calculating the distance of neighbors is called the K nearest neighbors algorithm.

    Then if we console dot logged first pies plus second pies and divide this by two, we'll get the number of pies we should make for today. This process of finding an N number of closest neighbors and then finding the average between them is called regression. It's a simple but powerful way of predicting future events based on past events.

    In our example we're only getting the average of the two closest neighbors, though this is just one-use case and there's no magic number of neighbors to get the best answer. A general rule of thumb is to use the square root of the number of items that we have to know how many neighbors to calculate against.

    As you can probably imagine, the more features that we have to work with, the more accurate our function will become. There's nothing special about having just two features of today.