-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathxgboostpp.h
77 lines (63 loc) · 1.82 KB
/
xgboostpp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef __XGBOOSTPP_H__
#define __XGBOOSTPP_H__
#include <string>
#include <xgboost/c_api.h>
#include <math.h>
#include <memory>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <Eigen/Eigen>
#include <iostream>
class XGBoostPP
{
public:
typedef Eigen::Matrix<float, -1, -1, Eigen::RowMajor> Matrix;
template<typename M>
static void vector2Matrix(M& m, const typename M::Scalar * vec, Eigen::Index const rows, Eigen::Index const cols)
{
m = Eigen::Map<const Eigen::Matrix<typename M::Scalar, M::RowsAtCompileTime, M::ColsAtCompileTime, Eigen::RowMajor>>(vec, rows, cols);
}
XGBoostPP(std::string const& path, uint64_t nlabels):
_modelPath(path),
_nlabels(nlabels)
{
if (XGBoosterCreate(NULL, 0, &_booster) == 0 && XGBoosterLoadModel(_booster, _modelPath.c_str()) == 0){
//LOG HERE
}else{
//LOG HERE
_booster = NULL;
}
}
int predict(Matrix const& features, Matrix& result)
{
DMatrixHandle X;
const float* data = features.data();
auto const nrow = features.rows();
auto const nrow = features.cols();
XGDMatrixCreateFromMat(data, nrow, ncol, NAN, &X);
const float* out;
uint64_t l;
auto ret = XGBoosterPredict(_booster, X, 0, 0, 0, &l, &out);
if (ret < 0){
// LOG HERE
return -1;
}
XGDMatrixFree(X);
if (l != nrow*_nlabels){
//LOG HERE
return -1;
}
vector2Matrix(result, out, nrow, _nlabels);
return 0;
}
virtual ~XGBoostPP(){
XGBoosterFree(_booster);
}
private:
std::string const _modelPath;
BoosterHandle _booster;
uint64_t const _nlabels;
};
#endif