相关疑难解决方法(0)

c++ 中的 xgboost 加载模型(python -> c++ 预测分数不匹配)

我正在联系所有 SO C++ 天才。

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

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')
Run Code Online (Sandbox Code Playgroud)

我关注了 XgBoost 上的一篇文章(见链接),它解释了在 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); 
Run Code Online (Sandbox Code Playgroud)

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

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

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, …
Run Code Online (Sandbox Code Playgroud)

c++ python xgboost

6
推荐指数
1
解决办法
4873
查看次数

标签 统计

c++ ×1

python ×1

xgboost ×1