Preprocessing Utilities
Prepare numeric and categorical features with the @kanaries/ml Preprocessing JavaScript and TypeScript utilities for browser and Node.js machine learning pipelines.
Algorithm overview
Preprocessing utilities convert raw arrays into model-ready features. They cover scaling, normalization, label and categorical encoding, binarization, missing-value imputation, variance filtering, and univariate feature selection.
JavaScript implementation
@kanaries/ml exports these helpers under utils.Preprocessing, so feature preparation can run in the same JavaScript or TypeScript runtime as model training and prediction.
Quick start example
import { utils } from '@kanaries/ml';
const scaler = new utils.Preprocessing.StandardScaler();
const scaled = scaler.fitTransform([[1, 10], [2, 20], [3, 30]]);
const encoder = new utils.Preprocessing.OneHotEncoder({ drop: 'ifBinary' });
const encoded = encoder.fitTransform([
['red', 'S'],
['blue', 'M'],
]);Detailed API reference
Scalers and normalizers
new utils.Preprocessing.StandardScaler({ withMean?: boolean; withStd?: boolean })
new utils.Preprocessing.MinMaxScaler({ featureRange?: [number, number] })
new utils.Preprocessing.MaxAbsScaler()
new utils.Preprocessing.Normalizer({ norm?: 'l1' | 'l2' | 'max' })Scaler methods:
fit(X: number[][]): voidtransform(X: number[][]): number[][]fitTransform(X: number[][]): number[][]inverseTransform(X: number[][]): number[][]forStandardScaler,MinMaxScaler, andMaxAbsScaler
Normalizer supports fit, transform, and fitTransform; fit is a no-op.
Encoders and binarizer
new utils.Preprocessing.LabelEncoder()
new utils.Preprocessing.OrdinalEncoder()
new utils.Preprocessing.OneHotEncoder({ drop?: 'none' | 'first' | 'ifBinary' })
new utils.Preprocessing.Binarizer({ threshold?: number })LabelEncoder works on numeric label arrays. OrdinalEncoder and OneHotEncoder accept categorical matrices containing string, number, boolean, or null values.
Imputation and feature selection
new utils.Preprocessing.SimpleImputer({
strategy?: 'mean' | 'median' | 'mostFrequent' | 'constant';
fillValue?: number;
missingValues?: number | null;
})
new utils.Preprocessing.VarianceThreshold({ threshold?: number })
utils.Preprocessing.fRegression(X: number[][], y: number[]): number[]
new utils.Preprocessing.SelectKBest({ k?: number; scoreFunc?: FeatureScoreFunc })SimpleImputer supports fit, transform, and fitTransform. VarianceThreshold and SelectKBest remove features during transform.
JavaScript deployment notes
- Fit preprocessing on training data only, then reuse the fitted transformer for inference.
- Keep encoder category assumptions stable between browser sessions or Node.js jobs.
- Use
fitTransformfor quick experiments, but keep explicitfitandtransformcalls in production pipelines.
Machine Learning Utilities
Explore machine learning workflow utilities in JavaScript and TypeScript with @kanaries/ml, including preprocessing, sampling, model selection, statistics, and async execution helpers.
Sampling Utilities
Split and sample JavaScript arrays for machine learning workflows with the @kanaries/ml Sampling utilities in browser and Node.js applications.