XGBoost for Node.js
This module provides the runtime for XGBoost model. Run existing XGBoost model with Node.js.
Require a C++ compiler to build XGBoost library.
Install
from npm
npm install xgboost
from GitHub
git clone --recursive git@github.com:nuanio/xgboost-node.git
npm install
Example
Train XGBoost model with Python, and then save the model file.
import xgboost as xgb
from sklearn import datasets
iris = datasets.load_iris()
dtrain = xgb.DMatrix(iris.data, label = iris.target)
param = {
'max_depth': 3,
'eta': 0.3,
'objective': 'multi:softprob',
'num_class': 3 # three class 0, 1, 2
}
num_round = 20
bst = xgb.train(param, dtrain, num_round)
bst.save_model("iris.xg.model")
Load the model with Node.js:
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());
Checkout more examples in the test cases.
Functions
XGMatrix
-
matrix(data, row, col, missing = NaN) ⇒
XGMatrix
-
restoreMatrix(file) ⇒
XGMatrix
-
matrixFromCSC(data, indptr, indices, n = 0) ⇒
XGMatrix
-
matrixFromCSR(data, indptr, indices, n = 0) ⇒
XGMatrix
XGModel
-
XGModel(file) ⇒
XGModel
-
XGModel.predict(xgmatrix, mask = 0, ntree = 0) ⇒
Result
Result
XGMatrix
Kind: object - input matrix for XGModel
Field | Type | Description |
---|---|---|
matrix | internal |
readonly property |
error | Error |
error status |
matrix(data, row, col, missing) ⇒ XGMatrix
Kind: global function
Returns: XGMatrix
- xgboost matrix
Param | Type | Description |
---|---|---|
data | Float32Array |
input matrix |
row | Integer |
matrix row |
col | Integer |
matrix col |
missing | Number = NaN |
missing value place holder |
const xgboost = require('xgboost');
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);
XGMatrix.col() ⇒ Result
Kind: member function
Returns: Result
- return matrix column size
mat.col();
XGMatrix.row() ⇒ Result
Kind: member function
Returns: Result
- return matrix row size
mat.row();
restoreMatrix(file) ⇒ XGMatrix
Kind: global function
Returns: XGMatrix
- xgboost matrix
Param | Type | Description |
---|---|---|
file | string |
input matrix file path |
const matFromFile = xgboost.restoreMatrix('test/data/xgmatrix.bin');
matrixFromCSC(data, indptr, indices, n) ⇒ XGMatrix
Kind: global function
Returns: XGMatrix
- xgboost matrix
Param | Type | Description |
---|---|---|
data | Float32Array |
input matrix |
indptr | Uint32Array |
pointer to col headers |
indices | Uint32Array |
findex |
n | Integer = 0 |
number of rows; when it's set to 0, then guess from data |
// [
// 1, 2, 3, 1,
// 0, 1, 2, 3,
// 0, 1, 1, 1,
// ]
const sparseCSC = xgb.matrixFromCSC(
new Float32Array([1, 2, 1, 1, 3, 2, 1, 1, 3, 1]),
new Uint32Array([0, 1, 4, 7, 10]),
new Uint32Array([0, 0, 1, 2, 0, 1, 2, 0, 1, 2]),
0);
matrixFromCSR(data, indptr, indices, n) ⇒ XGMatrix
Kind: global function
Returns: XGMatrix
- xgboost matrix
Param | Type | Description |
---|---|---|
data | Float32Array |
input matrix |
indptr | Uint32Array |
pointer to row headers |
indices | Uint32Array |
findex |
n | Integer = 0 |
number of columns; when it's set to 0, then guess from data |
// [
// 1, 2, 3, 1,
// 0, 1, 2, 3,
// 0, 1, 1, 1,
// ]
const sparseCSR = xgboost.matrixFromCSR(
new Float32Array([1, 2, 3, 1, 1, 2, 3, 1, 1, 1]),
new Uint32Array([0, 4, 7, 10]),
new Uint32Array([0, 1, 2, 3, 1, 2, 3, 1, 2, 3]),
0);
XGModel
Kind: object - Trained XGModel
Field | Type | Description |
---|---|---|
model | internal |
readonly property |
error | Error |
error status |
XGModel(file) ⇒ XGModel
Kind: global function
Returns: XGModel
- xgboost model
Param | Type | Description |
---|---|---|
file | string |
model file path |
const model = xgboost.XGModel('test/data/iris.xg.model');
XGModel.predict(xgmatrix, mask = 0, ntree = 0) ⇒ Result
Kind: member function
Returns: Result
- prediction result with Float32Array
Param | Type | Description |
---|---|---|
matrix | XGMatrix |
input matrix |
mask | Integer = 0 |
bit-mask of options taken in prediction, possible values, 0:normal prediction, 1:output margin instead of transformed value, 2:output leaf index of trees instead of leaf value, note leaf index is unique per tree, 4:output feature contributions to individual predictions |
ntree | Integer = 0 |
limit number of trees used for prediction, this is only valid for boosted trees when the parameter is set to 0, we will use all the trees |
model.predict(mat);
Result
Kind: object
Field | Type | Description |
---|---|---|
value | Float32Array | number |
prediction result or method result |
error | Error |
error status |
Contributing
Thank you for your interest to contribute to the XGBoost Node.js module.
Please submit an issue to suggest new feature before submitting pull requests.