Skip to content

deepbox/ml

Linear Models

Linear and generalized linear models for regression and classification, including regularized, robust, kernel, Bayesian and SGD-trained variants.

  • Linear estimators

Overview

  • Estimators follow the scikit-learn pattern. fit(X, y) takes X as [nSamples, nFeatures] and y as a 1-D tensor (multi-output targets are not supported). predict before fit throws NotFittedError, and NaN or infinite input throws a typed error. Fitted values are getters without scikit-learn's trailing underscore: model.coef, model.intercept. score is R squared for regressors and accuracy for classifiers, and clone() returns an unfitted copy.
  • Ridge, Lasso and ElasticNet default to alpha: 1.0 (l1Ratio: 0.5 for ElasticNet). Ridge with alpha: 0 on rank-deficient input returns the minimum-norm solution, as scikit-learn does. LinearRegression, Ridge and Lasso accept normalize, which scikit-learn removed.
  • LogisticRegression defaults to penalty: "l2", C: 1.0, solver: "lbfgs" and multiClass: "auto". It has predictProba and accepts classWeight: "balanced" or a map from label to weight.
  • KernelRidge defaults to gamma: 1.0, where scikit-learn uses 1 / nFeatures. Pass gamma explicitly when you compare results.

Types

8

BayesianRidgeOptions

type

Constructor options of BayesianRidge.

  • readonly maxIter?: number

    Maximum number of evidence-maximization iterations (default: 300).

  • readonly tol?: number

    Stop when the summed absolute change of the coefficients between two iterations is below this value (default: 1e-3).

  • readonly fitIntercept?: boolean

    Fit an intercept term (default: true).

  • readonly alphaInit?: number

    Initial noise precision.

  • readonly lambdaInit?: number

    Initial weight precision (default: 1).

  • readonly alpha1?: number

    Shape parameter of the Gamma prior over the noise precision alpha (default: 1e-6).

  • readonly alpha2?: number

    Rate parameter of the Gamma prior over the noise precision alpha (default: 1e-6).

  • readonly lambda1?: number

    Shape parameter of the Gamma prior over the weight precision lambda (default: 1e-6).

  • readonly lambda2?: number

    Rate parameter of the Gamma prior over the weight precision lambda (default: 1e-6).

  • readonly computeScore?: boolean

    Record the log marginal likelihood at every iteration (default: false).

ElasticNetOptions

type

Constructor options of ElasticNet.

  • readonly alpha?: number

    Overall penalty strength, must be >= 0 (default: 1.0).

  • readonly l1Ratio?: number

    Mix between L1 and L2, in [0, 1]; 1 is pure Lasso, 0 is pure Ridge (default: 0.5).

  • readonly fitIntercept?: boolean

    Fit an intercept term (default: true).

  • readonly normalize?: boolean

    Scale each (centered) column to unit L2 norm before fitting (default: false).

  • readonly maxIter?: number

    Maximum number of full passes over the coordinates (default: 1000).

  • readonly tol?: number

    Convergence tolerance (default: 1e-4).

  • readonly warmStart?: boolean

    Start from the coefficients of the previous fit call (default: false).

  • readonly positive?: boolean

    Constrain the coefficients to be non-negative (default: false).

  • readonly selection?: "cyclic" | "random"

    Order in which coordinates are visited: "cyclic" or "random" (default: "cyclic").

  • readonly randomState?: number

    Seed for selection: "random"; the global generator is used when omitted.

HuberRegressorOptions

type

Constructor options of HuberRegressor.

  • readonly epsilon?: number

    Threshold between the quadratic and linear regimes, in units of the scale; must be > 1 (default: 1.35).

  • readonly alpha?: number

    Strength of the squared L2 penalty on the coefficients, must be >= 0 (default: 0.0001).

  • readonly maxIter?: number

    Maximum number of reweighting iterations (default: 100).

  • readonly tol?: number

    Convergence tolerance relative to the standard deviation of y (default: 1e-5).

  • readonly fitIntercept?: boolean

    Fit an intercept term (default: true).

  • readonly learningRate?: numberdeprecated

    Ignored.

IsotonicOutOfBounds

type

How IsotonicRegression.predict treats X outside the training range.

type IsotonicOutOfBounds = "clip" | "nan" | "raise";

IsotonicRegressionOptions

type

