@kanaries/ml
API Reference/Serialization

Model Serialization

Save and load trained @kanaries/ml models in browser or Node.js using toJSON(), loadModel(), and fromJSON() with full Pipeline and typed-state support.

View as Markdown

Algorithm overview

In production machine learning workflows, training and inference rarely happen in the same process or at the same time. You might train a classifier or Pipeline in a Node.js build job, store the fitted artifact in object storage or localStorage, and load it later inside a browser tab, Web Worker, or serverless edge function for sub-millisecond inference.

Model serialization converts both an estimator's hyperparameters (params) and its learned internal state (state) into a portable, self-describing JSON payload—and revives it back into a live estimator instance without retraining.

JavaScript implementation

Every contract estimator in @kanaries/ml extends Base.BaseEstimator and ships with built-in JSON serialization—no pickle, binary blobs, or native addons required:

  • model.toJSON() returns a versioned SerializedModel object (format: '@kanaries/ml-model', formatVersion: 1), so JSON.stringify(model) works out of the box.
  • loadModel(json) revives any registered estimator—including nested Pipeline, ColumnTransformer, FeatureUnion, CalibratedClassifierCV, OneVsRestClassifier, and CSRMatrix payloads—from either a JSON string or a parsed object.
  • EstimatorClass.fromJSON(json) provides a type-safe static revival helper that verifies the deserialized instance matches the expected class.

Non-JSON JavaScript types used inside fitted states—such as Float64Array, Int32Array, Map, Set, NaN, Infinity, -Infinity, and -0—are encoded cleanly with tagged envelopes and restored faithfully on load. Prototype-polluting keys (__proto__, constructor, prototype) are explicitly rejected during deserialization.

Quick start example

import { makePipeline, loadModel, Linear, utils } from '@kanaries/ml';

const XTrain = [[1, 10], [2, 12], [8, 2], [9, 1]];
const yTrain = [0, 0, 1, 1];

const pipe = makePipeline(
  new utils.Preprocessing.StandardScaler(),
  new Linear.LogisticRegression()
);
pipe.fit(XTrain, yTrain);

// Serialize to a portable JSON string (works in Node.js and browser)
const payload = JSON.stringify(pipe);

// Revive anywhere (localStorage, IndexedDB, Web Worker, edge function)
const loaded = loadModel(payload) as typeof pipe;
console.log(loaded.predict([[1.5, 11], [8.5, 1.5]]));

Save & Load Models in Python vs JavaScript / TypeScript

If you searched for "Save & Load Models in JavaScript" or "Save & Load Models in TypeScript", this section maps the familiar scikit-learn call to the equivalent @kanaries/ml usage for browser and Node.js runtimes.

Python
scikit-learn
import joblib
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

pipe = make_pipeline(StandardScaler(), LogisticRegression())
pipe.fit(X_train, y_train)

joblib.dump(pipe, "model.joblib")
loaded = joblib.load("model.joblib")
preds = loaded.predict(X_test)
JavaScript / TypeScript
@kanaries/ml
import { makePipeline, loadModel, Linear, utils } from '@kanaries/ml';

const pipe = makePipeline(
new utils.Preprocessing.StandardScaler(),
new Linear.LogisticRegression()
);
pipe.fit(XTrain, yTrain);

const payload = JSON.stringify(pipe);
const loaded = loadModel(payload) as typeof pipe;
const preds = loaded.predict(XTest);

Type-safe revival with fromJSON()

When you know the exact estimator class at the call site, fromJSON() avoids manual type assertions and throws a clear error if the payload belongs to a different estimator:

import { Clusters } from '@kanaries/ml';

const points = [[0, 0], [0, 1], [10, 10], [10, 11]];
const kmeans = new Clusters.KMeans({ n_clusters: 2, random_state: 42 });
kmeans.fitPredict(points);

const json = JSON.stringify(kmeans);
const restored = Clusters.KMeans.fromJSON(json);
console.log(restored.getCentroids(), restored.getInertia());

Detailed API reference

SerializedModel payload schema

interface SerializedModel {
  format: '@kanaries/ml-model';
  formatVersion: 1;
  estimator: string;
  params: unknown;
  state: Record<string, unknown>;
}

Top-level & Base exports

SymbolSignatureDescription
loadModel(json: SerializedModel | string) => BaseEstimatorRevives any serialized @kanaries/ml estimator from a JSON string or parsed SerializedModel. Throws if the format, version, or estimator registry entry is invalid.
model.toJSON()() => SerializedModelSerializes hyperparameters and fitted state into a JSON-safe object. Automatically invoked by JSON.stringify(model).
Class.fromJSON(json: SerializedModel | string) => BaseEstimatorStatic method on BaseEstimator subclasses that calls loadModel(json) and asserts model instanceof Class.
model.clone()() => thisReturns a fresh unfitted copy with identical hyperparameters, recursively cloning nested estimators inside pipelines or meta-estimators.
model.getParams()() => ParamsReturns the estimator's current constructor parameters.
model.setParams(params)(params: Params) => thisMerges updated parameters and resets the estimator to the unfitted state, re-running constructor validation. Throws on unknown parameter names.
Base.registerEstimator(name: string, ctor: AnyCtor) => voidRegisters a custom BaseEstimator subclass so toJSON() and loadModel() can serialize and revive it.
Base.getRegisteredEstimators() => ReadonlyMap<string, AnyCtor>Inspects all estimator names registered in the current runtime.

Production best practices

  1. Import modules before calling loadModel(): If your bundler tree-shakes unused modules, make sure the estimator's module (or @kanaries/ml namespace) is imported before calling loadModel(), otherwise the estimator registry will throw an Unknown estimator error.
  2. Avoid custom function closures in serializable models: Plain numbers, arrays, typed arrays, Map, Set, and nested estimators serialize cleanly, while arbitrary JavaScript functions cannot be serialized to JSON and throw a clear error in encodeValue().
  3. Store models alongside preprocessing: Wrap scalers, encoders, and vectorizers inside a Pipeline before calling JSON.stringify() so inference never drifts from training preprocessing.
  4. Related pages: ML Pipelines, Classical ML in Edge & Web Workers, Migrating a scikit-learn Pipeline to TypeScript, Sparse Data (CSRMatrix).