XGBoost - high performance gradient boosting - for Ruby
Add this line to your application’s Gemfile:
gem "xgb"On Mac, also install OpenMP:
brew install libompPrep your data
x = [[1, 2], [3, 4], [5, 6], [7, 8]]
y = [1, 2, 3, 4]Train a model
params = {objective: "reg:squarederror"}
dtrain = XGBoost::DMatrix.new(x, label: y)
booster = XGBoost.train(params, dtrain)Predict
dtest = XGBoost::DMatrix.new(x)
booster.predict(dtest)Save the model to a file
booster.save_model("model.json")Load the model from a file
booster = XGBoost::Booster.new(model_file: "model.json")Get the importance of features
booster.scoreEarly stopping
XGBoost.train(params, dtrain, evals: [[dtrain, "train"], [dtest, "eval"]], early_stopping_rounds: 5)CV
XGBoost.cv(params, dtrain, nfold: 3, verbose_eval: true)Set metadata about a model
booster["key"] = "value"
booster.attributesPrep your data
x = [[1, 2], [3, 4], [5, 6], [7, 8]]
y = [1, 2, 3, 4]Train a model
model = XGBoost::Regressor.new
model.fit(x, y)For classification, use
XGBoost::Classifier
Predict
model.predict(x)For classification, use
predict_probafor probabilities
Save the model to a file
model.save_model("model.json")Load the model from a file
model.load_model("model.json")Get the importance of features
model.feature_importancesEarly stopping
model = XGBoost::Regressor.new(early_stopping_rounds: 5)
model.fit(x, y, eval_set: [[x_test, y_test]])Data can be an array of arrays
[[1, 2, 3], [4, 5, 6]]Or a Numo array
Numo::NArray.cast([[1, 2, 3], [4, 5, 6]])Or a Rover data frame
Rover.read_csv("houses.csv")Or a Daru data frame
Daru::DataFrame.from_csv("houses.csv")This library follows the Python API, with the get_ and set_ prefixes removed from methods to make it more Ruby-like.
Thanks to the xgboost gem for showing how to use FFI.
View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/ankane/xgboost-ruby.git
cd xgboost-ruby
bundle install
bundle exec rake vendor:all
bundle exec rake test