xgboost vs2015 c语言环境搭建

首先,我们从git上下载xgboost的源码:

git clone --recursive https://github.com/dmlc/xgboost

对于window用户,打开git shell,输入以下命令

git submodule init

git submodule update

mkdir build

cd build

编译x86版本:cmake .. -G"Visual Studio 14 2015"

编译Win 64版本:cmake .. -G"Visual Studio 14 2015 Win64"

编译ARM版本:cmake .. -G"Visual Studio 14 2015 ARM"

这里我们选择x86版本编译

编译完之后build中有如下文件结构

使用vs2015打开xgboost.sln,点击Build,默认只有7项勾选,我们选择将所有的项勾选,点击Build Solution开始编译。

 

 

编译完成之后,将/xgboost/lib/下的xgboost.dll复制到C:\Windows\System32和C:\Windows\SysWOW64目录下(不执行此步可能会出现“应用程序无法正常启动 0x0000007b”错误)

接下来创建一个新的win32控制台应用程序test_xgboost进行测试

配置依赖库

在创建好的项目上右键->Properties,配置如下图所示的包含目录和库目录,再在Linker->input中添加xgboost.lib与rabit.lib依赖

创建test_xgboost.cpp文件输入一下代码

#include "stdafx.h"
#include<iostream>
#include<xgboost/c_api.h>
void test();
int main()
{
	test();
	system("pause");
    return 0;
}

void test()
{
	const int cols = 3, rows = 5;
	float train[rows][cols];
	for (int i = 0; i<rows; i++)
		for (int j = 0; j<cols; j++)
			train[i][j] = (i + 1) * (j + 1);

	float train_labels[rows];
	for (int i = 0; i<rows; i++)
		train_labels[i] = 1 + i*i*i;


	// convert to DMatrix
	DMatrixHandle h_train[1];
	XGDMatrixCreateFromMat((float*) train, rows, cols, -1, &h_train[0]);

	// load the labels
	XGDMatrixSetFloatInfo(h_train[0], "label", train_labels, rows);

	// read back the labels, just a sanity check
	bst_ulong bst_result;
	const float *out_floats;
	XGDMatrixGetFloatInfo(h_train[0], "label", &bst_result, &out_floats);
	for (unsigned int i = 0; i<bst_result; i++)
		std::cout << "label[" << i << "]=" << out_floats[i] << std::endl;

	// create the booster and load some parameters
	BoosterHandle h_booster;
	XGBoosterCreate(h_train, 1, &h_booster);
	XGBoosterSetParam(h_booster, "booster", "gbtree");
	XGBoosterSetParam(h_booster, "objective", "reg:linear");
	XGBoosterSetParam(h_booster, "max_depth", "5");
	XGBoosterSetParam(h_booster, "eta", "0.1");
	XGBoosterSetParam(h_booster, "min_child_weight", "1");
	XGBoosterSetParam(h_booster, "subsample", "0.5");
	XGBoosterSetParam(h_booster, "colsample_bytree", "1");
	XGBoosterSetParam(h_booster, "num_parallel_tree", "1");

	// perform 200 learning iterations
	for (int iter = 0; iter<200; iter++)
		XGBoosterUpdateOneIter(h_booster, iter, h_train[0]);

	// predict
	const int sample_rows = 5;
	float test[sample_rows][cols];
	for (int i = 0; i<sample_rows; i++)
		for (int j = 0; j<cols; j++)
			test[i][j] = (i + 1) * (j + 1);
	DMatrixHandle h_test;
	XGDMatrixCreateFromMat((float *)test, sample_rows, cols, -1, &h_test);
	bst_ulong out_len;
	const float *f;
	XGBoosterPredict(h_booster, h_test, 0, 0, &out_len, &f);

	for (unsigned int i = 0; i<out_len; i++)
		std::cout << "prediction[" << i << "]=" << f[i] << std::endl;
	// free xgboost internal structures
	XGDMatrixFree(h_train[0]);
	XGDMatrixFree(h_test);
	XGBoosterFree(h_booster);
	return;
}

运行测试文件发现报uint64_t错误,需要在c_api.h中添加#include<stdint.h>。

再次运行测试文件得到测试结果

xgboost安装参考自http://xgboost.apachecn.org/cn/latest/build.html

测试代码参考:https://stackoverflow.com/questions/36071672/using-xgboost-in-c

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值