@kanaries/ml
API Reference/Linear Models

Logistic Regression

Learn what logistic regression does, when to use it, and how to run logistic regression in JavaScript or TypeScript with @kanaries/ml in the browser or Node.js.

View as Markdown

Algorithm overview

Logistic regression is a classic supervised learning algorithm for binary and multiclass classification. It models class probabilities with a sigmoid for two classes and multinomial softmax for three or more classes, which makes it useful when you need both a final prediction and interpretable linear scores.

This algorithm works especially well when:

  • you need a strong and explainable baseline for binary or multiclass classification
  • you want probability-like outputs for ranking, thresholding, or alerts
  • your decision boundary is approximately linear after feature engineering or scaling

Compared with heavier models, logistic regression is fast to train, easy to inspect, and usually a sensible first model before trying trees, SVMs, or ensembles.

JavaScript implementation

@kanaries/ml provides a JavaScript and TypeScript implementation of logistic regression so you can run classification workflows without leaving the JS ecosystem. That means you can train or serve models in browser applications, Node.js services, and frontend-heavy products that want a scikit-learn-like API without switching to Python at runtime.

This is particularly useful for product decisions, ranking thresholds, and multiclass prediction tasks where teams want coefficients and prediction logic to stay understandable to both engineers and stakeholders.

Quick start

LogisticRegression in Python vs JavaScript / TypeScript

If you searched for "LogisticRegression in JavaScript" or "LogisticRegression 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.linear_model import LogisticRegression

X = [[0, 0], [1, 1], [1, 0], [0, 1]]
y = [0, 1, 1, 0]

clf = LogisticRegression(max_iter=500, random_state=0)
clf.fit(X, y)
pred = clf.predict([[0.9, 0.8], [0.2, 0.1]])
JavaScript / TypeScript
@kanaries/ml
import { Linear } from '@kanaries/ml';

const X = [[0, 0], [1, 1], [1, 0], [0, 1]];
const y = [0, 1, 1, 0];

const clf = new Linear.LogisticRegression({ learningRate: 0.1, maxIter: 800 });
clf.fit(X, y);
const pred = clf.predict([[0.9, 0.8], [0.2, 0.1]]);

Quick JavaScript example

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

const trainX = [[0, 0], [1, 1], [1, 0], [0, 1]];
const trainY = [0, 1, 1, 0];

const model = new Linear.LogisticRegression({
    learningRate: 0.1,
    maxIter: 800,
});

model.fit(trainX, trainY);

const predictions = model.predict([[0.9, 0.8], [0.2, 0.1]]);
console.log(predictions);

Detailed API reference

interface LogisticRegressionProps {
    learningRate?: number;
    maxIter?: number;
    C?: number | null;
}
constructor(props: LogisticRegressionProps = {})

learningRate controls the optimizer step size, maxIter controls how many optimization rounds the model runs, and C is the inverse L2 regularization strength. The default C: null preserves the unregularized behavior from earlier releases.

Parameters

  • learningRate: gradient descent step size
  • maxIter: maximum number of training iterations
  • C: inverse L2 regularization strength; smaller positive values regularize more strongly, while null disables L2 regularization

Methods

  • fit(trainX: number[][], trainY: number[]): void
  • predict(testX: number[][]): number[]
  • predictProba(testX: number[][]): number[][]
  • decisionFunction(testX: number[][]): number[] | number[][]

fit

  • trainX: training features with shape [nSamples, nFeatures]
  • trainY: numeric class labels for each training sample; at least two distinct classes are required

predict

  • testX: feature matrix to classify

predictProba returns columns in sorted class-label order. decisionFunction returns one signed score for a binary fit and one logit column per class for a multiclass fit. The coef getter similarly returns one coefficient vector for binary classification or a matrix with one row per class.

Usage notes

  • Standardize numeric features before training for more stable optimization.
  • Use this model when you want interpretable coefficients and threshold-based decision logic.
  • For browser applications, move large training jobs into a Web Worker to keep the UI responsive.