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)takesXas[nSamples, nFeatures]andyas a 1-D tensor (multi-output targets are not supported).predictbeforefitthrowsNotFittedError, and NaN or infinite input throws a typed error. Fitted values are getters without scikit-learn's trailing underscore:model.coef,model.intercept.scoreis R squared for regressors and accuracy for classifiers, andclone()returns an unfitted copy. Ridge,LassoandElasticNetdefault toalpha: 1.0(l1Ratio: 0.5forElasticNet).Ridgewithalpha: 0on rank-deficient input returns the minimum-norm solution, as scikit-learn does.LinearRegression,RidgeandLassoacceptnormalize, which scikit-learn removed.LogisticRegressiondefaults topenalty: "l2",C: 1.0,solver: "lbfgs"andmultiClass: "auto". It haspredictProbaand acceptsclassWeight: "balanced"or a map from label to weight.KernelRidgedefaults togamma: 1.0, where scikit-learn uses1 / nFeatures. Passgammaexplicitly when you compare results.
BayesianRidgeOptions
typeConstructor options of BayesianRidge.
readonly maxIter?: numberMaximum number of evidence-maximization iterations (default: 300).
readonly tol?: numberStop when the summed absolute change of the coefficients between two iterations is below this value (default: 1e-3).
readonly fitIntercept?: booleanFit an intercept term (default: true).
readonly alphaInit?: numberInitial noise precision.
readonly lambdaInit?: numberInitial weight precision (default: 1).
readonly alpha1?: numberShape parameter of the Gamma prior over the noise precision alpha (default: 1e-6).
readonly alpha2?: numberRate parameter of the Gamma prior over the noise precision alpha (default: 1e-6).
readonly lambda1?: numberShape parameter of the Gamma prior over the weight precision lambda (default: 1e-6).
readonly lambda2?: numberRate parameter of the Gamma prior over the weight precision lambda (default: 1e-6).
readonly computeScore?: booleanRecord the log marginal likelihood at every iteration (default: false).
ElasticNetOptions
typeConstructor options of ElasticNet.
readonly alpha?: numberOverall penalty strength, must be >= 0 (default: 1.0).
readonly l1Ratio?: numberMix between L1 and L2, in [0, 1]; 1 is pure Lasso, 0 is pure Ridge (default: 0.5).
readonly fitIntercept?: booleanFit an intercept term (default: true).
readonly normalize?: booleanScale each (centered) column to unit L2 norm before fitting (default: false).
readonly maxIter?: numberMaximum number of full passes over the coordinates (default: 1000).
readonly tol?: numberConvergence tolerance (default: 1e-4).
readonly warmStart?: booleanStart from the coefficients of the previous
fitcall (default: false).readonly positive?: booleanConstrain 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?: numberSeed for
selection: "random"; the global generator is used when omitted.
HuberRegressorOptions
typeConstructor options of HuberRegressor.
readonly epsilon?: numberThreshold between the quadratic and linear regimes, in units of the scale; must be > 1 (default: 1.35).
readonly alpha?: numberStrength of the squared L2 penalty on the coefficients, must be >= 0 (default: 0.0001).
readonly maxIter?: numberMaximum number of reweighting iterations (default: 100).
readonly tol?: numberConvergence tolerance relative to the standard deviation of y (default: 1e-5).
readonly fitIntercept?: booleanFit an intercept term (default: true).
readonly learningRate?: numberdeprecatedIgnored.
IsotonicOutOfBounds
typeHow IsotonicRegression.predict treats X outside the training range.
type IsotonicOutOfBounds = "clip" | "nan" | "raise";IsotonicRegressionOptions
typeConstructor options of IsotonicRegression.
readonly increasing?: boolean | "auto"truefor a non-decreasing fit,falsefor a non-increasing one,"auto"to pick the direction from the sign of the Spearman correlation betweenXandy(default: true).readonly outOfBounds?: IsotonicOutOfBoundsHandling of prediction inputs outside the training range:
"clip"uses the nearest fitted value,"nan"returns NaN and"raise"throws (default: "clip").readonly yMin?: numberLower bound applied to the fitted values.
readonly yMax?: numberUpper bound applied to the fitted values.
KernelRidgeKernel
typeKernel functions supported by KernelRidge. "poly" is an alias of "polynomial".
type KernelRidgeKernel = "linear" | "rbf" | "polynomial" | "poly" | "sigmoid" | "laplacian";KernelRidgeOptions
typeConstructor options of KernelRidge.
readonly alpha?: numberStrength of the L2 penalty, must be >= 0 (default: 1.0).
readonly kernel?: KernelRidgeKernelKernel function (default: "rbf").
readonly gamma?: numberKernel coefficient of
rbf,laplacian,polynomialandsigmoid, must be >= 0 (default: 1.0).readonly degree?: numberDegree of the polynomial kernel, must be >= 0 (default: 3).
readonly coef0?: numberIndependent term of the polynomial and sigmoid kernels (default: 1).
LassoOptions
typeConstructor options of Lasso.
readonly alpha?: numberL1 penalty strength, must be >= 0 (default: 1.0).
readonly fitIntercept?: booleanFit an intercept term (default: true).
readonly normalize?: booleanScale each (centered) column to unit L2 norm before fitting (default: false).
readonly maxIter?: numberMaximum number of full passes over the coordinates (default: 1000).
readonly tol?: numberConvergence tolerance (default: 1e-4).
readonly warmStart?: booleanStart from the coefficients of the previous
fitcall (default: false).readonly positive?: booleanConstrain 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?: numberSeed for
selection: "random"; the global generator is used when omitted.
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.
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: Float64ArrayPosterior mean of the weights, shape (n_features,).
coefTensor: TensorPosterior mean of the weights as a float64 tensor of shape (n_features,), the same type as
LinearRegression.coef.intercept: numberIntercept term (0 when
fitInterceptis false).alpha: numberEstimated precision of the noise.
lambda: numberEstimated precision of the weights.
nIter: numberNumber of evidence-maximization iterations run by the last
fit.sigma: TensorPosterior covariance matrix of the weights, shape (n_features, n_features).
scores: Float64ArrayLog marginal likelihood after every iteration plus the final value.
nFeaturesIn: numberNumber of features seen during
fit.fit(X: Tensor, y: Tensor): thisFit the model by evidence maximization.
predict(X: Tensor): TensorPredict 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): numberCoefficient 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>): thisSet hyper-parameters.
clone(): BayesianRidgeCreate 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.
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: TensorCoefficients of shape (n_features,) in the original feature scale.
intercept: numberIntercept term (0 when
fitInterceptis false).nIter: number | undefinedNumber of coordinate descent passes run by the last
fit.dualGap: number | undefinedDuality gap at the end of the last
fit(an upper bound on the suboptimality of the objective, in units ofntimes the objective).nFeaturesIn: numberNumber of features seen during
fit.fit(X: Tensor, y: Tensor): thisFit Elastic Net model using coordinate descent.
predict(X: Tensor): TensorPredict using the Elastic Net model.
score(X: Tensor, y: Tensor): numberReturn 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>): thisSet hyper-parameters.
clone(): ElasticNetCreate 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.
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 2Members (14)
constructor(options?: HuberRegressorOptions)Create a new Huber regressor.
coef: Float64ArrayFitted coefficients, shape (n_features,).
coefTensor: TensorFitted coefficients as a float64 tensor of shape (n_features,), the same type as
LinearRegression.coef.intercept: numberIntercept term (0 when
fitInterceptis false).nIter: numberNumber of reweighting iterations run by the last
fit.scale: numberEstimated scale of the residuals.
outliers: boolean[]For every training sample, whether its absolute residual exceeds
epsilon * scale.nFeaturesIn: numberNumber of features seen during
fit.fit(X: Tensor, y: Tensor): thisFit the model.
predict(X: Tensor): TensorPredict target values.
score(X: Tensor, y: Tensor): numberCoefficient 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>): thisSet hyper-parameters.
clone(): HuberRegressorCreate 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).
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: Float64ArrayBreakpoints of the fitted function (strictly increasing).
yThresholds: Float64ArrayFitted values at IsotonicRegression.xThresholds.
increasing: booleanDirection used by the last fit (resolves
increasing: "auto").fit(X: Tensor, y: Tensor, sampleWeightArg?: Tensor | void): thisFit the isotonic function.
predict(X: Tensor): TensorEvaluate the fitted function.
score(X: Tensor, y: Tensor): numberCoefficient of determination R² of the predictions.
getParams(): Record<string, unknown>Hyper-parameters of this estimator.
setParams(params: Record<string, unknown>): thisSet hyper-parameters.
clone(): IsotonicRegressionCreate 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.
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: Float64ArrayDual coefficients (one per training sample).
nFeaturesIn: numberNumber of features seen during
fit.fit(X: Tensor, y: Tensor): thisFit the model by solving
(K + alpha I) c = y.predict(X: Tensor): TensorPredict target values.
score(X: Tensor, y: Tensor): numberCoefficient of determination R² of the predictions.
getParams(): Record<string, unknown>Hyper-parameters of this estimator.
setParams(params: Record<string, unknown>): thisSet hyper-parameters.
clone(): KernelRidgeCreate 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.
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: TensorGet the model coefficients (weights).
intercept: numberGet the intercept (bias term).
nIter: number | undefinedGet the number of passes run by coordinate descent in the last fit.
dualGap: number | undefinedDuality gap at the end of the last fit.
nFeaturesIn: numberNumber of features seen during
fit.fit(X: Tensor, y: Tensor): thisFit Lasso regression model using Coordinate Descent.
predict(X: Tensor): TensorPredict using the Lasso regression model.
score(X: Tensor, y: Tensor): numberReturn 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>): thisSet the parameters of this estimator.
clone(): LassoCreate 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).
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: TensorGet the model coefficients (weights).
intercept: Tensor | undefinedGet the intercept (bias term).
interceptValue: numberThe intercept as a plain number, the same type the other linear models return from
intercept.rank: numberEffective rank of the design matrix used by the last fit.
singular: Float64ArraySingular values of the design matrix used by the last fit, in descending order.
nFeaturesIn: numberNumber of features seen during
fit.fit(X: Tensor, y: Tensor): thisFit linear model using Ordinary Least Squares.
predict(X: Tensor): TensorPredict using the linear model.
score(X: Tensor, y: Tensor): numberReturn the coefficient of determination R^2 of the prediction.
getParams(): Record<string, unknown>Get parameters for this estimator.
setParams(params: Record<string, unknown>): thisSet the parameters of this estimator.
clone(): LinearRegressionCreate 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.
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 | undefinedClass labels seen during
fit, sorted ascending.coef: TensorGet the model coefficients (weights).
intercept: number | number[]Get the intercept (bias term).
nIter: numberNumber of solver iterations of the last fit (the maximum over the binary problems for one-vs-rest).
fit(X: Tensor, y: Tensor): thisFit logistic regression model.
decisionFunction(X: Tensor): TensorRaw scores
X @ coef + interceptfor each sample.predict(X: Tensor): TensorPredict class labels.
predictProba(X: Tensor): TensorPredict class probabilities for samples.
score(X: Tensor, y: Tensor): numberReturn the mean accuracy on the given test data and labels.
getParams(): Record<string, unknown>Get hyperparameters for this estimator.
setParams(params: Record<string, unknown>): thisSet the parameters of this estimator.
clone(): LogisticRegressionCreate 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.
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: Float64ArrayFitted coefficients, one per feature.
coefTensor: TensorFitted coefficients as a float64 tensor of shape (n_features,).
intercept: numberFitted intercept (0 when
fitInterceptis false).nIter: numberNumber of interior point iterations used by the last fit.
fit(X: Tensor, y: Tensor): thisFit the model.
predict(X: Tensor): TensorPredict the conditional quantile for each sample.
score(X: Tensor, y: Tensor): numberCoefficient of determination R² of the prediction.
getParams(): Record<string, unknown>Get the hyperparameters of this estimator.
setParams(params: Record<string, unknown>): thisSet hyperparameters.
clone(): QuantileRegressorCreate 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.
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: Uint8ArrayMask over the training samples: 1 for inliers of the best model, 0 for outliers.
estimator: RegressorThe base estimator refitted on the inliers.
nTrials: numberNumber of random trials run by the last fit.
fit(X: Tensor, y: Tensor): thisFit the model with RANSAC.
predict(X: Tensor): TensorPredict with the base estimator refitted on the inliers.
score(X: Tensor, y: Tensor): numberCoefficient of determination R² of the prediction.
getParams(): Record<string, unknown>Get the hyperparameters of this estimator.
setParams(params: Record<string, unknown>): thisSet hyperparameters.
clone(): RANSACRegressorCreate 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.
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: TensorGet the model coefficients (weights).
intercept: numberGet the intercept (bias term).
nIter: number | undefinedGet the number of iterations run by the solver.
fit(X: Tensor, y: Tensor): thisFit Ridge regression model.
predict(X: Tensor): TensorPredict using the Ridge regression model.
score(X: Tensor, y: Tensor): numberReturn the coefficient of determination R² of the prediction.
getParams(): Record<string, unknown>Get parameters for this estimator.
setParams(params: Record<string, unknown>): thisSet the parameters of this estimator.
clone(): RidgeCreate 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.
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 | undefinedClass labels seen during fit, sorted ascending.
coef: Float64ArrayFitted coefficients as a flat array:
n_featuresvalues for two classes,n_classes * n_featuresvalues (row-major, one row per class) otherwise.coefTensor: TensorFitted coefficients as a float64 tensor: shape (n_features,) for two classes, (n_classes, n_features) for more.
intercept: numberFitted intercept of a binary model (0 when
fitInterceptis false).interceptArray: number[]Fitted intercepts, one per one-vs-rest model (a single value for two classes).
nIter: numberNumber of epochs run by the last fit (the maximum over the one-vs-rest models).
fit(X: Tensor, y: Tensor): thisFit the classifier.
partialFit(X: Tensor, y: Tensor, classes?: Tensor | readonly number[]): thisRun one epoch of SGD on a batch, continuing from the current weights.
decisionFunction(X: Tensor): TensorSigned distance of each sample to the separating hyperplane(s).
predict(X: Tensor): TensorPredict class labels.
predictProba(X: Tensor): TensorPredict class probabilities.
score(X: Tensor, y: Tensor): numberMean accuracy on the given data and labels.
getParams(): Record<string, unknown>Get the hyperparameters of this estimator.
setParams(params: Record<string, unknown>): thisSet hyperparameters.
clone(): SGDClassifierCreate 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.
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: Float64ArrayFitted coefficients, one per feature.
coefTensor: TensorFitted coefficients as a float64 tensor of shape (n_features,).
intercept: numberFitted intercept (0 when
fitInterceptis false).nIter: numberNumber of epochs run by the last fit.
fit(X: Tensor, y: Tensor): thisFit the regressor.
partialFit(X: Tensor, y: Tensor): thisRun one epoch of SGD on a batch, continuing from the current weights.
predict(X: Tensor): TensorPredict target values.
score(X: Tensor, y: Tensor): numberCoefficient of determination R² of the prediction.
getParams(): Record<string, unknown>Get the hyperparameters of this estimator.
setParams(params: Record<string, unknown>): thisSet hyperparameters.
clone(): SGDRegressorCreate an unfitted copy of this estimator with the same parameters.
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());Found a mistake? Open an issue.