dmitryikh / leaves

pure Go implementation of prediction part for GBRT (Gradient Boosting Regression Trees) models from popular frameworks
MIT License
427 stars 72 forks source link

Meet error when load xgboost model which trained with the API in sklearn #56

Open frontword opened 5 years ago

frontword commented 5 years ago

leaves does not support the sklearn xgboost model ? I use the below python code to train one xgboost model, meet error when use the below API to load this model in go code leaves.XGEnsembleFromFile("xg_iris.model", false)


The error : mark@mark:~/golang $ go run predict_iris.go Name: xgboost.gbtree NFeatures: 4 NOutputGroups: 3 NEstimators: 100 panic: different sizes: len(a) = 30, len(b) = 90

goroutine 1 [running]: main.main() /home/mark/golang/predict_iris.go:44 +0x686 exit status 2


Below is the python code to train the xgboost model using the xgboost API in sklearn: import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split import xgboost as xgb from xgboost.sklearn import XGBClassifier

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)

clf = XGBClassifier(**params) clf = clf.fit(X_train, y_train) y_pred = clf.predict_proba(X_test)[:,1] 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')

dmitryikh commented 5 years ago

@frontword , thank you for your detailed feedback.

leaves does not support the sklearn xgboost model ?

Currently, models generated from xgboost sklearn API are not tested. I thought, that xgboost sklearn API produces the same models as xgboost python API.

I will check you case and will come back yo you soon.