Introducing XGBoost.Net – .Net wrappers for the awesome XGBoost library

Introducing XGBoost.Net – .Net wrappers for the awesome XGBoost library

gjgh

XGBoost is a big part of our Machine Learning and Predictive Analytics toolkit here at PicNet.  We use it almost heavily for our proof of concept and prototype work and it is always present in ensembles for production systems.  We usually host our python models on a Linux server and communicate with other back-end systems using RabbitMQ.  However, this architecture is very often too big and cumbersome for simple systems and given the fact that .Net and Python integration is terrible we decided to build our own .Net wrappers to XGBoost.

Note: Currently this package only supports x64 bit applications.

I will be writing a tutorial soon to show how to use this but in the meantime this short set of instructions should be enough to get you going.

  • Create a .Net project
  • Install the package from NuGet by opening the NuGet Package Manager Console and use the following command:
    • Install-Package PicNet. XGBoost
  • Use it in your class:
    • Add the using statement: using XGBoost;
    • Create either a XGBClassifier or XGBRegressor
    • Train using the Fit method which takes two parameters:
      • Training data, which is a 2D float array (n_rows & n_columns)
      • Training labels which is a float array (n_rows)
    • Predict using:
      • Predict: For regression and classification
      • PredictProba: Probabilities for classification

The train and predict (Fit / Predict / Predict Proba) methods are heavily inspired by the sklearn API.  So please read this to get a better idea of how these work.

The following code is an example of using this package in a Unit Test:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XGBoost;

namespace TestXGBoost
{
  [TestClass] public class XGBoostTests
  {
    [TestMethod] public void TestXGBClassifierPredictProba()
    {
      var xgb = new XGBClassifier();
      var X = new[] {
        new[] {1f, 2f, 3f, 4f, 5f},
        new[] {1f, 2f, 3f, 4f, 5f}
      };
      var y = new[] {.5f, .5f};
      xgb.Fit(X, y);
      var h = xgb.PredictProba(X);
      CollectionAssert.AreEqual(y, h[0]);
    }
  }
}


Dec, 06, 2016

2