Skip to content
XGBoost - the high performance machine learning library - for Ruby
Ruby Python Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

Xgb

XGBoost - the high performance machine learning library - for Ruby

🔥 Uses the C API for blazing performance

Build Status

Installation

First, install XGBoost. On Mac, copy lib/libxgboost.dylib to /usr/local/lib.

Add this line to your application’s Gemfile:

gem 'xgb'

Getting Started

This library follows the Python API, with the get_ and set_ prefixes removed from methods. Some methods and options are missing at the moment. PRs welcome!

Learning API

Prep your data

x = [[1, 2], [3, 4], [5, 6], [7, 8]]
y = [1, 2, 3, 4]

Train a model

params = {objective: "reg:squarederror"}
dtrain = Xgb::DMatrix.new(x, label: y)
booster = Xgb.train(params, dtrain)

Predict

dtest = Xgb::DMatrix.new(x)
booster.predict(dtest)

Save the model to a file

booster.save_model("my.model")

Load the model from a file

booster = Xgb::Booster.new(model_file: "my.model")

Get the importance of features

booster.score

Early stopping

Xgb.train(params, dtrain, evals: [[dtrain, "train"], [dtest, "eval"]], early_stopping_rounds: 5)

CV

Xgb.cv(params, dtrain, nfold: 3, verbose_eval: true)

Scikit-Learn API

Prep your data

x = [[1, 2], [3, 4], [5, 6], [7, 8]]
y = [1, 2, 3, 4]

Train a model

model = Xgb::Regressor.new
model.fit(x, y)

For classification, use Xgb::Classifier

Predict

model.predict(x)

For classification, use predict_proba for probabilities

Save the model to a file

model.save_model("my.model")

Load the model from a file

model.load_model("my.model")

Get the importance of features

model.feature_importances

Early stopping

model.fit(x, y, eval_set: [[x_test, y_test]], early_stopping_rounds: 5)

Data

Data can be an array of arrays

[[1, 2, 3], [4, 5, 6]]

Or a Daru data frame

Daru::DataFrame.from_csv("houses.csv")

Or a Numo NArray

Numo::DFloat.new(3, 2).seq

XGBoost Installation

There’s an experimental branch that includes XGBoost with the gem for easiest installation.

gem 'xgb', github: 'ankane/xgb', branch: 'vendor'

Please file an issue if it doesn’t work for you.

You can also specify the path to XGBoost in an initializer:

Xgb.ffi_lib << "/path/to/xgboost/lib/libxgboost.so"

Use libxgboost.dylib for Mac and xgboost.dll for Windows

Helpful Resources

Related Projects

  • LightGBM - LightGBM for Ruby
  • Eps - Machine Learning for Ruby

Credits

Thanks to the xgboost gem for serving as an initial reference, and Selva Prabhakaran for the test datasets.

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

You can’t perform that action at this time.