DEV Community

Cover image for Scikit-Learn, from Python to JavaScript
Oluwafemi Paul Adeyemi
Oluwafemi Paul Adeyemi

Posted on

Scikit-Learn, from Python to JavaScript

You may have found Scikit-learn to be a great package for your traditional Machine Learning tasks, especially if you are not creating your own implementations of the algorithms you use - going by a good tip in programming, which says: do not reinvent the wheel. Truth be told, you may feel you have a great tool, until you are about to switch to an entirely different programming language. The problem is how do you carry your Scikit-learn tool to another language?

If you are switching to R, there is a way around that: you just need to use a package called Reticulate to link Python and R, and then you can still use your Scikit-learn within R, if you really want to stick to Scikit-learn. But what if you were to switch to JavaScript? Well, fortunately, there are two solutions I can recommend.

1. Scikit.js

It takes very much after its mother, Scikit-learn from Python, except for four basic differences. With reference to the codes below, these differences are: (1) in JavaScript, while every other function takes in positional arguments (as in Python), class constructors take in objects (in the JavaScript code below, object = { fitIntercept: true }). Python, however, allows constructors (initializers) to take in positional arguments.

Python

from sklearn.linear_model import LinearRegression

x = [[3, 2, 5, 7],
     [1, 2, 5, 2], 
     [2, 8, 9, 7]]
y = [45, 32, 52]
model = LinearRegression(fit_intercept = True)
model.fit(x, y)
print(model.coef_)
Enter fullscreen mode Exit fullscreen mode

JavaScript

import * as tf from '@tensorflow/tfjs'
import { LinearRegression, setBackend } from 'scikitjs'
setBackend(tf)

let x = [[3, 2, 5, 7],
         [1, 2, 5, 2], 
         [2, 8, 9, 7]]
let y = [45, 32, 52]
let model = new LinearRegression({ fitIntercept: true })
await model.fit(x, y)
console.log(model.coef)
Enter fullscreen mode Exit fullscreen mode

(2) While camel case is used in JavaScript, underscore case are used in Python: you can see this from the fitIntercept (which is a camel case) and fit_intercept (which is an underscore case) attributes of JavaScript and Python respectively. (3) In JavaScript, there is always an await keyword written before calls to the fit method. Python does not await function calls. This can also be seen in the codes above.
(4) JavaScript demands a new keyword when creating objects while Python does not. This is illustrated in the codes above.

See Scikit.js for more details.

2. Mljs

This package may not offer the exact functionalities of Scikit-learn, however it is easy to see some similarities in fitting of models and prediction as illustrated below:

import SimpleLinearRegression from 'ml-regression-simple-linear';

const x = [[3, 2, 5, 7],
           [1, 2, 5, 2], 
           [2, 8, 9, 7]]
const y = [45, 32, 52]
const model = new SimpleLinearRegression(x, y);
console.log(model.coefficients)
Enter fullscreen mode Exit fullscreen mode

See Mljs package for me details.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.