Constructor options of IsotonicRegression.

  • readonly increasing?: boolean | "auto"

    true for a non-decreasing fit, false for a non-increasing one, "auto" to pick the direction from the sign of the Spearman correlation between X and y (default: true).

  • readonly outOfBounds?: IsotonicOutOfBounds

    Handling of prediction inputs outside the training range: "clip" uses the nearest fitted value, "nan" returns NaN and "raise" throws (default: "clip").

  • readonly yMin?: number

    Lower bound applied to the fitted values.

  • readonly yMax?: number

    Upper bound applied to the fitted values.

KernelRidgeKernel

type

Kernel functions supported by KernelRidge. "poly" is an alias of "polynomial".

type KernelRidgeKernel = "linear" | "rbf" | "polynomial" | "poly" | "sigmoid" | "laplacian";

KernelRidgeOptions

type

Constructor options of KernelRidge.

  • readonly alpha?: number

    Strength of the L2 penalty, must be >= 0 (default: 1.0).

  • readonly kernel?: KernelRidgeKernel

    Kernel function (default: "rbf").

  • readonly gamma?: number

    Kernel coefficient of rbf, laplacian, polynomial and sigmoid, must be >= 0 (default: 1.0).

  • readonly degree?: number

    Degree of the polynomial kernel, must be >= 0 (default: 3).

  • readonly coef0?: number

    Independent term of the polynomial and sigmoid kernels (default: 1).

LassoOptions

type

Constructor options of Lasso.

  • readonly alpha?: number

    L1 penalty strength, must be >= 0 (default: 1.0).

  • readonly fitIntercept?: boolean

    Fit an intercept term (default: true).

  • readonly normalize?: boolean

    Scale each (centered) column to unit L2 norm before fitting (default: false).

  • readonly maxIter?: number

    Maximum number of full passes over the coordinates (default: 1000).

  • readonly tol?: number

    Convergence tolerance (default: 1e-4).

  • readonly warmStart?: boolean

    Start from the coefficients of the previous fit call (default: false).

  • readonly positive?: boolean

    Constrain the coefficients to be non-negative (default: false).

  • readonly selection?: "cyclic" | "random"

    Order in which coordinates are visited: "cyclic" or "random" (default: "cyclic").

  • readonly randomState?: number

    Seed for selection: "random"; the global generator is used when omitted.

Classes

13

BayesianRidge

Bayesian Ridge Regression with automatic regularization.

Iteratively updates the precision parameters alpha (noise) and lambda (weights) with MacKay's evidence maximization, using Gamma hyper-priors (alpha1, alpha2, lambda1, lambda2). The algorithm, defaults and stopping rule follow sklearn.linear_model.BayesianRidge. Uncertainty is available through the posterior covariance of the weights (BayesianRidge.sigma) and BayesianRidge.predictWithStd.

Each iteration works on the singular value decomposition of the centered design matrix, so the cost per iteration does not depend on the number of samples.

Example
import { BayesianRidge } from 'deepbox/ml';import { tensor } from 'deepbox/ndarray'; const X = tensor([[1], [2], [3], [4], [5]]);const y = tensor([2.1, 3.9, 6.1, 7.9, 10.2]);const reg = new BayesianRidge();reg.fit(X, y);const pred = reg.predict(X);
Members (17)
  • constructor(options?: BayesianRidgeOptions)

    Create a new Bayesian Ridge model.

  • coef: Float64Array

    Posterior mean of the weights, shape (n_features,).

  • coefTensor: Tensor

    Posterior mean of the weights as a float64 tensor of shape (n_features,), the same type as LinearRegression.coef.

  • intercept: number

    Intercept term (0 when fitIntercept is false).

  • alpha: number

    Estimated precision of the noise.

  • lambda: number

    Estimated precision of the weights.

  • nIter: number

    Number of evidence-maximization iterations run by the last fit.

  • sigma: Tensor

    Posterior covariance matrix of the weights, shape (n_features, n_features).

  • scores: Float64Array

    Log marginal likelihood after every iteration plus the final value.

  • nFeaturesIn: number

    Number of features seen during fit.

  • fit(X: Tensor, y: Tensor): this

    Fit the model by evidence maximization.

  • predict(X: Tensor): Tensor

    Predict the posterior mean of the target.

  • predictWithStd(X: Tensor): { mean: Tensor; std: Tensor; }

    Predict the posterior mean together with the standard deviation of the predictive distribution.

  • score(X: Tensor, y: Tensor): number

    Coefficient of determination R² on the given data.

  • getParams(): Record<string, unknown>

    Hyper-parameters of this estimator, with defaults filled in.

  • setParams(params: Record<string, unknown>): this

    Set hyper-parameters.

  • clone(): BayesianRidge

    Create an unfitted copy with the same hyper-parameters.

