@kanaries/ml
API Reference/Linear Models

Ridge Regression

Use the RidgeRegression JavaScript and TypeScript implementation in @kanaries/ml for regularized linear regression in browser and Node.js applications.

Algorithm overview

Ridge regression is linear regression with L2 regularization. It is useful when features are correlated, coefficients are unstable, or an ordinary least squares model overfits.

JavaScript implementation

@kanaries/ml exposes Linear.RidgeRegression for JavaScript and TypeScript projects. It follows the familiar fit and predict estimator shape and works with numeric arrays in browser or Node.js.

Quick start example

import { Linear } from '@kanaries/ml';

const X = [[0], [1], [2], [3]];
const y = [1, 3, 5, 7];

const model = new Linear.RidgeRegression({ alpha: 0.5, fitIntercept: true });
model.fit(X, y);
const pred = model.predict([[4]]);

Detailed API reference

new Linear.RidgeRegression(props?: {
  alpha?: number;
  fitIntercept?: boolean;
})

Options:

  • alpha?: number, default 1. L2 penalty strength. Must be finite and greater than or equal to 0.
  • fitIntercept?: boolean, default true.

Methods:

  • fit(X: number[][], Y: number[]): void
  • predict(X: number[][]): number[]

predict throws if the model has not been fitted or if the feature count differs from the fitted data.

sklearn-style alias: Ridge

Linear.Ridge extends Linear.RidgeRegression with identical options and methods. Use it when you prefer the scikit-learn class name:

import { Linear } from '@kanaries/ml';

const model = new Linear.Ridge({ alpha: 0.5, fitIntercept: true });
model.fit([[0], [1], [2], [3]], [1, 2, 3, 4]);
const pred = model.predict([[4]]);