使用Xgboost模型使用treelite

时间:2019-09-02 13:30:12

标签: python c compilation prediction xgboost

我正在使用Treelite库将Xgboost模型转换为C文件。

我有一个正确执行此示例的示例。

import treelite
import xgboost
from sklearn.datasets import load_boston
import treelite.runtime     # runtime module

X, y = load_boston(return_X_y=True)
print('dimensions of X = {}'.format(X.shape))
print('dimensions of y = {}'.format(y.shape))
dtrain = xgboost.DMatrix(X, label=y)
params = {'max_depth':3, 'eta':1, 'silent':1, 'objective':'reg:linear',
          'eval_metric':'rmse'}
bst = xgboost.train(params, dtrain, 20, [(dtrain, 'train')])
bst.save_model('bst1.model')
bst = xgboost.Booster({'nthread':4}) #init model
bst.load_model("bst1.model") # load data
model = treelite.Model.from_xgboost(bst)
toolchain = 'gcc'
model.export_lib(toolchain=toolchain, libpath='./mymodel.dylib', verbose=True)
model.export_lib(toolchain=toolchain, libpath='./mymodel.dylib',params={'parallel_comp': 32}, verbose=True)
predictor = treelite.runtime.Predictor('./mymodel.dylib', verbose=True)
batch = treelite.runtime.Batch.from_npy2d(X, rbegin=10, rend=20)
out_pred = predictor.predict(batch)
print(out_pred)

以下是可帮助我解决问题的教程-https://treelite.readthedocs.io/en/latest/tutorials/first.html

我可以运行此示例程序。现在,我想使用Treelite运行我的实际Xgboost模型来生成C文件。

我实际的Xgboost模型是这个-https://gist.github.com/karimkhanvi/ee0d515ce9b24dba0f10a73759352c7a

在这里我已将其转换为treelite格式-

def predict(data):
    features_noise = np.zeros((5, ))

    snr, rr_num, var, fr, fr2 = find_noise_features(data)
    features_noise[0] = snr
    features_noise[1] = rr_num
    features_noise[2] = var
    features_noise[3] = fr
    features_noise[4] = fr2
    features = extract_basic_features(data, 30000)
    features = np.hstack((features, features_noise.reshape(1, -1)))

    bst = xgb.Booster({'nthread': 4})
    bst.load_model("xgb_model.bin")
    dfeatures = xgb.DMatrix(features)
    prediction = bst.predict(dfeatures,ntree_limit=420)
    prediction = prediction.astype('int8')
    result = data_preprocess.encoder.inverse_transform(prediction)
    # print(prediction)
    # print(result)
    model = treelite.Model.from_xgboost(bst)
    toolchain = 'gcc'
    model.export_lib(toolchain=toolchain, libpath='./afibmodel.dylib', verbose=True)
    model.export_lib(toolchain=toolchain, libpath='./afibmodel.dylib',params={'parallel_comp': 32}, verbose=True)
    predictor = treelite.runtime.Predictor('./afibmodel.dylib', verbose=True)
    batch = treelite.runtime.Batch.from_npy2d(dfeatures)
    out_pred = predictor.predict(batch)
    print(out_pred)


train_data, train_labels = data_preprocess(path)
train(train_data, train_labels)
data = genfromtxt('AFIB.csv', delimiter=',')
predict(data)

我怀疑我做对与否。如果您能告诉我是否正确预测了数据,我们将不胜感激。

0 个答案:

没有答案