ElasticNet

Elastic Net Regression (L1 + L2 Regularized Linear Regression).

Elastic Net combines L1 (Lasso) and L2 (Ridge) penalties, controlled by the l1Ratio parameter. It is more stable than Lasso when features are correlated, while still driving some coefficients exactly to zero.

Objective: minimize (1/(2*n)) ||y - Xw||² + α * l1Ratio * ||w||₁ + α * (1-l1Ratio)/2 * ||w||²

- l1Ratio = 1 → pure Lasso (L1 only) - l1Ratio = 0 → pure Ridge (L2 only, with the penalty scaled by 1/n relative to Ridge) - 0 < l1Ratio < 1 → mix of L1 and L2

The solver and its stopping rule (coordinate updates followed by a duality gap check scaled by ||y||²) follow scikit-learn, so results agree with sklearn.linear_model.ElasticNet. With normalize: true every centered column is divided by its L2 norm before fitting and the coefficients are mapped back to the original scale afterwards.

Example
import { ElasticNet } from 'deepbox/ml';import { tensor } from 'deepbox/ndarray'; const X = tensor([[1, 2], [2, 1], [3, 5], [4, 3]]);const y = tensor([1, 2, 3, 5]);const model = new ElasticNet({ alpha: 0.1, l1Ratio: 0.5 });model.fit(X, y);const predictions = model.predict(X);
Members (12)
  • constructor(options?: ElasticNetOptions)

    Create a new Elastic Net model.

  • coef: Tensor

    Coefficients of shape (n_features,) in the original feature scale.

  • intercept: number

    Intercept term (0 when fitIntercept is false).

  • nIter: number | undefined

    Number of coordinate descent passes run by the last fit.

  • dualGap: number | undefined

    Duality gap at the end of the last fit (an upper bound on the suboptimality of the objective, in units of n times the objective).

  • nFeaturesIn: number

    Number of features seen during fit.

  • fit(X: Tensor, y: Tensor): this

    Fit Elastic Net model using coordinate descent.

  • predict(X: Tensor): Tensor

    Predict using the Elastic Net model.

  • score(X: Tensor, y: Tensor): number

    Return the R² score on the given test data.

  • getParams(): Record<string, unknown>

    Hyper-parameters of this estimator, with defaults filled in.

  • setParams(params: Record<string, unknown>): this

    Set hyper-parameters.

  • clone(): ElasticNet

    Create an unfitted copy with the same hyper-parameters.

HuberRegressor

Linear regression with Huber loss, which limits the influence of outliers.

Minimizes, over the coefficients w, the intercept c and a scale sigma, sum_i [ sigma + H(r_i / sigma) * sigma ] + alpha * ||w||^2 with r_i = y_i - x_i . w - c and the Huber function H(z) = z^2 for |z| <= epsilon and 2 epsilon |z| - epsilon^2 otherwise. Because the scale is estimated jointly, epsilon is relative to the noise level and the fit does not depend on the units of y. This is the objective of sklearn.linear_model.HuberRegressor.

The problem is convex. It is solved by iteratively reweighted ridge regression (a majorize-minimize scheme that never increases the objective) alternated with an exact update of the scale.

