Run XGBoost model and make predictions in Node.js

Overview

XGBoost-Node

Build Status NPM version codecov license

eXtreme Gradient Boosting Package in Node.js

XGBoost-Node is a Node.js interface of XGBoost. XGBoost is a library from DMLC. It is designed and optimized for boosted trees. The underlying algorithm of XGBoost is an extension of the classic gbm algorithm. With multi-threads and regularization, XGBoost is able to utilize more computational power and get a more accurate prediction.

The package is made to run existing XGBoost model with Node.js easily.

Features

  • Runs XGBoost Model and make predictions in Node.js.

  • Both dense and sparse matrix input are supported, and missing value is handled.

  • Supports Linux, macOS.

Install

Install from npm

npm install xgboost

Install from GitHub

git clone --recursive [email protected]:nuanio/xgboost-node.git
npm install

Documentation

Roadmap

  • Matrix API
  • Model API
  • Prediction API
  • Async API
  • Windows Support
  • Training API
  • Visualization API

Examples

Train a XGBoost model and save to a file, more in doc.

Load the model with XGBoost-Node:

const xgboost = require('xgboost');
const model = xgboost.XGModel('iris.xg.model');

const input = new Float32Array([
  5.1,  3.5,  1.4,  0.2, // class 0
  6.6,  3. ,  4.4,  1.4, // class 1
  5.9,  3. ,  5.1,  1.8  // class 2
]);

const mat = new xgboost.matrix(input, 3, 4);
console.log(model.predict(mat));
// {
//   value: [
//     0.991, 0.005, 0.004, // class 0
//     0.004, 0.990, 0.006, // class 1
//     0.005, 0.035, 0.960, // class 2
//   ],
//   error: undefined,      // no error
// }

const errModel = xgboost.XGModel('data/empty');
console.log(errModel);
console.log(errModel.predict());

Contributing

Your help and contribution is very valuable. Welcome to submit issue and pull requests. Learn more

