@kanaries/ml — Machine Learning LibraryJavaScript · TypeScriptscikit-learn-style · MIT

Open source · Browser + Node.js

Machine Learning in JavaScript,
the scikit-learn way.

A lightweight ML library for the browser and Node.js. Familiar fit / predict API, zero-install demos, production-ready builds.

$npm install @kanaries/ml
TypeScript-native~32 kB gzippedTree-shakable ESM11 algorithm families
x₁x₂
Fig. 1 — Binary classification over a two-dimensional feature space. A linear decision boundary (dashed) separates two classes, fit with Linear.LogisticRegression.
Abstract

Bringing the ergonomics of scikit-learn to JavaScript and TypeScript, @kanaries/ml pairs a small, predictable estimator API with tree-shakable ESM builds — so classical machine learning runs natively in the browser, on Node.js servers, and at the edge.

The library favours practical coverage and runnable documentation over breadth for its own sake. Every estimator is typed, every algorithm has a guide, and the same fit/predict code ships from prototype to production.

§ 01Motivation

Why JavaScript for machine learning?


01

Run models in the browser

Ship zero-backend ML experiences with full client-side inference and privacy by default.

02

One language front to back

Build training pipelines and production inference with Node.js and modern frameworks.

03

Interactive ML UIs

Create exploratory dashboards that react instantly without server round-trips.

04

Tiny, tree-shakable bundles

Tree-shakable ESM builds, typed arrays, and Web Worker friendly execution via asyncMode.

§ 02Method

Quick start


Listing 1node.ts
import { Linear, utils } from '@kanaries/ml';

const X = [[5.1, 3.5], [4.9, 3.0], [7.0, 3.2]];
const y = [0, 0, 1];

// standardize features, then fit
const scaler = new utils.Preprocessing.StandardScaler();
const clf = new Linear.LogisticRegression({ maxIter: 200 });
clf.fit(scaler.fitTransform(X), y);

clf.predict([[6.1, 3.1]]); // → [1]
Listing 2index.html — browser via CDN
<script type="module">
  import { Clusters } from
    'https://cdn.skypack.dev/@kanaries/ml';

  const km = new Clusters.KMeans(2);
  km.fitPredict([[1, 1], [1.2, 1.1], [5, 5]]);
  // → [0, 0, 1]
</script>
§ 03Applications

What you can build


a.

Tabular

Client-side churn prediction, lead scoring, and data capture scoring widgets.

b.

Text

Intent detection, TF–IDF pipelines, and simple classifiers entirely in JS.

c.

Clustering & DR

k-means, PCA, and dimensionality reduction for visualization dashboards.

d.

Time series

Prototype forecasts in Node.js with classical regression and smoothing.

§ 04Capabilities

Key features


01

Familiar API

Just like scikit-learn: fit, predict, pipelines, transformers, and metrics.

02

Browser & Node support

ESM-first distribution runs anywhere JavaScript does.

03

Fast numerics

Typed arrays and tight inner loops keep memory low and performance high.

04

Built-in metrics

Accuracy, precision/recall, F1, ROC-AUC, R², and confusion matrices ship in the metrics module.

05

Docs for every algorithm

Runnable guides for each estimator shorten onboarding time.

06

Tiny footprint

Import only what you need with tree-shakable modules.

§ 05Reference

Index of modules


FamilyEstimators & algorithmsReference
Linear & SVMLinear module · LinearRegression · LogisticRegression · SVM module · SVC / NuSVC / LinearSVCView →
Tree & EnsembleTree module · DecisionTreeClassifier · ExtraTreeClassifier · Ensemble module · IsolationForest / AdaBoostView →
Neighbors & ClusteringNeighbors module · KNearestNeighbors · BallTree / KDTree · Clusters module · KMeans · DBSCAN · OPTICS · HDBSCAN · MeanShiftView →
Decomposition & ManifoldDecomposition module · PCA / SparsePCA / TruncatedSVD · Manifold module · MDS / LLE / TSNE / SpectralEmbeddingView →
Bayes, Semi-Supervised & UtilsBayes module · BernoulliNB / CategoricalNB · SemiSupervised module · LabelPropagation / LabelSpreading · Utils module (asyncMode, trainTestSplit, preprocessing, model selection)View →
§ 06Workflow

From data to deployment


STEP 01

Prepare data

Clean and transform with scalers, encoders, and pipeline helpers.

scaler.fitTransform(X)

STEP 02

Fit a model

Use the sklearn-style API to train estimators on typed arrays or plain arrays.

model.fit(X, y)

STEP 03

Deploy anywhere

Run the same fit/predict code in browsers, Node.js, and edge functions.

model.predict(Xnew)

§ 07Notes

Frequently asked


Q1Is JavaScript fast enough for ML?+

Yes—for small to medium datasets and interactive experiences, @kanaries/ml delivers responsive inference. Wrap heavy training or inference in utils.asyncMode to run it off the main thread and keep UIs snappy.

Q2Does it run fully in the browser?+

Absolutely. All supported algorithms can execute in modern browsers and in Node.js environments.

Q3Can I save and load models?+

There is no built-in serialization yet. For now you can re-fit from stored training data, or read a fitted estimator’s parameters and reconstruct it manually.

Q4How close is the API to scikit-learn?+

We follow scikit-learn naming and options wherever practical. Note that some estimators take positional constructor arguments rather than keyword options, so check each algorithm page for the exact signature.

Q5How big is the bundle?+

The full library is about 32 kB gzipped, and each algorithm tree-shakes to just the code you import.

Q6Does it support Web Workers?+

Yes. utils.asyncMode runs a synchronous function in a Web Worker (browser) or worker thread (Node.js) and returns a Promise, so training and inference can stay off the main thread.

§ 08Community

Join the project


Get started

Build ML in JavaScript today

Deploy machine learning models anywhere JavaScript runs with @kanaries/ml.

© ml.kanaries.net — released under MITSet in Fraunces & Newsreader · Built with @kanaries/ml