ML Pipelines
Chain preprocessing, feature extraction, and ML estimators in JavaScript or TypeScript with Pipeline and makePipeline from @kanaries/ml for browser and Node.js.
View as MarkdownAlgorithm overview
A machine learning Pipeline chains one or more preprocessing or feature-transformation steps with a final estimator into a single unified object. Instead of manually calling fit, transform, and predict on separate scalers, vectorizers, and classifiers—and risking subtle train/test data leakage—a pipeline enforces identical step ordering during training, cross-validation, hyperparameter tuning, and inference.
Pipelines are essential whenever your workflow includes:
- Leakage-free cross-validation: scalers, imputers, and TF-IDF vectorizers fit strictly on each training fold and transform the validation fold without peeking at held-out statistics.
- Nested hyperparameter tuning: tune any step's parameter with scikit-learn-style
stepName__paramNamenotation viasetParams(),GridSearchCV, orRandomizedSearchCV. - Single-artifact deployment: serialize an entire preprocessing-plus-model graph with
toJSON()and revive it in browser or Node.js withloadModel(). - Mixed data flows: move seamlessly from raw text (
string[]) throughTfidfVectorizerinto sparseCSRMatrixor densenumber[][]estimators.
JavaScript implementation
@kanaries/ml exports Pipeline, makePipeline, ColumnTransformer, and FeatureUnion at both the top level (import { Pipeline, makePipeline } from '@kanaries/ml') and under the MLPipeline namespace (import { MLPipeline } from '@kanaries/ml').
Because Pipeline extends BaseEstimator and runs in pure TypeScript with zero native bindings, you can train and serve multi-step tabular or text workflows directly inside browser tabs, Web Workers, Node.js APIs, and edge runtimes. TypeScript preserves step tuple types (Pipeline<const TSteps>), so calling transform() on a transformer-only pipeline infers the final step's output type automatically.
Quick start example
import { Pipeline, makePipeline, Linear, utils } from '@kanaries/ml';
const XTrain = [[1, 100], [2, 110], [8, 20], [9, 15]];
const yTrain = [0, 0, 1, 1];
const XTest = [[1.5, 105], [8.5, 18]];
const pipe = new Pipeline({
steps: [
['scaler', new utils.Preprocessing.StandardScaler()],
['clf', new Linear.LogisticRegression({ maxIter: 200 })],
],
});
pipe.fit(XTrain, yTrain);
console.log(pipe.predict(XTest), pipe.predictProba(XTest));
// Update nested step parameters with step__param syntax
pipe.setParams({ clf__maxIter: 400 });
// Shorthand: auto-names steps from lowercase class names
const quick = makePipeline(
new utils.Preprocessing.StandardScaler(),
new Linear.LogisticRegression()
);
quick.fit(XTrain, yTrain);
console.log(quick.predict(XTest));Pipeline & makePipeline in Python vs JavaScript / TypeScript
If you searched for "Pipeline & makePipeline in JavaScript" or "Pipeline & makePipeline in TypeScript", this section maps the familiar scikit-learn call to the equivalent @kanaries/ml usage for browser and Node.js runtimes.
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
pipe = Pipeline([
("scaler", StandardScaler()),
("clf", LogisticRegression()),
])
pipe.fit(X_train, y_train)
preds = pipe.predict(X_test)
pipe.set_params(clf__max_iter=400)
quick = make_pipeline(StandardScaler(), LogisticRegression())import { Pipeline, makePipeline, Linear, utils } from '@kanaries/ml';
const pipe = new Pipeline({
steps: [
['scaler', new utils.Preprocessing.StandardScaler()],
['clf', new Linear.LogisticRegression({ maxIter: 200 })],
],
});
pipe.fit(XTrain, yTrain);
const preds = pipe.predict(XTest);
pipe.setParams({ clf__maxIter: 400 });
const quick = makePipeline(
new utils.Preprocessing.StandardScaler(),
new Linear.LogisticRegression()
);End-to-end text classification pipeline
Pipeline validates input kinds ('text', 'csr', 'dense') between steps. You can pass raw string[] documents directly into a text pipeline:
import { Pipeline, FeatureExtraction, Bayes } from '@kanaries/ml';
const textPipe = new Pipeline({
steps: [
['tfidf', new FeatureExtraction.TfidfVectorizer()],
['nb', new Bayes.MultinomialNB({ alpha: 1.0 })],
],
});
textPipe.fit(
['fast browser machine learning', 'javascript typescript ml', 'quarterly revenue report', 'balance sheet audit'],
[0, 0, 1, 1]
);
console.log(textPipe.predict(['typescript browser analytics']));Detailed API reference
new Pipeline(props)
| Parameter | Type | Default | Description |
|---|---|---|---|
steps | readonly [string, BaseEstimator][] | required | Non-empty ordered list of [name, estimator] pairs. Every intermediate step must implement fit() and transform(). Step names must be unique and must not contain '__'. |
makePipeline(...estimators)
Convenience factory mirroring sklearn.pipeline.make_pipeline. Automatically derives each step's name from estimator.constructor.name.toLowerCase(). If the same estimator class appears multiple times, duplicate names are numbered as name-1, name-2, and so on.
Methods & properties
| Member | Signature | Description |
|---|---|---|
fit | (X: FeatureData, y?: number[], sampleWeight?: number[]) => void | Fits and transforms X sequentially through all intermediate steps, then fits the final estimator (sampleWeight is forwarded to a final predictor). |
predict | (X: FeatureData) => number[] | Transforms X through intermediate steps and calls predict() on the final estimator. |
predictProba | (X: FeatureData) => number[][] | Transforms X through intermediate steps and calls predictProba() on the final estimator. |
transform | (X: FeatureData) => PipelineOutput | Transforms X through every step. Requires the final step to implement transform(). |
fitTransform | (X: FeatureData, y?: number[]) => PipelineOutput | Fits and transforms through every step when the final step is a transformer. |
score | (X: FeatureData, y: number[]) => number | Transforms X through intermediate steps and delegates to the final estimator's score(). |
getStep | (name: string) => BaseEstimator | Returns the estimator registered under name; throws if unknown. |
namedSteps | Record<string, BaseEstimator> | Read-only getter returning an object mapping step names to their estimators. |
getParams | () => Params | Returns { steps } containing the current [name, estimator] pairs. |
setParams | (params: Params) => this | Supports nested stepName__paramName addressing (for example { scaler__withMean: false, clf__C: 10 }) and cascades into nested ColumnTransformer or FeatureUnion steps. |
clone / toJSON | Inherited from BaseEstimator | Deep-clones every step without sharing fitted state, or serializes the entire pipeline for loadModel(). |
Behavior & compatibility notes
- Type checking between steps: If a numeric estimator receives raw
string[]documents without a preceding text vectorizer,Pipelinethrows an explicit error telling you to add a text transformer first. Likewise, if a transformer outputs aCSRMatrixand the final estimator only accepts densenumber[][]inputs,Pipelinerejects the mismatch cleanly. - Combining parallel feature pipelines: Use
ColumnTransformerto apply different transformers to different numeric column indices, orFeatureUnionto concatenate multiple parallel transformations of the same matrix. - Related pages: ColumnTransformer, FeatureUnion, Model Serialization (
toJSON/loadModel), Migrating a scikit-learn Pipeline to TypeScript, Classical ML in Edge & Web Workers.
JavaScript Machine Learning APIs
Browse @kanaries/ml JavaScript and TypeScript ML APIs: clustering, linear models, trees, ensembles, SVM, decomposition, metrics, and preprocessing utilities.
ColumnTransformer
Apply different scalers, encoders, or transformers to specific feature columns in JavaScript and TypeScript with ColumnTransformer from @kanaries/ml.