Example
import { HuberRegressor } from 'deepbox/ml';import { tensor } from 'deepbox/ndarray'; const X = tensor([[1], [2], [3], [4], [100]]);const y = tensor([2, 4, 6, 8, 200]);const reg = new HuberRegressor({ epsilon: 1.35 });reg.fit(X, y);console.log(reg.coef); // close to 2
Members (14)
  • constructor(options?: HuberRegressorOptions)

    Create a new Huber regressor.

  • coef: Float64Array

    Fitted coefficients, shape (n_features,).

  • coefTensor: Tensor

    Fitted coefficients as a float64 tensor of shape (n_features,), the same type as LinearRegression.coef.

  • intercept: number

    Intercept term (0 when fitIntercept is false).

  • nIter: number

    Number of reweighting iterations run by the last fit.

  • scale: number

    Estimated scale of the residuals.

  • outliers: boolean[]

    For every training sample, whether its absolute residual exceeds epsilon * scale.

  • nFeaturesIn: number

    Number of features seen during fit.

  • fit(X: Tensor, y: Tensor): this

    Fit the model.

  • predict(X: Tensor): Tensor

    Predict target values.

  • score(X: Tensor, y: Tensor): number

    Coefficient of determination R² on the given data.

  • getParams(): Record<string, unknown>

    Hyper-parameters of this estimator, with defaults filled in.

  • setParams(params: Record<string, unknown>): this

    Set hyper-parameters.

  • clone(): HuberRegressor

    Create an unfitted copy with the same hyper-parameters.

IsotonicRegression

Isotonic Regression.

Fits a non-decreasing (or non-increasing) function to the data using the pool adjacent violators algorithm (PAVA) and predicts by linear interpolation between the fitted breakpoints, like sklearn.isotonic.IsotonicRegression. Samples with the same X are merged into their weighted mean before pooling.

Unlike most regressors, IsotonicRegression takes 1-D X and y (a single-column 2-D X is also accepted).

Example
import { IsotonicRegression } from 'deepbox/ml';import { tensor } from 'deepbox/ndarray'; const X = tensor([1, 2, 3, 4, 5]);const y = tensor([1, 3, 2, 4, 5]);const model = new IsotonicRegression({ increasing: true });model.fit(X, y);const predictions = model.predict(tensor([1.5, 2.5]));
Members (10)
  • constructor(options?: IsotonicRegressionOptions)

    Create a new isotonic regression model.

  • xThresholds: Float64Array

    Breakpoints of the fitted function (strictly increasing).

  • yThresholds: Float64Array

    Fitted values at IsotonicRegression.xThresholds.

  • increasing: boolean

    Direction used by the last fit (resolves increasing: "auto").

  • fit(X: Tensor, y: Tensor, sampleWeightArg?: Tensor | void): this

    Fit the isotonic function.

  • predict(X: Tensor): Tensor

    Evaluate the fitted function.

  • score(X: Tensor, y: Tensor): number

    Coefficient of determination R² of the predictions.

  • getParams(): Record<string, unknown>

    Hyper-parameters of this estimator.

  • setParams(params: Record<string, unknown>): this

    Set hyper-parameters.

  • clone(): IsotonicRegression

    Create an unfitted copy with the same hyper-parameters.

KernelRidge

Kernel Ridge Regression.

Combines Ridge Regression (L2 penalty) with the kernel trick, learning a non-linear function in the feature space induced by the kernel. There is no intercept term; center y beforehand if you need one.

Solves: (K + alpha * I) * dual_coef = y

and predicts K(X_test, X_train) @ dual_coef. The defaults differ from scikit-learn's (kernel: "rbf" and gamma: 1.0 here, "linear" and 1 / n_features there), so pass them explicitly when comparing the two.

Example
import { KernelRidge } from 'deepbox/ml';import { tensor } from 'deepbox/ndarray'; const X_train = tensor([[0], [1], [2], [3]]);const y_train = tensor([0, 1, 4, 9]);const model = new KernelRidge({ alpha: 1.0, kernel: 'rbf', gamma: 0.1 });model.fit(X_train, y_train);const predictions = model.predict(tensor([[1.5]]));
Members (9)
  • constructor(options?: KernelRidgeOptions)

    Create a new kernel ridge model.

  • dualCoef: Float64Array

    Dual coefficients (one per training sample).

  • nFeaturesIn: number

    Number of features seen during fit.

  • fit(X: Tensor, y: Tensor): this

    Fit the model by solving (K + alpha I) c = y.

  • predict(X: Tensor): Tensor

    Predict target values.

  • score(X: Tensor, y: Tensor): number

    Coefficient of determination R² of the predictions.

  • getParams(): Record<string, unknown>

    Hyper-parameters of this estimator.

  • setParams(params: Record<string, unknown>): this

    Set hyper-parameters.

  • clone(): KernelRidge

    Create an unfitted copy with the same hyper-parameters.

Lasso

Lasso Regression (L1 Regularized Linear Regression).

