@kanaries/ml
API Reference/Linear Models

Lasso Regression

Use the LassoRegression JavaScript and TypeScript implementation in @kanaries/ml for sparse regularized linear regression in browser and Node.js workflows.

View as Markdown

Algorithm overview

Lasso regression uses L1 regularization, which can shrink some coefficients to zero. It is useful when you want a linear model that can perform simple feature selection while controlling overfitting.

JavaScript implementation

@kanaries/ml implements Linear.LassoRegression with coordinate-descent style optimization and a JavaScript API for browser or Node.js applications.

Interactive Lasso regression playground

Change the L1 penalty, dataset, and noise to see how regularization changes the fitted trend. The model is trained live with Linear.LassoRegression in your browser.

Live browser model

LassoRegression 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.714
Holdout RMSE0.676
Holdout R²-1.278
Custom points0

Quick start example

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

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

const model = new Linear.LassoRegression({ alpha: 0.1, maxIter: 1000, tol: 1e-6 });
model.fit(X, y);
const pred = model.predict([[4, 0]]);
console.log(pred);

Detailed API reference

new Linear.LassoRegression(props?: {
  alpha?: number;
  fitIntercept?: boolean;
  maxIter?: number;
  tol?: number;
})

Options:

  • alpha?: number, default 1. L1 penalty strength.
  • fitIntercept?: boolean, default true.
  • maxIter?: number, default 1000.
  • tol?: number, default 1e-6.

Methods:

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

sklearn-style alias: Lasso

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

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

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