#native_company# #native_desc#

Linear Regression

Let's get physical

Sometimes, nothing beats holding a copy of a book in your hands. Writing in the margins, highlighting sentences, folding corners. So this book is also available from Amazon as a paperback.

Buy now on Amazon

Linear regression is the hello world of machine learning. It’s the simplest predictive modelling you can do, and it’s predictive because given an independent value x you can then predict what the dependant value of y will be. For example cricket chirps increase linearly with the independent variable temperature, so if the temperature is 70 degrees fahrenheit, then cricket chirps should be around 120 per minute, at least in Iowa in autumn where this data was collected.

Figure 1. Example linear regression cricket chirps vs. temp.

It’s also predictive because you can extend the line beyond the data you have, you can extrapolate from the data. So you can use a value for x that you have never seen before and have no data about to predict the value of cricket chirps per hour.


Figure 2. Equation of a line

A line is represented by an equation y = a*x + b.

  • y is the value you’re trying to predict based on the value of x

  • b is the intercept of the line to the y axis.

  • a defines the slope of the line

x is independent variable, y is the dependant variable, the nature of the relationship is defined by the values a and b. To find the line that best represents a linear relationship between two sets of variables x and y we need to find values for a and b, we call this the best fit line.


It’s common in Machine Learning to refer to x's as the inputs to a Neural Network (independent variables) and the y's are the outputs (dependant variables).

One reason for using linear regression as our first proper TensorFlow.js application is that the problem can be represented as a single neuron.

Figure 3. Equation of a line represented as an artificial neuron

As long as we have some input data (x) with known good values (y) we can use TensorFlow.js to optimize (train) the values of a and b to find our best fit line.

The application we will be building in this chapter

We could demo this with a set of data such as temperature and cricket chirps, but I find it’s easier to understand what’s going on when you can play around with the data yourself, adjust, and see how this affects the training and outcome.

So in our demo app you will be making the data points yourself from your own clicks on the screen, you could just as easily load up your own data sourced from somewhere else though.

linear regression app completed
Figure 4. Completed Linear Regression Application

Every time you click on the screen, you add a data point and it re-trains the values of a and b through 300 executions of optimizer.minimise.

Advanced JavaScript

This unique course teaches you advanced JavaScript knowledge through a series of interview questions. Bring your JavaScript to the 2021's today.

Level up your JavaScript now!