Lasso performs both regularization and feature selection by adding an L1 penalty that can drive coefficients exactly to zero.

Objective: minimize (1/(2*n)) ||y - Xw||² + α ||w||₁

The solver and its stopping rule (coordinate updates followed by a duality gap check scaled by ||y||²) follow scikit-learn, so results agree with sklearn.linear_model.Lasso.

Example
import { Lasso } from 'deepbox/ml';import { tensor } from 'deepbox/ndarray'; const X = tensor([[0, 1], [1, 0], [2, 1], [3, 5]]);const y = tensor([1, 3, 5, 7]);const model = new Lasso({ alpha: 0.1, maxIter: 1000 });model.fit(X, y); // Many coefficients will be exactly 0console.log(model.coef); const predictions = model.predict(X);
Members (12)
  • constructor(options?: LassoOptions)

    Create a new Lasso Regression model.

  • coef: Tensor

    Get the model coefficients (weights).

  • intercept: number

    Get the intercept (bias term).

  • nIter: number | undefined

    Get the number of passes run by coordinate descent in the last fit.

  • dualGap: number | undefined

    Duality gap at the end of the last fit.

  • nFeaturesIn: number

    Number of features seen during fit.

  • fit(X: Tensor, y: Tensor): this

    Fit Lasso regression model using Coordinate Descent.

  • predict(X: Tensor): Tensor

    Predict using the Lasso regression model.

  • score(X: Tensor, y: Tensor): number

    Return the R² score on the given test data and target values.

  • getParams(): Record<string, unknown>

    Get hyperparameters for this estimator, with defaults filled in.

  • setParams(params: Record<string, unknown>): this

    Set the parameters of this estimator.

  • clone(): Lasso

    Create an unfitted copy with the same hyper-parameters.

LinearRegression

Ordinary Least Squares Linear Regression.

Fits a linear model with coefficients w = (w1, ..., wp) to minimize the residual sum of squares between the observed targets and the targets predicted by the linear approximation. The system is solved with an SVD-based least squares solver, so rank-deficient and under-determined problems return the minimum-norm solution (as numpy.linalg.lstsq does).

Example
import { LinearRegression } from 'deepbox/ml';import { tensor } from 'deepbox/ndarray'; // Create training dataconst X = tensor([[1, 1], [1, 2], [2, 2], [2, 3]]);const y = tensor([1, 2, 2, 3]); // Fit modelconst model = new LinearRegression({ fitIntercept: true });model.fit(X, y); // Make predictionsconst X_test = tensor([[3, 5]]);const predictions = model.predict(X_test); // Get R^2 scoreconst score = model.score(X, y);
Members (13)
  • constructor(options?: { readonly fitIntercept?: boolean; readonly normalize?: boolean; readonly copyX?: boolean; })

    Create a new Linear Regression model.

  • coef: Tensor

    Get the model coefficients (weights).

  • intercept: Tensor | undefined

    Get the intercept (bias term).

  • interceptValue: number

    The intercept as a plain number, the same type the other linear models return from intercept.

  • rank: number

    Effective rank of the design matrix used by the last fit.

  • singular: Float64Array

    Singular values of the design matrix used by the last fit, in descending order.

  • nFeaturesIn: number

    Number of features seen during fit.

  • fit(X: Tensor, y: Tensor): this

    Fit linear model using Ordinary Least Squares.

  • predict(X: Tensor): Tensor

    Predict using the linear model.

  • score(X: Tensor, y: Tensor): number

    Return the coefficient of determination R^2 of the prediction.

  • getParams(): Record<string, unknown>

    Get parameters for this estimator.

  • setParams(params: Record<string, unknown>): this

    Set the parameters of this estimator.

  • clone(): LinearRegression

    Create an unfitted copy with the same parameters.

LogisticRegression

Logistic Regression (Binary and Multiclass Classification).

Minimizes the regularized negative log-likelihood. With sample weights s_i (from classWeight) the objective is, up to a constant factor,

C * sum_i s_i * logloss_i + penalty(w)

where penalty is 0.5 * ||w||_2^2 ('l2'), ||w||_1 ('l1') or zero ('none'). This is scikit-learn's objective; the intercept is never penalized. L2 and unpenalized problems are solved with L-BFGS, L1 problems with an accelerated proximal gradient method. The solver option is accepted for scikit-learn compatibility and checked against the penalty by the constructor, but every solver minimizes the same objective and reaches the same solution. (scikit-learn's liblinear also penalizes the intercept, so its results differ slightly; here the intercept is never penalized.)

