@kanaries/ml
API Reference/Discriminant Analysis

Linear Discriminant Analysis in JS

Run Linear Discriminant Analysis (LDA) classification and supervised projection in JavaScript and TypeScript with @kanaries/ml using SVD or eigen solvers.

View as Markdown

Algorithm overview

Linear Discriminant Analysis (LDA) is both a closed-form generative classifier and a supervised dimensionality-reduction technique. It models each class with a multivariate Gaussian sharing a common within-class covariance matrix, yielding linear decision boundaries without iterative gradient descent.

As a transformer (transform / fitTransform), LDA finds linear combinations of features that maximize the ratio of between-class scatter to within-class scatter—unlike unsupervised PCA, which maximizes total variance regardless of class labels. This makes LDA ideal for visualizing class separation in 1D–2D charts or compressing correlated features inside a Pipeline.

JavaScript implementation

@kanaries/ml provides DiscriminantAnalysis.LinearDiscriminantAnalysis and its alias DiscriminantAnalysis.LDA. It implements scikit-learn's 'svd' solver (default—avoids explicitly forming the covariance matrix, making it robust on high-dimensional or rank-deficient features) and 'eigen' solver (solves the generalized eigenproblem with optional covariance shrinkage in [0, 1]).

Everything runs synchronously in pure TypeScript across browsers and Node.js, and trained LDA models serialize cleanly with toJSON() / loadModel().

Quick start example

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

const XTrain = [
  [-2, -1], [-1.5, -1.2], [-1, -2],
  [1, 1], [1.5, 1.3], [2, 1.1],
  [5, -2], [5.5, -1.8], [6, -2.2],
];
const yTrain = [0, 0, 0, 1, 1, 1, 2, 2, 2];

const lda = new DiscriminantAnalysis.LinearDiscriminantAnalysis({
  solver: 'svd',
  nComponents: 2,
});

const projected = lda.fitTransform(XTrain, yTrain);
const preds = lda.predict([[-1.4, -1.5], [1.2, 1.2], [5.4, -2.0]]);
console.log({ preds, varianceRatio: lda.getExplainedVarianceRatio(), projectedShape: [projected.length, projected[0].length] });

LinearDiscriminantAnalysis (LDA) in Python vs JavaScript / TypeScript

If you searched for "LinearDiscriminantAnalysis (LDA) in JavaScript" or "LinearDiscriminantAnalysis (LDA) in TypeScript", this section maps the familiar scikit-learn call to the equivalent @kanaries/ml usage for browser and Node.js runtimes.

Python
scikit-learn
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

lda = LinearDiscriminantAnalysis(solver="svd", n_components=2)
X_proj = lda.fit_transform(X_train, y_train)
preds = lda.predict(X_test)
proba = lda.predict_proba(X_test)
JavaScript / TypeScript
@kanaries/ml
import { DiscriminantAnalysis } from '@kanaries/ml';

const lda = new DiscriminantAnalysis.LinearDiscriminantAnalysis({
solver: 'svd',
nComponents: 2,
});

const XProj = lda.fitTransform(XTrain, yTrain);
const preds = lda.predict(XTest);
const proba = lda.predictProba(XTest);

Shrinkage LDA for high-dimensional data

When nFeatures approaches nSamples, switch to solver: 'eigen' with shrinkage to regularize the within-class covariance toward a scaled identity matrix:

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

const shrunkLda = new DiscriminantAnalysis.LDA({
  solver: 'eigen',
  shrinkage: 0.2,
});
shrunkLda.fit([[-2, -1], [-1, -2], [2, 1], [1, 2]], [0, 0, 1, 1]);
console.log(shrunkLda.predict([[-1.5, -1.5], [1.5, 1.5]]));

Detailed API reference

new DiscriminantAnalysis.LinearDiscriminantAnalysis(props?)

(Also exported as DiscriminantAnalysis.LDA.)

ParameterTypeDefaultDescription
solver'svd' | 'eigen''svd''svd' avoids forming the covariance matrix and handles rank-deficient features cleanly; 'eigen' solves Sw⁻¹ Sb and supports shrinkage.
shrinkagenumberundefinedShrinkage weight in [0, 1] for within-class covariance. Supported only when solver: 'eigen' (throws with 'svd').
priorsnumber[]empirical class frequenciesCustom class prior probabilities in sorted-class order.
nComponentsnumbermin(nClasses - 1, nFeatures)Number of discriminant components returned by transform() and fitTransform().
tolnumber1e-4Singular-value rank threshold for the 'svd' solver.

Methods

MethodSignatureDescription
fit(X: number[][], y: number[]) => voidFits class means, priors, linear decision weights, and discriminant scalings.
predict(testX: number[][]) => number[]Predicts the highest-posterior class label for each sample.
predictProba(testX: number[][]) => number[][]Computes softmax-normalized class posterior probabilities.
decisionFunction(testX: number[][]) => number[] | number[][]Returns signed binary margins (number[]) for 2 classes or per-class scores (number[][]) for multiclass problems.
transform(X: number[][]) => number[][]Projects X onto the top nComponents discriminant axes.
fitTransform(X: number[][], y: number[]) => number[][]Fits LDA on (X, y) and returns transform(X).
getClasses / getMeans / getPriors() => number[] / number[][]Inspects sorted labels, per-class feature means, and class priors.
getCoef / getIntercept() => number[][] / number[]Returns linear scoring coefficients and intercepts.
getScalings / getExplainedVarianceRatio() => number[][] / number[]Returns discriminant projection vectors and fraction of between-class variance explained per axis.

Compatibility & practical notes

  • Maximum projection dimensions: LDA can extract at most min(nClasses - 1, nFeatures) components—so binary classification produces at most 1 discriminant axis.
  • sklearn note: scikit-learn's 'lsqr' solver is omitted intentionally because it produces the same classifier as 'eigen' without supporting transform().
  • Related pages: Quadratic Discriminant Analysis (QDA), Discriminant Analysis Overview, PCA, ML Pipelines.