How to make a Machine Learning Recommendation System using JavaScript

Pradyuman Dixit
HackerNoon.com
7 min readMay 3, 2019

--

Are you interested in machine learning and its applications? If you’re looking to learn something new and great, how about a machine learning recommendation system?

Photo by Franck V. on Unsplash

Well, don’t be scared or blown away with those massive terms and words, they just tell someone what exactly some code does.

Machine learning has always been in the news, whether for taking away jobs or making a robot understand and reply to a human. But what exactly can we do with machine learning?

The answer is, almost everything.

Whatever a human can do, machine learning model can do better.

Now before anyone who has seen such models failing drastically starts commenting furiously, let me rephrase that statement a bit.

Whatever a human can do, machine learning model can do better; given time and effort.

Well enough of ML vs human, let’s get into what we’re making here. So here we would be building a machine learning recommendation system.

What is a machine learning Recommendation System?

A Recommendation System is a machine learning model that recommends new options to choose from and learns from your previous actions on a topic. This means that the model would understand your selection and recommend choices that you “might” like. You might have seen Spotify, Netflix or Amazon recommended items. This is similar to that, but maybe a little less efficient.

What exactly are we going to recommend?

After giving it a bit of thought, movies and songs require a lot of other code and data that would make the learning harder, so I came up with the code/text editor theme.

Everybody (read almost everybody) loves customisable themes for their IDE or text editors and spend a lot of time looking for themes they would like to use.

This problem is what we would be targeting in this example because it makes learning the concept of machine learning recommendation system easy to understand, and makes up an example that would be usable in the real world.

How would it look?

Here’s how it’ll look (for best results, open this in private/incognito window).

Let’s get into some code now. We will be using JavaScript, CSS, and HTML along with the ‘brain.js’ machine learning library to make this model.

The complete code can be found in this GitHub repository.

Now we’ll discuss the JavaScript code that we are using to make this model work. First let’s write the code for instantiating some variables, libraries and code that will make up the UI for our webpage.

These are some of those variables that we will be using to change the text colour and theme of the text in our webpage.

We will now generate a random theme on load of the webpage. Here we also give the user the choice to vote the themes and use that data to recommend more themes that would appeal to the user.

We will be writing code for generateRandomTheme() and predictThemeCombinations() later.

Also, we need to write code for stars that user can use to rate the theme. We will be making it a bit interactive by making the star golden when the user hovers over it or clicks on it. The theme is rated and that data is used by our model to predict further choices for the user.

We will also be writing code for setStars() and clearStars() functions.

Now let’s write the code for the function saveTrainingData() for the score that the user generates when he rates the theme. This method saves training data according to the score and uses it to train our model.

Here we also have used the method clearStars() which basically removes the star rating from the page if the user has not clicked on it and removes the cursor.

The setStars() method does exactly what we discussed earlier.

Now let’s write code for generateRandomTheme() that we used at the very beginning of the webpage. This method just gets a random background colour and 3 other random colours to set the theme for the first time.

And the supplement methods (functions if you call them) that we used above.

And the method that does it all. The predictThemeCombinations() function uses the user’s liked history trained network to get colours and themes that they would probably like. Take some time to understand the working of this method. It’s simple but clever.

Also, we have 100,000 results and we don’t just want to show the user the first one. A top 20 recommendations would be better. So we just sort the results and show the top 20 results from them.

addNewTheme()? Well, this method would also contain a lot of HTML and would be a bit ugly. But in essence, we are just using the colour themes in the HTML components, using this.

Did I say a bit ugly? Well, what can I say…

And believe me, this method does nothing but set colours that we get in our Top 20 list and sets them up in the components.

Again, the complete code can be found in this GitHub repository.

So in nutshell

We gave the user the choices to choose a background colour and text colour with its variants. Then the user rated the combination of that choice shown as a theme. We took that choice to train our model to present themes that align with the user’s choices.

The model simply makes more combinations of the colours the users choose or the colours that are similar and is trained with that. This lets it make a list of other possible choices for the user. Then we get the top choices using sorting and display the top 20.

And the result?

In case we’re meeting for the first time here, I am Pradyuman Dixit and I mostly write about Machine learning, Android Development and sometimes about Web Development.

You can read my other Machine Learning posts here:

How to make a simple Machine Learning Website from Scratch

How to understand machine learning with simple code examples

--

--