With more than two classes, multiClass: 'multinomial' fits a softmax model and 'ovr' fits one binary model per class and normalizes their probabilities. 'auto' (the default) uses 'multinomial', except with solver: 'liblinear', which uses 'ovr'. Binary problems use a single logistic model in every mode. In a one-vs-rest fit, classWeight: 'balanced' balances each binary problem (class k against the rest), while a weight dictionary applies the weight of each sample's original class.

Example
import { LogisticRegression } from 'deepbox/ml'; // Binary classificationconst model = new LogisticRegression({ C: 1.0, maxIter: 100 });model.fit(X_train, y_train); const predictions = model.predict(X_test);const probabilities = model.predictProba(X_test); // Multiclass classificationconst multiModel = new LogisticRegression({ multiClass: 'multinomial' });multiModel.fit(X_train_multi, y_train_multi);
Members (13)
  • constructor(options?: { readonly penalty?: Penalty; readonly tol?: number; readonly C?: number; readonly fitIntercept?: boolean; readonly maxIter?: number; readonly learningRate?: number; readonly multiClass?: MultiClass; readonly classWeight?: ClassWeight; readonly solver?: Solver; })

    Create a new Logistic Regression classifier.

  • classes: Tensor | undefined

    Class labels seen during fit, sorted ascending.

  • coef: Tensor

    Get the model coefficients (weights).

  • intercept: number | number[]

    Get the intercept (bias term).

  • nIter: number

    Number of solver iterations of the last fit (the maximum over the binary problems for one-vs-rest).

  • fit(X: Tensor, y: Tensor): this

    Fit logistic regression model.

  • decisionFunction(X: Tensor): Tensor

    Raw scores X @ coef + intercept for each sample.

  • predict(X: Tensor): Tensor

    Predict class labels.

  • predictProba(X: Tensor): Tensor

    Predict class probabilities for samples.

  • score(X: Tensor, y: Tensor): number

    Return the mean accuracy on the given test data and labels.

  • getParams(): Record<string, unknown>

    Get hyperparameters for this estimator.

  • setParams(params: Record<string, unknown>): this

    Set the parameters of this estimator.

  • clone(): LogisticRegression

    Create an unfitted copy of this estimator with the same parameters.

QuantileRegressor

Quantile Regressor.

Fits a linear model of the conditional quantile of y by minimizing

(1 / n) * sum_i rho_q(y_i - x_i . w - b) + alpha * ||w||_1

where rho_q(r) = q * max(r, 0) + (1 - q) * max(-r, 0) is the pinball loss. The intercept b is never penalized. This is the objective used by scikit-learn's QuantileRegressor; note that its default alpha = 1.0 is a strong L1 penalty, so pass alpha: 0 for an unpenalized fit.

Example
import { QuantileRegressor } from 'deepbox/ml';import { tensor } from 'deepbox/ndarray'; const X = tensor([[1], [2], [3], [4], [5]]);const y = tensor([1.2, 1.9, 3.4, 3.9, 5.8]);const median = new QuantileRegressor({ quantile: 0.5, alpha: 0 }).fit(X, y);const upper = new QuantileRegressor({ quantile: 0.9, alpha: 0 }).fit(X, y);
Members (11)
  • constructor(options?: { readonly quantile?: number; readonly alpha?: number; readonly fitIntercept?: boolean; readonly maxIter?: number; readonly tol?: number; })

    Create a new Quantile Regressor.

  • coef: Float64Array

    Fitted coefficients, one per feature.

  • coefTensor: Tensor

    Fitted coefficients as a float64 tensor of shape (n_features,).

  • intercept: number

    Fitted intercept (0 when fitIntercept is false).

  • nIter: number

    Number of interior point iterations used by the last fit.

  • fit(X: Tensor, y: Tensor): this

    Fit the model.

  • predict(X: Tensor): Tensor

    Predict the conditional quantile for each sample.

  • score(X: Tensor, y: Tensor): number

    Coefficient of determination R² of the prediction.

  • getParams(): Record<string, unknown>

    Get the hyperparameters of this estimator.

  • setParams(params: Record<string, unknown>): this

    Set hyperparameters.

  • clone(): QuantileRegressor

    Create an unfitted copy of this estimator with the same parameters.

