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

Algorithm overview

Logistic regression is a classic supervised learning algorithm for binary classification. It models the probability that a sample belongs to class 1 versus class 0, which makes it useful when you need both a final prediction and an interpretable confidence score.

This algorithm works especially well when:

  • you need a strong and explainable baseline for binary 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 binary 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;
}
constructor(props: LogisticRegressionProps = {})

learningRate controls the optimizer step size, and maxIter controls how many optimization rounds the model runs during fitting.

Parameters

  • learningRate: gradient descent step size
  • maxIter: maximum number of training iterations

Methods

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

fit

  • trainX: training features with shape [nSamples, nFeatures]
  • trainY: binary labels for each training sample

predict

  • testX: feature matrix to classify

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.