Help us shape the future of Go! Take the 2026 Go Developer Survey

leaves

package module
v0.0.0-...-e89ec51 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2019 License: MIT Imports: 13 Imported by: 0

README

leaves

Build Status GoDoc Coverage Status Go Report Card

Logo

Introduction

leaves is a library implementing prediction code for GBRT (Gradient Boosting Regression Trees) models in pure Go. The goal of the project - make it possible to use models from popular GBRT frameworks in Go programs without C API bindings.

Features

  • General Features:
    • support parallel predictions for batches
  • Support LightGBM (repo) models:
    • read models from text format and from JSON format
    • support gbdt, rf (random forest) and dart models
    • support multiclass predictions
    • addition optimizations for categorical features (for example, one hot decision rule)
    • addition optimizations exploiting only prediction usage
  • Support XGBoost (repo) models:
    • read models from binary format
    • support gbtree, gblinear, dart models
    • support multiclass predictions
    • support missing values (nan)
  • Support scikit-learn (repo) tree models (experimental support):
    • read models from pickle format (protocol 0)
    • support sklearn.ensemble.GradientBoostingClassifier

Usage examples

In order to start, go get this repository:

go get github.com/dmitryikh/leaves

Minimal example:

package main

import (
	"fmt"

	"github.com/dmitryikh/leaves"
)

func main() {
	// 1. Read model
	model, err := leaves.LGEnsembleFromFile("lightgbm_model.txt")
	if err != nil {
		panic(err)
	}

	// 2. Do predictions!
	fvals := []float64{1.0, 2.0, 3.0}
	p := model.PredictSingle(fvals, 0)
	fmt.Printf("Prediction for %v: %f\n", fvals, p)
}

In order to use XGBoost model, just change leaves.LGEnsembleFromFile, to leaves.XGEnsembleFromFile.

Documentation

Documentation is hosted on godoc (link). Documentation contains complex usage examples and full API reference. Some additional information about usage examples can be found in leaves_test.go.

Benchmark

Below are comparisons of prediction speed on batches (~1000 objects in 1 API call). Hardware: MacBook Pro (15-inch, 2017), 2,9 GHz Intel Core i7, 16 ГБ 2133 MHz LPDDR3. C API implementations were called from python bindings. But large batch size should neglect overhead of python bindings. leaves benchmarks were run by means of golang test framework: go test -bench. See benchmark for mode details on measurments. See testdata/README.md for data preparation pipelines.

Single thread:

Test Case Features Trees Batch size C API leaves
LightGBM MS LTR 137 500 1000 49ms 51ms
LightGBM Higgs 28 500 1000 50ms 50ms
LightGBM KDD Cup 99* 41 1200 1000 70ms 85ms
XGBoost Higgs 28 500 1000 44ms 50ms

4 threads:

Test Case Features Trees Batch size C API leaves
LightGBM MS LTR 137 500 1000 14ms 14ms
LightGBM Higgs 28 500 1000 14ms 14ms
LightGBM KDD Cup 99* 41 1200 1000 19ms 24ms
XGBoost Higgs 28 500 1000 ? 14ms

(?) - currenly I'm unable to utilize multithreading form XGBoost predictions by means of python bindings

(*) - KDD Cup 99 problem involves continuous and categorical features simultaneously

Limitations

  • LightGBM models:
    • no support transformations functions (sigmoid, lambdarank, etc). Output scores is raw scores
  • XGBoost models:
    • no support transformations functions. Output scores is raw scores
    • could be slight divergence between C API predictions vs. leaves because of floating point convertions and comparisons tolerances
  • scikit-learn tree models:
    • no support transformations functions. Output scores is raw scores (as from GradientBoostingClassifier.decision_function)
    • only pickle protocol 0 is supported
    • could be slight divergence between sklearn predictions vs. leaves because of floating point convertions and comparisons tolerances

Contacts

In case if you are interested in the project or if you have questions, please contact with me by email: khdmitryi at gmail.com

Documentation

Overview

Package leaves is pure Go implemetation of prediction part for GBRT (Gradient Boosting Regression Trees) models from popular frameworks.

General All loaded models exibit the same interface from `Ensemble struct`. One can use method `Name` to get string representation of model origin. Possible name values are "lightgbm.gbdt", "lightgbm.rf", "xgboost.gbtree", "xgboost.gblinear", etc.

LightGBM model

Example: binary classification

build_breast_cancer_model.py:

import lightgbm as lgb
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split

X, y = datasets.load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

n_estimators = 30
d_train = lgb.Dataset(X_train, label=y_train)
params = {
    'boosting_type': 'gbdt',
    'objective': 'binary',
}
clf = lgb.train(params, d_train, n_estimators)
# note raw_score=True used here because `leaves` output only raw scores
y_pred = clf.predict(X_test, raw_score=True)

clf.save_model('lg_breast_cancer.model')  # save the model in txt format
np.savetxt('lg_breast_cancer_true_predictions.txt', y_pred)
np.savetxt('breast_cancer_test.tsv', X_test, delimiter='\t')

