@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.

View as Markdown

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.

Interactive Ridge regression playground

Tune the L2 penalty and data noise below. Linear.RidgeRegression refits in the browser after every change, including observations you add directly to the chart.

Live browser model

RidgeRegression playground

Adjust the data and model, then click the chart to add a training observation.

Fitted with @kanaries/ml
-3-2-10123-1.9-0.90.01.01.9feature xtarget y
prediction training holdout your points
Train RMSE0.702
Holdout RMSE0.713
Holdout R²-1.538
Custom points0

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]]);
console.log(pred);

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]]);