Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

XGB

Gradient boosted tree training and inference in TypeScript for Deno.

Install

// Import the XGB class and types
import XGB, { type XgbDump, type XgbObjective, type XgbParams } from '@neabyte/xgb'

Quick Start

import XGB from '@neabyte/xgb'

// Train a binary classifier and predict labels
const model = new XGB({ objective: 'binary:logistic', nEstimators: 50 })
const x = [
  [1, 2],
  [3, 4],
  [5, 6],
  [7, 8]
]
const y = [0, 0, 1, 1]
model.fit(x, y)
const labels = model.predict(x)
const probabilities = model.proba(x)

Constructor

The constructor accepts either a string objective or a partial config object. When no argument is provided it defaults to an empty object.

If config is a string, it is wrapped as { objective: config }. The provided config is merged with default parameters using a spread, so any fields you omit will take their default values.

The constructor looks up an internal strategy by the resolved objective. If the objective is unknown, it throws a RangeError with a message listing all valid objectives.

Both objective and params are exposed as readonly public properties on the instance.

// Construct with an objective string shorthand
const a = new XGB('reg:squarederror')

// Construct with a partial config object
const b = new XGB({ objective: 'multi:softprob', nEstimators: 200, maxDepth: 4 })

// Construct with defaults (binary:logistic, 100 rounds)
const c = new XGB()

Public Methods

Method Returns Description
fit(matrix, labels) this Trains the model on a feature matrix and label array. Returns the model instance for chaining. Throws TypeError on invalid input shapes, RangeError when the matrix has no features, RangeError when sizes disagree, RangeError when a label is not finite, and RangeError when a multiclass label is negative or not an integer.
predict(matrix) number[] Returns one predicted label or value per sample by applying the objective-specific reducer to the raw margins. Throws TypeError when the model has not been fitted, TypeError when the matrix is not an array of number arrays, and RangeError when a row width differs from the feature count seen during fit.
proba(matrix) number[][] Returns a probability vector per sample. Throws TypeError when the model has not been fitted, TypeError if the objective does not support probability output, TypeError when the matrix is not an array of number arrays, and RangeError when a row width differs from the feature count seen during fit.
margin(matrix) number[] | number[][] Returns raw margins per sample. For scalar objectives the result is number[] and for multiclass objectives it is number[][]. Throws TypeError when the model has not been fitted, TypeError when the matrix is not an array of number arrays, and RangeError when a row width differs from the feature count seen during fit.
importance(type) number[] Returns a per-feature score vector. The type argument defaults to 'gain' and accepts 'gain', 'cover', 'weight', 'total_gain', or 'total_cover'. Throws TypeError when the model has not been fitted, TypeError when the model was restored by XGB.load and its importance stats are absent, and RangeError for unknown types.
dump() XgbDump Serializes the fitted model to a plain object for persistence. The returned dump copies parameters, baseMargin, and every tree, so it never aliases live model state. Throws TypeError when the model has not been fitted.
XGB.load(dump) XGB Static method that restores a ready-to-predict model from a serialized XgbDump. Throws TypeError when the dump format is not 'xgb/1' or when the dump is missing required fields, RangeError when numFeature or numClass is not a positive integer, and RangeError when baseMargin or the tree nesting disagrees with numClass.

Parameters

All fields on XgbParams with their types and defaults from the built-in default config.

Property Type Default Description
objective XgbObjective 'binary:logistic' Training objective name that selects the loss function and prediction strategy.
nEstimators number 100 Number of boosting rounds to run during training.
maxDepth number 6 Maximum depth allowed for each tree.
learningRate number 0.3 Shrinkage factor applied to every leaf weight after each round.
lambda number 1.0 L2 regularization term on leaf weights.
gamma number 0.0 Minimum loss reduction required to make a split. Splits with gain below this value are pruned after the tree is fully grown.
minWeight number 1.0 Minimum sum of hessian values in a child node required for a split.
maxStep number | null null Maximum absolute leaf weight clamp. When set to null, the objective-specific default is used. For count:poisson this defaults to 0.7 and for all other objectives it defaults to 0.
subsample number 1.0 Fraction of training rows sampled per boosting round. A value of 1.0 uses all rows.
colsampleByTree number 1.0 Fraction of features sampled per tree. A value of 1.0 uses all features.
baseScore number | null null Explicit global base score. When set to null, the base score is derived from the training labels using the objective-specific strategy.
seed number 0 Seed for the random number generator used in row and column subsampling.

Objectives

Objective Task Description
binary:logistic Binary classification Outputs a probability between 0 and 1 using the logistic sigmoid. Supports both predict and proba.
binary:logitraw Binary classification Outputs raw logit margins without applying sigmoid. Does not support proba.
reg:squarederror Regression Minimizes squared error loss. Predictions are raw margin values.
reg:logistic Regression Applies logistic sigmoid to raw margins for regression targets in the 0 to 1 range.
count:poisson Count regression Models count data with Poisson loss. Predictions are exponentiated margins. Uses a default maxStep of 0.7 and derives the base score as the mean of the training labels with a floor of 1e-6.
multi:softprob Multiclass classification Outputs a probability vector over all classes using softmax. Trains one tree per class per round.
multi:softmax Multiclass classification Outputs the class index with the highest margin. Trains one tree per class per round. Does not support proba.

Persistence

// Serialize a trained model and restore it later
const dump = model.dump()
const json = JSON.stringify(dump)
const restored = XGB.load(JSON.parse(json))
const predictions = restored.predict(x)

The dump method produces a plain XgbDump object that captures the format version, objective, parameters, base score, base margins, feature and class counts, and all trained trees. Every field is copied rather than shared, so mutating the dump cannot corrupt the model it came from. The XGB.load static method accepts this object, validates the format tag, the required fields, and the shape agreement between numClass, baseMargin, and the tree nesting, and returns a fully restored model that is ready for prediction. Serialization to and from JSON is handled by the caller.