@kanaries/ml
API Reference/Pipeline

FeatureUnion

Combine multiple parallel feature transformers horizontally in JavaScript and TypeScript with FeatureUnion from @kanaries/ml for browser and Node.js pipelines.

View as Markdown

Algorithm overview

Sometimes a single feature representation is not enough for a downstream model: you may want both PCA components and univariate selected features, or both raw scaled features and random projections fed side by side into a classifier.

FeatureUnion runs several transformers in parallel on the same input matrix X and concatenates their outputs column-wise (hstack) into one wider feature matrix. Unlike ColumnTransformer, which routes disjoint column subsets to different transformers, FeatureUnion passes the full matrix to every branch and merges all generated representations.

JavaScript implementation

@kanaries/ml exports FeatureUnion at the root (import { FeatureUnion } from '@kanaries/ml') and under MLPipeline. It extends BaseEstimator, integrates cleanly inside Pipeline, supports name__param parameter routing in setParams(), and serializes cleanly via toJSON() / loadModel().

In browser and Node.js applications, FeatureUnion keeps multi-view feature engineering deterministic and declarative—avoiding manual array slicing and concatenation bugs when serving predictions.

Quick start example

import { FeatureUnion, Pipeline, Decomposition, Linear, utils } from '@kanaries/ml';

const XTrain = [
  [1, 0, 5, 2],
  [2, 1, 6, 3],
  [8, 9, 1, 7],
  [9, 8, 2, 8],
];
const yTrain = [0, 0, 1, 1];

const union = new FeatureUnion({
  transformerList: [
    ['scaled', new utils.Preprocessing.StandardScaler()],
    ['pca', new Decomposition.PCA({ nComponents: 2 })],
  ],
});

const pipe = new Pipeline({
  steps: [
    ['features', union],
    ['clf', new Linear.LogisticRegression()],
  ],
});

pipe.fit(XTrain, yTrain);
console.log(pipe.predict([[1.5, 0.5, 5.5, 2.5], [8.5, 8.5, 1.5, 7.5]]));

FeatureUnion in Python vs JavaScript / TypeScript

If you searched for "FeatureUnion in JavaScript" or "FeatureUnion 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
from sklearn.pipeline import FeatureUnion, Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression

union = FeatureUnion([
  ("scaled", StandardScaler()),
  ("pca", PCA(n_components=2)),
])

pipe = Pipeline([
  ("features", union),
  ("clf", LogisticRegression()),
])
pipe.fit(X_train, y_train)
JavaScript / TypeScript
@kanaries/ml
import { FeatureUnion, Pipeline, Decomposition, Linear, utils } from '@kanaries/ml';

const union = new FeatureUnion({
transformerList: [
  ['scaled', new utils.Preprocessing.StandardScaler()],
  ['pca', new Decomposition.PCA({ nComponents: 2 })],
],
});

const pipe = new Pipeline({
steps: [
  ['features', union],
  ['clf', new Linear.LogisticRegression()],
],
});
pipe.fit(XTrain, yTrain);

Detailed API reference

new FeatureUnion(props)

ParameterTypeDefaultDescription
transformerListArray<[string, BaseEstimator]>requiredNon-empty list of [name, transformer] pairs. Each transformer must extend BaseEstimator and implement fit() and transform(). Names must be unique and must not contain '__'.

Methods

MethodSignatureDescription
fit(X: number[][], y?: number[]) => voidFits every transformer in transformerList on (X, y).
transform(X: number[][]) => number[][]Transforms X with each branch and concatenates outputs horizontally into [nRows][sumOfCols].
fitTransform(X: number[][], y?: number[]) => number[][]Calls fit(X, y) followed by transform(X).
getTransformer(name: string) => BaseEstimatorLooks up a branch transformer by name; throws if not found.
getParams() => ParamsReturns { transformerList } with a shallow copy of the pairs list.
setParams(params: Params) => thisSupports nested transformerName__param addressing so branches can be tuned inside grid search.

FeatureUnion vs ColumnTransformer

NeedUse
Run multiple transformers on the same full matrix and concatenate all outputsFeatureUnion
Route different column indices to different scalers/transformersColumnTransformer
Run transformers sequentially one after anotherPipeline