FeatureUnion
Combine multiple parallel feature transformers horizontally in JavaScript and TypeScript with FeatureUnion from @kanaries/ml for browser and Node.js pipelines.
View as MarkdownAlgorithm 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.
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)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)
| Parameter | Type | Default | Description |
|---|---|---|---|
transformerList | Array<[string, BaseEstimator]> | required | Non-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
| Method | Signature | Description |
|---|---|---|
fit | (X: number[][], y?: number[]) => void | Fits 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) => BaseEstimator | Looks up a branch transformer by name; throws if not found. |
getParams | () => Params | Returns { transformerList } with a shallow copy of the pairs list. |
setParams | (params: Params) => this | Supports nested transformerName__param addressing so branches can be tuned inside grid search. |
FeatureUnion vs ColumnTransformer
| Need | Use |
|---|---|
| Run multiple transformers on the same full matrix and concatenate all outputs | FeatureUnion |
| Route different column indices to different scalers/transformers | ColumnTransformer |
| Run transformers sequentially one after another | Pipeline |
- Related pages: ML Pipelines, ColumnTransformer, Feature Selection, Dimensionality Reduction (PCA).
ColumnTransformer
Apply different scalers, encoders, or transformers to specific feature columns in JavaScript and TypeScript with ColumnTransformer from @kanaries/ml.
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.