RANSACRegressor

RANSAC (Random Sample Consensus) Regressor.

Each trial draws minSamples rows at random, fits a clone of the base estimator, and counts the samples whose loss is at most residualThreshold. The trial with the most inliers wins (ties go to the higher R² on the inliers). The final model is the base estimator refitted on the winning inliers.

Fitting stops early once stopNInliers or stopScore is reached, or once enough trials have run to have drawn an outlier-free subset with probability stopProbability.

Example
import { RANSACRegressor } from 'deepbox/ml';import { tensor } from 'deepbox/ndarray'; const X = tensor([[1], [2], [3], [4], [5], [6]]);const y = tensor([2, 4, 6, 8, 10, 90]); // last point is an outlierconst model = new RANSACRegressor({ minSamples: 2, randomState: 0 }).fit(X, y);console.log(model.inlierMask); // Uint8Array [1, 1, 1, 1, 1, 0]
Members (10)
  • constructor(options?: RANSACOptions)

    Create a new RANSAC regressor.

  • inlierMask: Uint8Array

    Mask over the training samples: 1 for inliers of the best model, 0 for outliers.

  • estimator: Regressor

    The base estimator refitted on the inliers.

  • nTrials: number

    Number of random trials run by the last fit.

  • fit(X: Tensor, y: Tensor): this

    Fit the model with RANSAC.

  • predict(X: Tensor): Tensor

    Predict with the base estimator refitted on the inliers.

  • score(X: Tensor, y: Tensor): number

    Coefficient of determination R² of the prediction.

  • getParams(): Record<string, unknown>

    Get the hyperparameters of this estimator.

  • setParams(params: Record<string, unknown>): this

    Set hyperparameters.

  • clone(): RANSACRegressor

    Create an unfitted copy of this estimator with the same parameters.

Ridge

Ridge Regression (L2 Regularized Linear Regression).

Minimizes ||y - Xw||^2 + alpha * ||w||^2. The intercept is never penalized: when fitIntercept is true, X and y are centered before solving and the intercept is recovered from the column means.

The model mirrors scikit-learn's Ridge, with one exception: normalize (removed from scikit-learn 1.2) divides every centered column by its L2 norm before the penalty is applied, and the coefficients are mapped back to the original feature scale afterwards.

Example
import { Ridge } from 'deepbox/ml'; const model = new Ridge({ alpha: 0.5 });model.fit(X_train, y_train);const predictions = model.predict(X_test);
Members (10)
  • constructor(options?: { readonly alpha?: number; readonly fitIntercept?: boolean; readonly normalize?: boolean; readonly solver?: RidgeSolver; readonly maxIter?: number; readonly tol?: number; })

    Create a new Ridge Regression model.

  • coef: Tensor

    Get the model coefficients (weights).

  • intercept: number

    Get the intercept (bias term).

  • nIter: number | undefined

    Get the number of iterations run by the solver.

  • fit(X: Tensor, y: Tensor): this

    Fit Ridge regression model.

  • predict(X: Tensor): Tensor

    Predict using the Ridge regression model.

  • score(X: Tensor, y: Tensor): number

    Return the coefficient of determination R² of the prediction.

  • getParams(): Record<string, unknown>

    Get parameters for this estimator.

  • setParams(params: Record<string, unknown>): this

    Set the parameters of this estimator.

  • clone(): Ridge

    Create an unfitted copy of this estimator with the same parameters.

SGDClassifier

SGD Classifier: linear classifiers trained with stochastic gradient descent.

Supports multiple loss functions: - "hinge": Linear SVM (default) - "log_loss": Logistic regression - "modified_huber": Smoothed hinge loss with probability estimates - "squared_hinge": Squared hinge loss

Models with more than two classes are trained one-vs-rest. The step size follows learningRate ('optimal' by default, which needs alpha > 0). Feature scaling matters for SGD; standardize the inputs first.