Comments
  • npm install xgboost - fails on Debian 8

    npm install xgboost - fails on Debian 8

    Description

    Running npm install xgboost won't fully install.

    Should it work with the following set-up?

    root@vm:~# npm --version
    6.2.0
    
    root@vm:~# node --version
    v10.8.0
    
    root@vm:~# uname -a
    Linux sketchy 3.16.0-4-amd64 #1 SMP Debian 3.16.51-2 (2017-12-03) x86_64 GNU/Linux
    
    root@vm:~# c++ -v
    Using built-in specs.
    COLLECT_GCC=c++
    COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
    Target: x86_64-linux-gnu
    Configured with: ../src/configure -v --with-pkgversion='Debian 4.9.2-10+deb8u1' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --with-arch-32=i586 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
    Thread model: posix
    gcc version 4.9.2 (Debian 4.9.2-10+deb8u1) 
    
    opened by swallace21 4
  • npm install xgboost fails on OSX

    npm install xgboost fails on OSX

    Description

    npm install xgboost fails on Mac OS

    Platform, Node.js version and C++ compiler version

    Mac OS High Sierra 10.13.4

    $ uname -a
    Darwin 17.5.0 Darwin Kernel Version 17.5.0; root:xnu-4570.51.1~1/RELEASE_X86_64 x86_64
    
    $ node --version
    v9.7.1
    
    $ CC --version
    Apple LLVM version 9.1.0 (clang-902.0.39.1)
    Target: x86_64-apple-darwin17.5.0
    Thread model: posix
    InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
    
    $ gcc-7 --version
    gcc-7 (Homebrew GCC 7.3.0_1) 7.3.0
    Copyright (C) 2017 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    

    Steps to reproduce this issue

    Attempt 1

    $ npm install xgboost
    
    > [email protected] install /workspace/ews-360-sim/node_modules/xgboost
    > bash ./build.sh; node-gyp rebuild
    
    c++ -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/learner.o src/learner.cc >build/learner.d
    clang: error: unsupported option '-fopenmp'
    make: *** [build/learner.o] Error 1
    -----------------------------
    Building multi-thread xgboost failed
    Start to build single-thread xgboost
    rm -f -rf build build_plugin lib bin *~ */*~ */*/*~ */*/*/*~ */*.o */*/*.o */*/*/*.o xgboost
    rm -f -rf build_tests *.gcov tests/cpp/xgboost_test
    cd dmlc-core; /Applications/Xcode.app/Contents/Developer/usr/bin/make clean; cd /workspace/ews-360-sim/node_modules/xgboost/xgboost
    Makefile:9: make/config.mk: No such file or directory
    make[1]: *** No rule to make target `make/config.mk'.  Stop.
    cd rabit; /Applications/Xcode.app/Contents/Developer/usr/bin/make clean; cd /workspace/ews-360-sim/node_modules/xgboost/xgboost
    ...
    ...
    clang: error: no such file or directory: 'dmlc-core/libdmlc.a'
    make: *** [lib/libxgboost.dylib] Error 1
    done building library
      CXX(target) Release/obj.target/xgboost/src/index.o
      CXX(target) Release/obj.target/xgboost/src/xgmodel.o
    ../src/xgmodel.cc:176:13: warning: 'Call' is deprecated [-Wdeprecated-declarations]
      callback->Call(2, argv);
                ^
    ../../nan/nan.h:1617:3: note: 'Call' has been explicitly marked deprecated here
      NAN_DEPRECATED inline v8::Local<v8::Value>
      ^
    ../../nan/nan.h:98:40: note: expanded from macro 'NAN_DEPRECATED'
    # define NAN_DEPRECATED __attribute__((deprecated))
                                           ^
    1 warning generated.
      CXX(target) Release/obj.target/xgboost/src/xgmatrix.o
      SOLINK_MODULE(target) Release/xgboost.node
    clang: error: no such file or directory: '../xgboost/dmlc-core/libdmlc.a'
    make: *** [Release/xgboost.node] Error 1
    gyp ERR! build error
    gyp ERR! stack Error: `make` failed with exit code: 2
    gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node_modules/node-gyp/lib/build.js:258:23)
    gyp ERR! stack     at ChildProcess.emit (events.js:127:13)
    gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:12)
    gyp ERR! System Darwin 17.5.0
    gyp ERR! command "/usr/local/Cellar/node/8.1.4/bin/node" "/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
    gyp ERR! cwd /Google Drive/workspace/ews-360-sim/node_modules/xgboost
    gyp ERR! node -v v9.7.1
    gyp ERR! node-gyp -v v3.6.2
    gyp ERR! not ok
    npm WARN @fortawesome/[email protected] requires a peer of [email protected] but none is installed. You must install peer dependencies yourself.
    
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! [email protected] install: `bash ./build.sh; node-gyp rebuild`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the [email protected] install script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /.npm/_logs/2018-04-26T21_15_07_146Z-debug.log
    

    Attempt 2

    $ CC="gcc-7" CXX="g++-7" npm i xgboost
    
    > [email protected] install /workspace/ews-360-sim/node_modules/xgboost
    > bash ./build.sh; node-gyp rebuild
    
    g++-7 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/learner.o src/learner.cc >build/learner.d
    g++-7 -c -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp src/learner.cc -o build/learner.o
    g++-7 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/logging.o src/logging.cc >build/logging.d
    g++-7 -c -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp src/logging.cc -o build/logging.o
    g++-7 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/c_api/c_api.o src/c_api/c_api.cc >build/c_api/c_api.d
    g++-7 -c -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp src/c_api/c_api.cc -o build/c_api/c_api.o
    g++-7 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/c_api/c_api_error.o src/c_api/c_api_error.cc >build/c_api/c_api_error.d
    g++-7 -c -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp src/c_api/c_api_error.cc -o build/c_api/c_api_error.o
    g++-7 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/common/common.o src/common/common.cc >build/common/common.d
    g++-7 -c -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp src/common/common.cc -o build/common/common.o
    g++-7 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/common/hist_util.o src/common/hist_util.cc >build/common/hist_util.d
    g++-7 -c -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp src/common/hist_util.cc -o build/common/hist_util.o
    g++-7 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/data/data.o src/data/data.cc >build/data/data.d
    g++-7 -c -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp src/data/data.cc -o build/data/data.o
    g++-7 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/data/simple_csr_source.o src/data/simple_csr_source.cc >build/data/simple_csr_source.d
    ...
    ...
    ...
    g++-7: error: dmlc-core/libdmlc.a: No such file or directory
    make: *** [lib/libxgboost.dylib] Error 1
    -----------------------------
    Building multi-thread xgboost failed
    Start to build single-thread xgboost
    rm -f -rf build build_plugin lib bin *~ */*~ */*/*~ */*/*/*~ */*.o */*/*.o */*/*/*.o xgboost
    rm -f -rf build_tests *.gcov tests/cpp/xgboost_test
    cd dmlc-core; /Applications/Xcode.app/Contents/Developer/usr/bin/make clean; cd /workspace/ews-360-sim/node_modules/xgboost/xgboost
    Makefile:9: make/config.mk: No such file or directory
    make[1]: *** No rule to make target `make/config.mk'.  Stop.
    cd rabit; /Applications/Xcode.app/Contents/Developer/usr/bin/make clean; cd /workspace/ews-360-sim/node_modules/xgboost/xgboost
    ...
    ...
    ...
    metric.o build/metric/metric.o build/metric/multiclass_metric.o build/metric/rank_metric.o build/objective/multiclass_obj.o build/objective/objective.o build/objective/rank_obj.o build/objective/regression_obj.o build/tree/tree_model.o build/tree/tree_updater.o build/tree/updater_colmaker.o build/tree/updater_fast_hist.o build/tree/updater_histmaker.o build/tree/updater_prune.o build/tree/updater_refresh.o build/tree/updater_skmaker.o build/tree/updater_sync.o dmlc-core/libdmlc.a rabit/lib/librabit_empty.a -pthread -lm
    g++-7: error: dmlc-core/libdmlc.a: No such file or directory
    make: *** [lib/libxgboost.dylib] Error 1
    done building library
      CXX(target) Release/obj.target/xgboost/src/index.o
    g++-7: error: unrecognized command line option '-stdlib=libc++'
    make: *** [Release/obj.target/xgboost/src/index.o] Error 1
    gyp ERR! build error
    gyp ERR! stack Error: `make` failed with exit code: 2
    gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node_modules/node-gyp/lib/build.js:258:23)
    gyp ERR! stack     at ChildProcess.emit (events.js:127:13)
    gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:12)
    gyp ERR! System Darwin 17.5.0
    gyp ERR! command "/usr/local/Cellar/node/8.1.4/bin/node" "/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
    gyp ERR! cwd /Google Drive/workspace/ews-360-sim/node_modules/xgboost
    gyp ERR! node -v v9.7.1
    gyp ERR! node-gyp -v v3.6.2
    gyp ERR! not ok
    
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! [email protected] install: `bash ./build.sh; node-gyp rebuild`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the [email protected] install script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /.npm/_logs/2018-04-26T21_22_18_780Z-debug.log
    

    Is compilation on Mac OS supported as claimed in the README?

    opened by dennismphil 4
  • #2: correct Travis badge

    #2: correct Travis badge

    Description

    the image URL of Travis badge is not correct: https://travis-ci.org/nuanio/xgboost-node.svg?branch=update-readme , which is point to update-readme branch.

    this patch changes the target branch to master.

    Tested platforms

    macOS

    opened by jiabinf 2
  • Any workarounds to make this run on AWS Lambda?

    Any workarounds to make this run on AWS Lambda?

    I installed this package on Amazon Linux machine and copied the whole node_modules folder directly to my Lambda zip file. This ensures that the package is built in the same environment that is supposed to run. However, when trying to require the package it fails with the following message:

    {
      "errorMessage": "/lib64/libm.so.6: version `GLIBC_2.23' not found (required by /var/task/node_modules/xgboost/build/Release/xgboost.node)",
      "errorType": "Error",
      "stackTrace": [
        "Object.Module._extensions..node (module.js:597:18)",
        "Module.load (module.js:487:32)",
        "tryModuleLoad (module.js:446:12)",
        "Function.Module._load (module.js:438:3)",
        "Module.require (module.js:497:17)",
        "require (internal/module.js:20:19)",
        "bindings (/var/task/node_modules/bindings/bindings.js:81:44)",
        "Object.<anonymous> (/var/task/node_modules/xgboost/index.js:6:11)",
        "Module._compile (module.js:570:32)"
      ]
    }
    

    I'm guessing there might be some dependencies that don't exists in node_modules/xgboost folder. Any ideas or workaround to get this working on AWS Lambda?

    opened by skilledDeveloper 1
  • correct Travis badge

    correct Travis badge

    Description

    the image URL of Travis badge is not correct: https://travis-ci.org/nuanio/xgboost-node.svg?branch=update-readme , which is point to update-readme branch

    Platform, Node.js version and C++ compiler version

    n/a

    Steps to reproduce this issue

    n/a

    Related issues

    #1

    opened by jiabinf 1
  • Inconsistent results for data with Nulls

    Inconsistent results for data with Nulls

    Hi,

    when I evaluate the model result in python it got different result from node.js in production. It turns out it is the problem with handling Null values. We used the API you mentioned: matrix(data, row, col, missing), and there is still about ~10% differences in terms of scores.

    Could you please tell me what could the problem be, or give an example or how to deal with it? Right now we are doing something like this in production:

    featureValues.push(item[col.name] || NaN) }) }) featureValues = new Float32Array(featureValues)

    let inputMatrix = new xgboost.matrix(featureValues,
      row,
      column, NaN)
    
    opened by cnll0075 1
Owner
暖房 / nuan.io
暖房 / nuan.io
Run Keras models in the browser, with GPU support using WebGL

**This project is no longer active. Please check out TensorFlow.js.** The Keras.js demos still work but is no longer updated. Run Keras models in the

Leon Chen 4.9k Dec 29, 2022
This is a JS/TS library for accelerated tensor computation intended to be run in the browser.

TensorJS TensorJS How to use Tensors Tensor operations Reading values Data types Converting between backends Onnx model support Optimizations Running

Frithjof Winkelmann 32 Jun 26, 2022
architecture-free neural network library for node.js and the browser

Synaptic Important: Synaptic 2.x is in stage of discussion now! Feel free to participate Synaptic is a javascript neural network library for node.js a

Juan Cazala 6.9k Dec 27, 2022
Bayesian bandit implementation for Node and the browser.

#bayesian-bandit.js This is an adaptation of the Bayesian Bandit code from Probabilistic Programming and Bayesian Methods for Hackers, specifically d3

null 44 Aug 19, 2022
Simple Javascript implementation of the k-means algorithm, for node.js and the browser

#kMeans.js Simple Javascript implementation of the k-means algorithm, for node.js and the browser ##Installation npm install kmeans-js ##Example (JS)

Emil Bay 44 Aug 19, 2022
Clustering algorithms implemented in Javascript for Node.js and the browser

Clustering.js ####Clustering algorithms implemented in Javascript for Node.js and the browser Examples License Copyright (c) 2013 Emil Bay github@tixz

Emil Bay 29 Aug 19, 2022
general natural language facilities for node

natural "Natural" is a general natural language facility for nodejs. It offers a broad range of functionalities for natural language processing. Docum

null 10k Jan 9, 2023
Machine-learning for Node.js

Limdu.js Limdu is a machine-learning framework for Node.js. It supports multi-label classification, online learning, and real-time classification. The

Erel Segal-Halevi 1k Dec 16, 2022
Machine Learning library for node.js

shaman Machine Learning library for node.js Linear Regression shaman supports both simple linear regression and multiple linear regression. It support

Luc Castera 108 Feb 26, 2021
Powerful Neural Network for Node.js

NeuralN Powerful Neural Network for Node.js NeuralN is a C++ Neural Network library for Node.js with multiple advantages compared to existing solution

TOTEMS::Tech 275 Dec 15, 2022
Latent Dirichlet allocation (LDA) topic modeling in javascript for node.js.

LDA Latent Dirichlet allocation (LDA) topic modeling in javascript for node.js. LDA is a machine learning algorithm that extracts topics and their rel

Kory Becker 279 Nov 4, 2022
FANN (Fast Artificial Neural Network Library) bindings for Node.js

node-fann node-fann is a FANN bindings for Node.js. FANN (Fast Artificial Neural Network Library) is a free open source neural network library, which

Alex Kocharin 186 Oct 31, 2022
JavaScript API for face detection and face recognition in the browser and nodejs with tensorflow.js

face-api.js JavaScript face recognition API for the browser and nodejs implemented on top of tensorflow.js core (tensorflow/tfjs-core) Click me for Li

Vincent Mühler 14.6k Jan 2, 2023
A JavaScript deep learning and reinforcement learning library.

neurojs is a JavaScript framework for deep learning in the browser. It mainly focuses on reinforcement learning, but can be used for any neural networ

Jan 4.4k Jan 4, 2023
A WebGL accelerated JavaScript library for training and deploying ML models.

TensorFlow.js TensorFlow.js is an open-source hardware-accelerated JavaScript library for training and deploying machine learning models. ⚠️ We recent

null 16.9k Jan 4, 2023
Train and test machine learning models for your Arduino Nano 33 BLE Sense in the browser.

Tiny Motion Trainer Train and test IMU based TFLite models on the Web Overview Since 2009, coders have created thousands of experiments using Chrome,

Google Creative Lab 59 Nov 21, 2022
Interactive digital art with head-coupled perspective effect using Three.js and TensorFlow.js

Interactive digital frame with head-tracking using Three.js & TensorFlow.js Using TensorFlow.js and Three.js, this project is a prototype of an intera

Charlie 61 Dec 12, 2022
A react native cached, animated, and auto chooses the best-resolution image for the current screen.

A react native cached, animated and auto chooses the best-resolution image for the current screen. This is a simple react native image component that

Nam Nguyen 29 Mar 30, 2022