C ++中的xgboost负载模型(python-> c ++预测分数不匹配) [英] xgboost load model in c++ (python -> c++ prediction scores mismatch)

查看:216
本文介绍了C ++中的xgboost负载模型(python-> c ++预测分数不匹配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在接触所有SO c ++天才。

I'm reaching out to all SO c++ geniuses.

我已经在python中训练(并成功测试了)xgboost模型,如下所示:

I've trained (and successfully tested) an xgboost model in python like so:

dtrain 
=xgb.DMatrix(np.asmatrix(X_train),label=np.asarray(y_train,dtype=np.int), feature_names=feat_names)

optimal_model = xgb.train(plst, dtrain)

dtest = xgb.DMatrix(np.asmatrix(X_test),feature_names=feat_names)

optimal_model.save_model('sigdet.model')

XgBoost上的帖子(参阅链接),其中介绍了在c ++中加载和应用预测的正确方法:

I've followed a post on the XgBoost (see link) which explains the correct way to load and apply prediction in c++:

// Load Model
g_learner = std::make_unique<Learner>(Learner::Create({}));
        std::unique_ptr<dmlc::Stream> fi(
            dmlc::Stream::Create(filename, "r"));
        g_learner->Load(fi.get());

// Predict
    DMatrixHandle h_test;
        XGDMatrixCreateFromMat((float *)features, 1, numFeatures , -999.9f, &h_test);
        xgboost::bst_ulong out_len;


        std::vector<float> preds;
        g_learner->Predict((DMatrix*)h_test,true, &preds); 

我的问题(1):我需要创建一个DMatrix *,但是我只有一个DMatrixHandle。如何用我的数据正确创建DMatrix?

My problem (1): I need to create a DMatrix*, however I only have a DMatrixHandle. How do I properly create a DMatrix with my data?

我的问题(2):当我尝试以下预测方法时:

My problem (2): When I tried the following prediction method:

DMatrixHandle h_test;
XGDMatrixCreateFromMat((float *)features, 1, numFeatures , -999.9f, &h_test);
xgboost::bst_ulong out_len;


int res = XGBoosterPredict(g_modelHandle, h_test, 1, 0, &out_len, (const float**)&scores);

我得到的分数与加载完全相同的模型完全不同并使用它进行预测(在python中)。

I'm getting completely different scores than by loading the exact same model and using it for prediction (in python).

谁能帮助我在c ++上获得一致的结果,python可能会上天。顺便说一句,我需要在实时应用程序的c ++中应用预测,否则我将使用其他语言。

Whoever helps me achieve consistent results across c++ and python will probably go to heaven. BTW, I need to apply prediction in c++ for a real-time application, otherwise I would use a different language.

推荐答案

到获取DMatrix,您可以执行以下操作:

To get the DMatrix you can do this:

g_learner->Predict(static_cast<std::shared_ptr<xgboost::DMatrix>*>(h_test)->get(), true, &pred);

对于问题(2),我没有答案。这实际上是我遇到的相同问题。我在python中有了XGBRegression,并且在C ++中使用相同的功能获得了不同的结果。

For problem (2), I don't have an answer. This is actually the same problem I have. I've got a XGBRegression in python and I obtain different results with the same features in C++.

这篇关于C ++中的xgboost负载模型(python-> c ++预测分数不匹配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