predict_breast_cancer_model.go:

package main

import (
	"fmt"

	"github.com/jingxil/leaves"
	"github.com/jingxil/leaves/mat"
	"github.com/jingxil/leaves/util"
)

func main() {
	// loading test data
	test, err := mat.DenseMatFromCsvFile("breast_cancer_test.tsv", 0, false, "\t", 0.0)
	if err != nil {
		panic(err)
	}

	// loading model
	model, err := leaves.LGEnsembleFromFile("lg_breast_cancer.model")
	if err != nil {
		panic(err)
	}
	fmt.Printf("Name: %s\n", model.Name())
	fmt.Printf("NFeatures: %d\n", model.NFeatures())
	fmt.Printf("NClasses: %d\n", model.NClasses())
	fmt.Printf("NEstimators: %d\n", model.NEstimators())

	// loading true predictions as DenseMat
	truePredictions, err := mat.DenseMatFromCsvFile("lg_breast_cancer_true_predictions.txt", 0, false, "\t", 0.0)
	if err != nil {
		panic(err)
	}

	// preallocate slice to store model predictions
	predictions := make([]float64, test.Rows*model.NClasses())
	// do predictions
	model.PredictDense(test.Values, test.Rows, test.Cols, predictions, 0, 1)
	// compare results
	const tolerance = 1e-6
	if err := util.AlmostEqualFloat64Slices(truePredictions.Values, predictions, tolerance); err != nil {
		panic(fmt.Errorf("different predictions: %s", err.Error()))
	}
	fmt.Println("Predictions the same!")
}

Output:

Name: lightgbm.gbdt
NFeatures: 30
NClasses: 1
NEstimators: 30
Predictions the same!

XGBoost Model

example: Multiclass Classification

build_iris_model.py

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
import xgboost as xgb

X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

xg_train = xgb.DMatrix(X_train, label=y_train)
xg_test = xgb.DMatrix(X_test, label=y_test)
params = {
    'objective': 'multi:softmax',
    'num_class': 3,
}
n_estimators = 5
clf = xgb.train(params, xg_train, n_estimators)
# use output_margin=True because of `leaves` predictions are raw scores (before
# transformation function)
y_pred = clf.predict(xg_test, output_margin=True)
# save the model in binary format
clf.save_model('xg_iris.model')
np.savetxt('xg_iris_true_predictions.txt', y_pred, delimiter='\t')
datasets.dump_svmlight_file(X_test, y_test, 'iris_test.libsvm')

predict_iris_model.go:

package main

import (
	"fmt"

	"github.com/jingxil/leaves"
	"github.com/jingxil/leaves/mat"
	"github.com/jingxil/leaves/util"
)

func main() {
	// loading test data
	csr, err := mat.CSRMatFromLibsvmFile("iris_test.libsvm", 0, true)
	if err != nil {
		panic(err)
	}

	// loading model
	model, err := leaves.XGEnsembleFromFile("xg_iris.model")
	if err != nil {
		panic(err)
	}
	fmt.Printf("Name: %s\n", model.Name())
	fmt.Printf("NFeatures: %d\n", model.NFeatures())
	fmt.Printf("NClasses: %d\n", model.NClasses())
	fmt.Printf("NEstimators: %d\n", model.NEstimators())

	// loading true predictions as DenseMat
	truePredictions, err := mat.DenseMatFromCsvFile("xg_iris_true_predictions.txt", 0, false, "\t", 0.0)
	if err != nil {
		panic(err)
	}

	// preallocate slice to store model predictions
	predictions := make([]float64, csr.Rows()*model.NClasses())
	// do predictions
	model.PredictCSR(csr.RowHeaders, csr.ColIndexes, csr.Values, predictions, 0, 1)
	// compare results
	const tolerance = 1e-6
	// compare results. Count number of mismatched values beacase of floating point
	// tolerances in decision rule
	mismatch, err := util.NumMismatchedFloat64Slices(truePredictions.Values, predictions, tolerance)
	if err != nil {
		panic(err)
	}
	if mismatch > 2 {
		panic(fmt.Errorf("mismatched more than %d predictions", mismatch))
	}
	fmt.Printf("Predictions the same! (mismatch = %d)\n", mismatch)
}

Output:

Name: xgboost.gbtree
NFeatures: 4
NClasses: 3
NEstimators: 5
Predictions the same! (mismatch = 0)

Notes on XGBoost DART support

Please note that one must not provide nEstimators = 0 when predict with DART models from xgboost. For more details see xgboost's documentation.

Notes on LightGBM DART support

Models trained with 'boosting_type': 'dart' options can be loaded with func `leaves.LGEnsembleFromFile`. But the name of the model (given by `Name()` method) will be 'lightgbm.gbdt', because LightGBM model format doesn't distinguish 'gbdt' and 'dart' models.

Index

Constants

View Source
const BatchSize = 16

BatchSize for parallel task

Variables

This section is empty.

Functions

This section is empty.

Types

type Ensemble

