- Notifications
You must be signed in to change notification settings - Fork13
JavaScript package for predictive data analysis and machine learning
License
javascriptdata/scikit.js
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
TypeScript package for predictive data analysis, data preparation and machine learning.
Aims to be a Typescript port of thescikit-learn python library.
This library is for users who wish to train or deploy their models to JS environments (browser, mobile) but with a familiar API.
Generic math operations are powered byTensorflow.js core layer for faster calculation.
Documentation site:www.scikitjs.org
For use with modern bundlers in a frontend application, simply
npm i @tensorflow/tfjs scikitjs
We depend on the tensorflow library in order to make our calculations fast, but we don't ship it in our bundle.We use it as a peer dependency. General usage is as follows.
import*astffrom'@tensorflow/tfjs'import*asskfrom'scikitjs'sk.setBackend(tf)
This allows us to build a library that can be used in Deno, Node, and the browser with the same configuration.
For Node.js users who wish to bind to the Tensorflow C++ library, simply import the tensorflow C++ version, and use that as the tf library
npm i @tensorflow/tfjs-node scikitjs
consttf=require('@tensorflow/tfjs-node')constsk=require('scikitjs')sk.setBackend(tf)
Note: If you have ESM enabled (by setting type="module" in your package.json), then you can consume this library with import / export, like in the following code block.
import*astffrom'@tensorflow/tfjs-node'import*asskfrom'scikitjs'sk.setBackend(tf)
For those that wish to use script src tags, simply
<scripttype="module">import*astffrom'https://cdn.skypack.dev/@tensorflow/tfjs'import*asskfrom'https://cdn.skypack.dev/scikitjs'sk.setBackend(tf)// or alternatively you can pull the bundle from unpkg// import * as sk from "https://unpkg.com/scikitjs/dist/web index.min.js"</script>
import*astffrom'@tensorflow/tfjs'import{setBackend,LinearRegression}from'scikitjs'setBackend(tf)constlr=newLinearRegression({fitIntercept:false})constX=[[1],[2]]// 2D Matrix with a single column vectorconsty=[10,20]awaitlr.fit(X,y)lr.predict([[3],[4]])// roughly [30, 40]console.log(lr.coef)console.log(lr.intercept)
This library aims to be a drop-in replacement for scikit-learn but for JS environments. There are somedifferences in deploy environment and underlying libraries that make for a slightly different experience.Here are the 3 main differences.
While I would have liked to make every function identical to the python equivalent, it wasn't possible. In python,one has named arguments, meaning that all of these are valid function calls.
defmyAdd(a=0,b=100):returna+bprint(myAdd())# 100print(myAdd(a=10))# 110print(myAdd(b=10))# 10print(myAdd(b=20,a=20))# 40 (order doesn't matter)print(myAdd(50,50))# 100
Javascript doesn't have named parameters, so one must choose between positional arguments, or passing in a single object with all the parameters.
For many classes in scikit-learn, theconstructors take in a ton of arguments with sane defaults, and the user usually only specifies which one they'd like to change. This rules out the positional approach.
After a class is created most function calls really only take in 1 or 2 arguments (think fit, predict, etc). In that case, I'd rather simply pass them positionally. So to recap.
fromsklearn.linear_modelimportLinearRegressionX,y= [[1],[2]], [10,20]lr=LinearRegression(fit_intercept=False)lr.fit(X,y)
Turns into
import*astffrom'@tensorflow/tfjs'import{setBackend,LinearRegression}from'scikitjs'setBackend(tf)letX=[[1],[2]]lety=[10,20]letlr=newLinearRegression({fitIntercept:false})awaitlr.fit(X,y)
You'll also notice in the code above, these are actual classes in JS, so you'll need tonew
them.
Not a huge change, but every function call and variable name that isunderscore_case
in python will simply becamelCase
in JS. In cases where there is an underscore but no word after, it is removed.
fromsklearn.linear_modelimportLinearRegressionX,y= [[1],[2]], [10,20]lr=LinearRegression(fit_intercept=False)lr.fit(X,y)print(lr.coef_)
Turns into
import*astffrom'@tensorflow/tfjs'import{setBackend,LinearRegression}from'scikitjs'setBackend(tf)letX=[[1],[2]]lety=[10,20]letlr=newLinearRegression({fitIntercept:false})awaitlr.fit(X,y)console.log(lr.coef)
In the code sample above, we see thatfit_intercept
turns intofitIntercept
(and it's an object). Andcoef_
turns intocoef
.
It's common practice in Javascript to not tie up the main thread. Many libraries, including tensorflow.js only give an async "fit" function.
So if we build on top of them our fit functions will be asynchronous. But what happens if we make our own estimator that has a synchronous fit function? Should we burden the user with finding out if their fit function is async or not, and then "awaiting" the proper one? I think not.
I think we should simply await all calls to fit. If you await a synchronous function, it resolves immediately and you are on your merry way. So I literally await all calls to .fit and you should too.
fromsklearn.linear_modelimportLogisticRegressionX,y= [[1],[-1]], [1,0]lr=LogisticRegression(fit_intercept=False)lr.fit(X,y)print(lr.coef_)
Turns into
import*astffrom'@tensorflow/tfjs'import{setBackend,LogisticRegression}from'scikitjs'setBackend(tf)letX=[[1],[-1]]lety=[1,0]letlr=newLogisticRegression({fitIntercept:false})awaitlr.fit(X,y)console.log(lr.coef)
Seeguide
About
JavaScript package for predictive data analysis and machine learning
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors12
Uh oh!
There was an error while loading.Please reload this page.