ColumnTransformer
Apply different scalers, encoders, or transformers to specific feature columns in JavaScript and TypeScript with ColumnTransformer from @kanaries/ml.
View as MarkdownAlgorithm overview
Real-world tabular datasets rarely treat every column the same way. Some columns represent continuous measurements that benefit from z-score standardization, others are bounded ratios that work better with min-max scaling, and some columns are already binary indicators that should pass through unchanged or be dropped.
ColumnTransformer applies distinct transformers to specified subsets of feature columns and horizontally concatenates the resulting blocks into a single numeric feature matrix. When placed inside a Pipeline, every column-specific transformation fits strictly on training data and replays identically during validation and production inference.
JavaScript implementation
ColumnTransformer in @kanaries/ml mirrors sklearn.compose.ColumnTransformer for JavaScript and TypeScript environments using zero-based numeric column indices (number[]). It is exported both at the root (import { ColumnTransformer } from '@kanaries/ml') and under MLPipeline.
Because ColumnTransformer extends BaseEstimator, it supports getParams(), nested name__param updates via setParams(), deep clone(), and full JSON serialization via toJSON() and loadModel().
Quick start example
import { ColumnTransformer, Pipeline, Linear, utils } from '@kanaries/ml';
const XTrain = [
[10, 200, 0.2, 1],
[20, 400, 0.5, 0],
[30, 600, 0.8, 1],
[40, 800, 1.0, 0],
];
const yTrain = [12, 22, 31, 40];
const preprocessor = new ColumnTransformer({
transformers: [
['std', new utils.Preprocessing.StandardScaler(), [0, 1]],
['minmax', new utils.Preprocessing.MinMaxScaler(), [2]],
['keep', 'passthrough', [3]],
],
remainder: 'drop',
});
const pipe = new Pipeline({
steps: [
['prep', preprocessor],
['model', new Linear.RidgeRegression({ alpha: 1.0 })],
],
});
pipe.fit(XTrain, yTrain);
console.log(pipe.predict([[25, 500, 0.6, 1]]));ColumnTransformer in Python vs JavaScript / TypeScript
If you searched for "ColumnTransformer in JavaScript" or "ColumnTransformer in TypeScript", this section maps the familiar scikit-learn call to the equivalent @kanaries/ml usage for browser and Node.js runtimes.
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, MinMaxScaler
ct = ColumnTransformer(
transformers=[
("std", StandardScaler(), [0, 1]),
("minmax", MinMaxScaler(), [2]),
("keep", "passthrough", [3]),
],
remainder="drop",
)
X_transformed = ct.fit_transform(X_train)import { ColumnTransformer, utils } from '@kanaries/ml';
const ct = new ColumnTransformer({
transformers: [
['std', new utils.Preprocessing.StandardScaler(), [0, 1]],
['minmax', new utils.Preprocessing.MinMaxScaler(), [2]],
['keep', 'passthrough', [3]],
],
remainder: 'drop',
});
const XTransformed = ct.fitTransform(XTrain);Detailed API reference
new ColumnTransformer(props)
| Parameter | Type | Default | Description |
|---|---|---|---|
transformers | ColumnTransformerEntry[] | required | Non-empty array of [name, transformer, columns] triples, where transformer is a BaseEstimator implementing fit()/transform(), 'passthrough', or 'drop', and columns is a number[] of non-negative column indices. |
remainder | 'drop' | 'passthrough' | 'drop' | Controls columns not claimed by any entry in transformers. 'drop' discards unclaimed columns; 'passthrough' appends them after all declared transformer blocks in original index order. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | (X: number[][], y?: number[]) => void | Records nFeaturesIn, computes unclaimed remainder indices, and fits each non-string transformer on its column slice. |
transform | (X: number[][]) => number[][] | Transforms each column subset, skips 'drop' entries, appends 'passthrough' remainder columns if enabled, and concatenates blocks horizontally with hstack. |
fitTransform | (X: number[][], y?: number[]) => number[][] | Fits on (X, y) and returns transform(X). |
getTransformer | (name: string) => BaseEstimator | 'passthrough' | 'drop' | Retrieves a configured transformer entry by name; throws if unknown. |
getParams | () => Params | Returns { transformers, remainder } with defensive copies of column index arrays. |
setParams | (params: Params) => this | Supports updating remainder as well as nested transformerName__param (throws if targeting a 'passthrough' or 'drop' entry). |
Behavior & validation details
- Output column order: Output columns always follow the declaration order of
transformers(omitting'drop'entries), withremainder: 'passthrough'columns placed last. - Feature count validation:
transform(X)checks thatX[0].lengthmatches the feature width seen duringfit(), catching schema drift early in browser forms or API payloads. - Name constraints: Transformer names must be unique and cannot contain
'__'so nestedsetParams()keys remain unambiguous inside aPipeline(for exampleprep__std__withMean). - Related pages: ML Pipelines, FeatureUnion, Preprocessing Utilities, Model Serialization.
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.
FeatureUnion
Combine multiple parallel feature transformers horizontally in JavaScript and TypeScript with FeatureUnion from @kanaries/ml for browser and Node.js pipelines.