type Ensemble struct {
	// contains filtered or unexported fields
}

Ensemble is a common wrapper for all models

func LGEnsembleFromFile

func LGEnsembleFromFile(filename string) (*Ensemble, error)

LGEnsembleFromFile reads LightGBM model from binary file

func LGEnsembleFromJSON

func LGEnsembleFromJSON(reader io.Reader) (*Ensemble, error)

LGEnsembleFromJSON reads LightGBM model from stream with JSON data

func LGEnsembleFromReader

func LGEnsembleFromReader(reader *bufio.Reader) (*Ensemble, error)

LGEnsembleFromReader reads LightGBM model from `reader`

func SKEnsembleFromFile

func SKEnsembleFromFile(filename string) (*Ensemble, error)

SKEnsembleFromFile reads sklearn tree ensemble model from pickle file

func SKEnsembleFromReader

func SKEnsembleFromReader(reader *bufio.Reader) (*Ensemble, error)

SKEnsembleFromReader reads sklearn tree ensemble model from `reader`

func XGBLinearFromFile

func XGBLinearFromFile(filename string) (*Ensemble, error)

XGBLinearFromFile reads XGBoost's 'gblinear' model from binary file

func XGBLinearFromReader

func XGBLinearFromReader(reader *bufio.Reader) (*Ensemble, error)

XGBLinearFromReader reads XGBoost's 'gblinear' model from `reader`

func XGEnsembleFromFile

func XGEnsembleFromFile(filename string) (*Ensemble, error)

XGEnsembleFromFile reads XGBoost model from binary file. Works with 'gbtree' and 'dart' models

func XGEnsembleFromReader

func XGEnsembleFromReader(reader *bufio.Reader) (*Ensemble, error)

XGEnsembleFromReader reads XGBoost model from `reader`. Works with 'gbtree' and 'dart' models

func (*Ensemble) GetLeafSize

func (e *Ensemble) GetLeafSize() int

func (*Ensemble) NClasses

func (e *Ensemble) NClasses() int

NClasses returns number of classes to predict

func (*Ensemble) NEstimators

func (e *Ensemble) NEstimators() int

NEstimators returns number of estimators (trees) in ensemble (per class)

func (*Ensemble) NFeatures

func (e *Ensemble) NFeatures() int

NFeatures returns number of features in the model

func (*Ensemble) Name

func (e *Ensemble) Name() string

Name returns name of the estimator

func (*Ensemble) Predict

func (e *Ensemble) Predict(fvals []float64, nEstimators int, predictions []float64) error

Predict calculates single prediction for one or multiclass ensembles. Only `nEstimators` first estimators (trees in most cases) will be used. Note, that result is a raw score (before sigmoid function transformation and etc) NOTE: for single class predictions one can use simplified function PredictSingle

func (*Ensemble) PredictCSR

func (e *Ensemble) PredictCSR(indptr []int, cols []int, vals []float64, predictions []float64, nEstimators int, nThreads int) error

PredictCSR calculates predictions from ensemble. `indptr`, `cols`, `vals` represent data structures from Compressed Sparse Row Matrix format (see CSRMat). Only `nEstimators` first estimators will be used (trees in most cases). `nThreads` points to number of threads that will be utilized (maximum is GO_MAX_PROCS) Note, that result is a raw score (before sigmoid function transformation and etc). Note, `predictions` slice should be properly allocated on call side

func (*Ensemble) PredictDense

func (e *Ensemble) PredictDense(
	vals []float64,
	nrows int,
	ncols int,
	predictions []float64,
	nEstimators int,
	nThreads int,
) error

PredictDense calculates predictions from ensemble. `vals`, `rows`, `cols` represent data structures from Rom Major Matrix format (see DenseMat). Only `nEstimators` first estimators (trees in most cases) will be used. `nThreads` points to number of threads that will be utilized (maximum is GO_MAX_PROCS) Note, that result is a raw score (before sigmoid function transformation and etc). Note, `predictions` slice should be properly allocated on call side

func (*Ensemble) PredictSingle

func (e *Ensemble) PredictSingle(fvals []float64, nEstimators int) float64

PredictSingle calculates prediction for single class model. If ensemble is multiclass, will return quitely 0.0. Only `nEstimators` first estimators (trees in most cases) will be used. If `len(fvals)` is not enough function will quietly return 0.0. Note, that result is a raw score (before sigmoid function transformation and etc) NOTE: for multiclass prediction use Predict

func (*Ensemble) PredictSingleLeafSparse

func (e *Ensemble) PredictSingleLeafSparse(fvals map[uint32]float64, ret []int) []int

PredictSingleLeafSparse output leaf indexes

func (*Ensemble) PredictSingleSparse

func (e *Ensemble) PredictSingleSparse(fvals map[uint32]float64, nFeatures int, nEstimators int) float64

PredictSingleSparse calculates prediction for single class model.

Directories

Path Synopsis
internal
Package mat provides matrix structures and loaders for `leaves` tests.
Package mat provides matrix structures and loaders for `leaves` tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL