JavaScript – Machine Learning – Linear Regression

Git Hub Code

So many topics, so little time.  I had intended to focus only on setting AWS code pipelines for each component of TGIMBA.  However, as with many things in life, you need to take advantage of opportunities when they present themselves.  One such opportunity presented itself to me in the form of a Machine Learning course.  More specifically, Professor Andrew Ng’s Machine Learning course on Coursera (see reference #1).  I am taking the course at my own pace (tough to keep up while working full time).  The first algorithm covered is Linear Regression.

Conceptually, it is pretty straight forward.  Given a list of x,y coordinates, the algorithm (once computed) provides a f(x) line slope formula that will give an average of the available x,y coordinates.  If you wanted to predict the location of y for a given x, you can.

However, after I started working through the example, the math didn’t add up. I worked through what I thought the correct implementation of the algorithm, but it wasn’t correct.  I posted some questions, but I was not able to get a concrete example of the algorithm.  So, I looked around online and found an excellent example.  In the article “Simple Linear Regression Tutorial for Machine Learning”, Jason Brownlee illustrates a complete math example of a single variable linear regression.

In this blog post, I will walk you through my JavaScript implementation of his example.

To run:

  • Make a get request

Screen Shot 2017-10-28 at 10.26.54 PM

  • View Results

Screen Shot 2017-10-28 at 10.26.35 PM

The ‘structure’ of the program follows the article’s math flow.

Screen Shot 2017-10-28 at 10.31.08 PM

The process is

  • Get the data sets

Screen Shot 2017-10-28 at 10.34.44 PM

  • Calculate the mean X and Y values

Screen Shot 2017-10-28 at 10.34.01 PM

  • Calculate the numerator

Screen Shot 2017-10-28 at 10.36.04 PM.png

  • Calculate Numerator – Part One

Screen Shot 2017-10-28 at 10.37.44 PM

  • Calculate Numerator – Part Two

Screen Shot 2017-10-28 at 10.37.55 PM

  • Calculate the Denominator

Screen Shot 2017-10-28 at 10.38.06 PM

  • Make a prediction

Screen Shot 2017-10-28 at 10.38.13 PM

What was really cool about this experience was having a complete example.  My next blog post will deal with taking a data set from Professor Ng’s class and seeing if Mr. Brownlee’s math holds up.  If so, I will expand it to multiple variables.

Stay tuned!



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s