Overview
StandardScaler transforms features to have zero mean and unit variance. This is a common requirement for many machine learning algorithms.
Constructor
No parameters required.
Properties
Mean value for each feature in the training set. Set during fit().
Standard deviation for each feature in the training set. Set during fit().
Methods
fit
Compute the mean and standard deviation to be used for later scaling.
Training data matrix where each row is a sample and each column is a feature.
Returns: this - The fitted scaler instance.
transform(X: Matrix): Matrix
Perform standardization by centering and scaling.
Data matrix to transform.
Returns: Matrix - Transformed data with zero mean and unit variance.
fitTransform(X: Matrix): Matrix
Fit to data, then transform it. Equivalent to calling fit(X).transform(X).
Training data matrix to fit and transform.
Returns: Matrix - Transformed data.
inverseTransform(X: Matrix): Matrix
Scale back the data to the original representation.
Transformed data matrix to inverse transform.
Returns: Matrix - Original scale data.
Example
import { StandardScaler } from 'bun-scikit';
// Training data
const X = [
[1, 2],
[2, 4],
[3, 6],
[4, 8]
];
// Create and fit scaler
const scaler = new StandardScaler();
scaler.fit(X);
console.log('Mean:', scaler.mean_);
// Output: Mean: [2.5, 5]
console.log('Scale:', scaler.scale_);
// Output: Scale: [1.118..., 2.236...]
// Transform data
const XScaled = scaler.transform(X);
console.log('Scaled:', XScaled);
// Output: Scaled: [[-1.34, -1.34], [-0.45, -0.45], [0.45, 0.45], [1.34, 1.34]]
// Or use fit_transform pattern
const scaler2 = new StandardScaler();
const XScaled2 = scaler2.fitTransform(X);
// Inverse transform
const XOriginal = scaler.inverseTransform(XScaled);
console.log('Original:', XOriginal);
// Output: Original: [[1, 2], [2, 4], [3, 6], [4, 8]]
Notes
- Features with zero variance are scaled to 1 to avoid division by zero
- The scaler must be fitted before calling
transform() or inverseTransform()
- Input data must be finite (no NaN or Infinity values)