How to build machine learning models using JavaScript

How to build machine learning models using JavaScript

Currently, the main languages ​​for modeling in the field of machine learning are Python and R. The machine learning framework Angel launched by Tencent not long ago supports Java and Scala. The author of this article, Abhishek Soni, tells us through his actions that JavaScript can also be used to develop machine learning models.

JavaScript? Shouldn’t I be using Python? Even Scikit-learn doesn’t work on JavaScript.

It is possible, in fact, I am surprised at how little developers have paid attention to it. In the case of Scikit-learn, Javascript developers have actually come up with a library that does the trick, which will be covered in this article. So, let’s see what Javascript can do for machine learning.

According to artificial intelligence pioneer Arthur Samuel, machine learning provides computers with the ability to learn without being explicitly programmed. In other words, it enables computers to learn on their own and execute correct instructions without humans providing all the guidance.

Google has long since switched its mobile-first strategy to an AI-first one.

Why is JavaScript not mentioned in the machine learning community?

  • Slow (really?)
  • Matrix manipulation is difficult (there are libraries for this, like math.js)
  • Only for web development (there is Node.js though)
  • Machine learning libraries are usually in Python (luckily, there are many JS developers as well)

There are some pre-made libraries available in JavaScript that contain some machine learning algorithms like Linear Regression, SVM, Naive Bayes, etc. Here are some of them.

  • brain.js (neural networks)
  • Synaptic (neural network)
  • Natural (Natural Language Processing)
  • ConvNetJS (Convolutional Neural Network)
  • mljs (a set of sub-libraries with various functions)

First, we will use the mljs regression library to perform some linear regression operations.

Reference code: https://github.com/abhisheksoni27/machine-learning-with-js

1. Install the library

$ npm install ml-regression csvtojson
$ yarn add ml-regression csvtojson

ml-regression, as its name suggests, is responsible for linear regression in machine learning.

csvtojson is a fast CSV parser for node.js that allows loading CSV data files and converting them to JSON.

2. Initialize and load data

Download the data file (.csv) and add it to your project.

Link: http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv

If you have initialized an empty npm project, open index.js and enter the following code.

const ml = require('ml-regression');
const csv = require('csvtojson');
const SLR = ml.SLR; // Simple Linear Regression

const csvFilePath = 'advertising.csv'; // Data
let csvData = [], // parsed Data
    X = [], // Input
    y = []; // Output

let regressionModel;

I put the file in the root of my project, if you want to put it somewhere else, remember to update the csvFilePath.

Now we use the fromFile method of csvtojson to load the data file:

csv()
    .fromFile(csvFilePath)
    .on('json', (jsonObj) => {
        csvData.push(jsonObj);
    })
    .on('done', () => {
        dressData(); // To get data points from JSON Objects
        performRegression(); 
    });

3. Pack data and prepare for execution

The JSON object is stored in csvData and we also need an array of input data points and an output data point. We run the data through a dressData function which populates the X and Y variables.

function dressData() {
    /**
     * One row of the data object looks like:
     * {
     * TV: "10",
     * Radio: "100",
     * Newspaper: "20",
     * "Sales": "1000"
     * }
     *
     * Hence, while adding the data points,
     * we need to parse the String value as a Float.
     */
    csvData.forEach((row) => {
        X.push(f(row.Radio));
        y.push(f(row.Sales));
    });
}

function f(s) {
    return parseFloat(s);
}

4. Train the model and start prediction

Now that the data is packaged, it’s time to train our model.

To do this, we need to write a performRegression function:

function performRegression() {
    regressionModel = new SLR(X, y); // Train the model on training data
    console.log(regressionModel.toString(3));
    predictOutput();
}

The performRegression function has a method toString that takes a parameter called precision for floating point output. The predictOutput function lets you input a numeric value and then sends the output of the model to the console. It looks like this (note that I'm using the Node.js readline tool):

function predictOutput() {
    rl.question('Enter input X for prediction (Press CTRL+C to exit) : ', (answer) => {
        console.log(`At X = ${answer}, y = ${regressionModel.predict(parseFloat(answer))}`);
        predictOutput();
    });
}

The following is the code to increase the reading user

const readline = require('readline'); // For user prompt to allow predictions

const rl = readline.createInterface({
    input: process.stdin, 
    output: process.stdout
});

5. You’re done!

Following the above steps, your index.js should look like this:

const ml = require('ml-regression');
const csv = require('csvtojson');
const SLR = ml.SLR; // Simple Linear Regression

const csvFilePath = 'advertising.csv'; // Data
let csvData = [], // parsed Data
    X = [], // Input
    y = []; // Output

let regressionModel;

const readline = require('readline'); // For user prompt to allow predictions

const rl = readline.createInterface({
    input: process.stdin, 
    output: process.stdout
});

csv()
    .fromFile(csvFilePath)
    .on('json', (jsonObj) => {
        csvData.push(jsonObj);
    })
    .on('done', () => {
        dressData(); // To get data points from JSON Objects
        performRegression(); 
    });

function performRegression() {
    regressionModel = new SLR(X, y); // Train the model on training data
    console.log(regressionModel.toString(3));
    predictOutput();
}

function dressData() {
    /**
     * One row of the data object looks like:
     * {
     * TV: "10",
     * Radio: "100",
     * Newspaper: "20",
     * "Sales": "1000"
     * }
     *
     * Hence, while adding the data points,
     * we need to parse the String value as a Float.
     */
    csvData.forEach((row) => {
        X.push(f(row.Radio));
        y.push(f(row.Sales));
    });
}

function f(s) {
    return parseFloat(s);
}

function predictOutput() {
    rl.question('Enter input X for prediction (Press CTRL+C to exit) : ', (answer) => {
        console.log(`At X = ${answer}, y = ${regressionModel.predict(parseFloat(answer))}`);
        predictOutput();
    });
}

Go to your terminal and run node index.js. The output you get should be something like this:

$ node index.js
f(x) = 0.202 * x + 9.31
Enter input X for prediction (Press CTRL+C to exit): 151.***t
Enter input X for prediction (Press CTRL+C to exit) :

Congratulations! You just trained your first linear regression model in JavaScript. (PS. Did you notice the speed?)

<<:  Flink Principles and Implementation: Architecture and Topology Overview

>>:  Long Text Decryption Convolutional Neural Network Architecture

Recommend

The popular energy drink is actually "poison"? How dangerous is radium water?

On the morning of March 31, 1932, Eben M. Byers d...

The way B station’s information flow ads go viral

The "Most Beautiful Night of 2019" New ...

A piece of experience, copied to the talented and smart copywriter

A few years ago, I had the great fortune of meeti...

Is it really necessary to clean barnacles from whales and turtles?

Recently, there have been many videos of people r...

The most elegant way to exit an Android app

[[185951]] Let's first look at some common ex...

Abandon 2.4GHz! The new Wi-Fi standard 802.11ax is coming

In our daily router reviews or shopping guides, w...