xgboost c 模型推理

xgboost c 模型推理

#include "xgboost_so/include/xgboost/c_api.h"
#include<stdio.h>
#include<stdlib.h>

//gcc -o  xgboost xgboost.c -L. -lxgboost -Wimplicit-function-declaration
int main()
{
    
    
    /* load xgbooster */
	FILE* fp;
	fp = fopen("best.model", "rb");
	if (fp) {
    
    
		printf("open model file !!!\n");
	}
	else {
    
    
		printf("open model file failure!");
	}

	long file_len;
	void* data = 0;
	fseek(fp, 0L, SEEK_END);
	file_len = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
	if (!(data = malloc(file_len))) {
    
    
		printf("Memory malloc error!");
		free(data);
		data = NULL;
		return -1;
	}

	if (fread(data, 1, file_len, fp) != file_len) {
    
    
		return -1;
	}
	DMatrixHandle xx;
	BoosterHandle m_booster = NULL;
	XGBoosterCreate(&xx, 0, &m_booster);
	XGBoosterLoadModelFromBuffer(m_booster, data, file_len);



	int ret = 0;
	//convert to DMatrix
	DMatrixHandle dtest;
	float* local_results = NULL;
	const float test[] = {
    
     214.946,  48.070, 224.661,  62.630, 223.336,  16.0, 28.0, 33.0, 25.0, 27.0, 38.0, 40.0, 532.0}; //模型输入
	XGDMatrixCreateFromMat((float*)test, 1, 36, -1, &dtest); //模型维度创建

	// predict
	bst_ulong out_len = 0;
	const float* out_result = NULL;
	int n_print = 0;
	out_len = 0;
	out_result = NULL;
	ret = XGBoosterPredict(m_booster, dtest, 0, 0, &out_len, &out_result);

	printf("out_result is %f\n", *out_result);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42280271/article/details/126545958