Press "Enter" to skip to content

Machine-Learning in JS: estimating monthly distance

It has been a few years since we had AI courses at the university, it was amazing, but then I started working and boring projects got higher priority over fun stuff. I’m freaking lazy to create a neural network from scratch again, as we did a few years ago, let alone in Java or C++. So naturally started searching for examples in JavaScript and came across a sympathetic little library called Brains.js that I needed to give a try.

I’ve been tracking the costs of my cars since 2016 including the refuelling (distance travelled, fuel burnt, cost of fuel), so the project is given: I want to estimate the distance travelled in a given year and month. The learning consists of the data from my spreadsheet.

Learning data

The data is obtained from my spreadsheets. Have to merge them and delete the unused columns. Then it is exported as CSV (directly from Google Drive) and with another online tool I converted the csv file to json:

...  
  {
    "date": "2016.06.22",
    "trip": 0
  },
  {
    "date": "2016.07.04",
    "trip": 625
  },
  {
    "date": "2016.07.15",
    "trip": 1134
  },
...

I wrote a small function that processes that json and creates another json with the monthly sum:

...
  {
     "year":"2016",
     "month":"07",
     "distance":2201
  },
  {
     "year":"2016",
     "month":"08",
     "distance":716
  },
  {
     "year":"2016",
     "month":"09",
     "distance":1672
  },
...

Expectations

I know that during the winter period I usually travel less (hate winter, hate being outside in the cold). So the main expectation is to get a lower estimation for the winter months than for the summer. Also 2020 is a special year because of the Covid-19. I work from home, no more need for my daily 20km commute and overall I think I’ve traveled less by car compared to previous years. Another expectation would be for the machine to pick up this decreasing trend and give me a lower estimation for future years.

Here’s a chart with all data summarised:

As you may see, I usually go into hibernation from January to March, barely travel by car. I travel more starting with April. But not this year…

The code

Don’t want to go into too much details about the setup, the only requirement is Node JS. I’ve created a new project (node init -y) and installed brain.js using npm. Let the fun begin!

I want all values to be between 0 and 1. I am dividing the years by 2050 and the months by 12.

const getInput = (year, month) => ({ year: year / 2050, month: month / 12});

As for the distance, I’m computing the maximum distance per month and I divide all distances with that value:

const maxDistance = Math.max(...trainingData.map(i => i.distance));

Then the whole data is turned into an array that Brains can process, with input and output objects:

const netTrainingData = trainingData.map(i => ({
  input: getInput(i.year, i.month),
  output: {
    distance: i.distance / maxDistance,
  },
}));

We’re ready, let’s create a neural network and train it using our data:

const net = new brain.NeuralNetwork();
net.train(netTrainingData, { log: true });

I’ve found that by default it does 20 000 iterations for my learning data with a training error of 0.028628. I tried 1 000 000 iterations and the training error went slightly down to 0.027377.

And finally we want to run some tests:

const result = net.run(getInput(2021, 1));
console.log('Result', result.distance * maxDistance);

The results

I run the network for all 12 months between 2021 and 2025.

I’m quite happy with the results! It successfully detects my stay-at-home period (January to March) and starting with April it gradually increases the estimated distance. Unfortunately I’m not sure if it picked up the decreased travel distance in 2020, as it shows a decreasing trend in the first few months, then the trend reverses.

All in all, taking into consideration the small amount of data – only four years – used to train the neural network, the results are close to my expectations. But the whole point of this project was to give Brain.js a try. It is very easy to get started with it, I would definitely use it in the future for POCs or even small AI projects.