npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xgboost-scorer

v0.0.4

Published

A pure JavaScript implementation of XGBoost scoring

Downloads

16

Readme

xgboost-scorer

xgboost-scorer is a pure JavaScript scoring function implementation for XGBoost models, for use in a Node.js environment or even in a browser.

NOTE: This implementation is an extremely naive one, that will most likely not work well with large inputs. Also, it doesn't handle all scenarios (currently only works for binary classification, but can be easily extended for other types of tasks). If performance is a big concern, consider using the xgboost-node package instead, which is a Node.js addon that invokes the actual XGBoost scoring function. However, you might have to compile the package for your specific platform (e.g., Windows), which could be a bit of a problem. So, if you don't care about serving a million QPS, are working with small models and inputs; and want to run in multiple hostile environments (e.g., in the browser), then this package might be of use to you.

Installation

npm install xgboost-scorer

Usage

import Scorer from 'xgboost-scorer';

// Create a scorer using an XGBoost model and (optionally) a feature index
const scorer = await Scorer.create('xgboost.model.json', 'feature_index.json');

// Get the score for an instance by passing a mapping of (feature -> value)
const score =
    await scorer.score({
        featureA: 100,
        featureB: 0.1,
        categoricalFeatureC: 1.0
    })
scorePromise.then(console.log); // E.g., 0.42

Documentation

A scorer is created by specifying the XGBoost model to use, in JSON. You can either create it using a JSON file on disk (in Node.js-like environments) or by passing in the JSON object directly (in the browser, for instance):

// In Node.js
const scorer = await Scorer.create('/path/to/xgboost.model.json');

// In the browser (or even in Node.js)
const scorer = await Scorer.create([
  {
    "nodeid": 0,
    "depth": 0,
    "split": "top_diff",
    "split_condition": 19.40625,
    "yes": 1,
    "no": 2,
    ...
])

This will handle most use-cases that require scoring in an online setting. For offline scoring, you would typically have your input data to be scored in a file in LibSVM format, and this is where the feature index comes in:

const scorer = Scorer.create('/path/to/xgboost.model.json', '/path/to/feature_index.json')
scorer.score('/path/to/data.libsvm');

The feature index is required to provide a mapping between the feature names (present in your JSON model dump) and the feature indices (used in the LibSVM file).

The model file format

We use a JSON dump of the XGBoost model for scoring, because that is the probably what you would want to work with in Node.js (and also because I'm too lazy to figure out how to get this to work with other formats). This is how you would get your model in JSON (if you were doing training/testing in Python, for instance):

import xgboost as xgb
...
model = xgb.train(...)
model.dump_model('xgb.model.json', 'feature_map.txt', dump_format='json')

# This is what it would look like:
[
  {
    "nodeid": 0,
    "depth": 0,
    "split": "featureA",
    "split_condition": 22,
    "yes": 1,
    "no": 2,
    "missing": 1,
    "children": [
      {
        "nodeid": 1,
        "leaf": 0.0700000003
      },
      {
        "nodeid": 2,
        "leaf": -0.0700000003
      }
    ]
  }
]

The feature index format

(Only used in offline scoring using a data file in LibSVM format) The feature index file contains a mapping from feature name to the feature ID used in the LibSVM file, e.g.:

// feature_index.json
{
  "featureA": 1,
  "featureB": 2,
  "categoricalFeatureC": 3,
  ...
}