Example
import { SGDClassifier } from 'deepbox/ml'; const clf = new SGDClassifier({ loss: 'log_loss', alpha: 0.0001 });clf.fit(X_train, y_train);const preds = clf.predict(X_test);
Members (16)
  • constructor(options?: SGDBaseOptions & { readonly loss?: Loss; readonly classWeight?: ClassWeight; })

    Create a new SGD classifier.

  • classes: Tensor | undefined

    Class labels seen during fit, sorted ascending.

  • coef: Float64Array

    Fitted coefficients as a flat array: n_features values for two classes, n_classes * n_features values (row-major, one row per class) otherwise.

  • coefTensor: Tensor

    Fitted coefficients as a float64 tensor: shape (n_features,) for two classes, (n_classes, n_features) for more.

  • intercept: number

    Fitted intercept of a binary model (0 when fitIntercept is false).

  • interceptArray: number[]

    Fitted intercepts, one per one-vs-rest model (a single value for two classes).

  • nIter: number

    Number of epochs run by the last fit (the maximum over the one-vs-rest models).

  • fit(X: Tensor, y: Tensor): this

    Fit the classifier.

  • partialFit(X: Tensor, y: Tensor, classes?: Tensor | readonly number[]): this

    Run one epoch of SGD on a batch, continuing from the current weights.

  • decisionFunction(X: Tensor): Tensor

    Signed distance of each sample to the separating hyperplane(s).

  • predict(X: Tensor): Tensor

    Predict class labels.

  • predictProba(X: Tensor): Tensor

    Predict class probabilities.

  • score(X: Tensor, y: Tensor): number

    Mean accuracy on the given data and labels.

  • getParams(): Record<string, unknown>

    Get the hyperparameters of this estimator.

  • setParams(params: Record<string, unknown>): this

    Set hyperparameters.

  • clone(): SGDClassifier

    Create an unfitted copy of this estimator with the same parameters.

SGDRegressor

SGD Regressor: linear regressor trained with stochastic gradient descent.

Supports multiple loss functions: - "squared_error": Ordinary least squares (default) - "huber": Huber loss (less sensitive to outliers) - "epsilon_insensitive": Support vector regression loss

The default schedule is 'invscaling' with eta0 = 0.01. Feature and target scaling matter for SGD; standardize the inputs first.

Example
import { SGDRegressor } from 'deepbox/ml'; const reg = new SGDRegressor({ loss: 'huber', alpha: 0.0001 });reg.fit(X_train, y_train);const preds = reg.predict(X_test);
Members (12)
  • constructor(options?: SGDBaseOptions & { readonly loss?: RegressionLoss; readonly epsilon?: number; })

    Create a new SGD regressor.

  • coef: Float64Array

    Fitted coefficients, one per feature.

  • coefTensor: Tensor

    Fitted coefficients as a float64 tensor of shape (n_features,).

  • intercept: number

    Fitted intercept (0 when fitIntercept is false).

  • nIter: number

    Number of epochs run by the last fit.

  • fit(X: Tensor, y: Tensor): this

    Fit the regressor.

  • partialFit(X: Tensor, y: Tensor): this

    Run one epoch of SGD on a batch, continuing from the current weights.

  • predict(X: Tensor): Tensor

    Predict target values.

  • score(X: Tensor, y: Tensor): number

    Coefficient of determination R² of the prediction.

  • getParams(): Record<string, unknown>

    Get the hyperparameters of this estimator.

  • setParams(params: Record<string, unknown>): this

    Set hyperparameters.

  • clone(): SGDRegressor

    Create an unfitted copy of this estimator with the same parameters.

ml-linear.ts
import {  ElasticNet,  LinearRegression,  LogisticRegression,  Ridge,} from "deepbox/ml";import { tensor } from "deepbox/ndarray"; const X = tensor([[1], [2], [3], [4]]);const y = tensor([2, 4, 6, 8]); const model = new LinearRegression().fit(X, y);console.log(model.coef.toString(), model.score(X, y));console.log(model.predict(tensor([[5]])).toString());console.log(new Ridge({ alpha: 1 }).fit(X, y).predict(tensor([[5]])).toString());console.log(new ElasticNet({ alpha: 0.1, l1Ratio: 0.5 }).fit(X, y).predict(tensor([[5]])).toString()); const classes = tensor([0, 0, 1, 1]);const clf = new LogisticRegression({ C: 1 }).fit(tensor([[0], [1], [2], [3]]), classes);console.log(clf.predict(tensor([[1.5]])).toString());console.log(clf.predictProba(tensor([[1.5]])).toString());