|
|
|
|
@@ -1,3 +1,4 @@
|
|
|
|
|
// Copyright (c) 2014 by Contributors
|
|
|
|
|
// implementations in ctypes
|
|
|
|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
|
|
|
#define _CRT_SECURE_NO_DEPRECATE
|
|
|
|
|
@@ -31,9 +32,11 @@ class Booster: public learner::BoostLearner {
|
|
|
|
|
this->init_model = false;
|
|
|
|
|
this->SetCacheData(mats);
|
|
|
|
|
}
|
|
|
|
|
inline const float *Pred(const DataMatrix &dmat, int option_mask, unsigned ntree_limit, bst_ulong *len) {
|
|
|
|
|
inline const float *Pred(const DataMatrix &dmat, int option_mask,
|
|
|
|
|
unsigned ntree_limit, bst_ulong *len) {
|
|
|
|
|
this->CheckInitModel();
|
|
|
|
|
this->Predict(dmat, (option_mask&1) != 0, &this->preds_, ntree_limit, (option_mask&2) != 0);
|
|
|
|
|
this->Predict(dmat, (option_mask&1) != 0, &this->preds_,
|
|
|
|
|
ntree_limit, (option_mask&2) != 0);
|
|
|
|
|
*len = static_cast<bst_ulong>(this->preds_.size());
|
|
|
|
|
return BeginPtr(this->preds_);
|
|
|
|
|
}
|
|
|
|
|
@@ -57,7 +60,7 @@ class Booster: public learner::BoostLearner {
|
|
|
|
|
this->init_model = true;
|
|
|
|
|
}
|
|
|
|
|
inline void LoadModelFromBuffer(const void *buf, size_t size) {
|
|
|
|
|
utils::MemoryFixSizeBuffer fs((void*)buf, size);
|
|
|
|
|
utils::MemoryFixSizeBuffer fs((void*)buf, size); // NOLINT(*)
|
|
|
|
|
learner::BoostLearner::LoadModel(fs, true);
|
|
|
|
|
this->init_model = true;
|
|
|
|
|
}
|
|
|
|
|
@@ -99,11 +102,10 @@ class Booster: public learner::BoostLearner {
|
|
|
|
|
|
|
|
|
|
using namespace xgboost::wrapper;
|
|
|
|
|
|
|
|
|
|
extern "C"{
|
|
|
|
|
void* XGDMatrixCreateFromFile(const char *fname, int silent) {
|
|
|
|
|
void* XGDMatrixCreateFromFile(const char *fname, int silent) {
|
|
|
|
|
return LoadDataMatrix(fname, silent != 0, false, false);
|
|
|
|
|
}
|
|
|
|
|
void* XGDMatrixCreateFromCSR(const bst_ulong *indptr,
|
|
|
|
|
}
|
|
|
|
|
void* XGDMatrixCreateFromCSR(const bst_ulong *indptr,
|
|
|
|
|
const unsigned *indices,
|
|
|
|
|
const float *data,
|
|
|
|
|
bst_ulong nindptr,
|
|
|
|
|
@@ -122,8 +124,8 @@ extern "C"{
|
|
|
|
|
}
|
|
|
|
|
mat.info.info.num_row = nindptr - 1;
|
|
|
|
|
return p_mat;
|
|
|
|
|
}
|
|
|
|
|
XGB_DLL void* XGDMatrixCreateFromCSC(const bst_ulong *col_ptr,
|
|
|
|
|
}
|
|
|
|
|
void* XGDMatrixCreateFromCSC(const bst_ulong *col_ptr,
|
|
|
|
|
const unsigned *indices,
|
|
|
|
|
const float *data,
|
|
|
|
|
bst_ulong nindptr,
|
|
|
|
|
@@ -138,9 +140,9 @@ extern "C"{
|
|
|
|
|
DMatrixSimple &mat = *p_mat;
|
|
|
|
|
utils::ParallelGroupBuilder<RowBatch::Entry> builder(&mat.row_ptr_, &mat.row_data_);
|
|
|
|
|
builder.InitBudget(0, nthread);
|
|
|
|
|
long ncol = static_cast<long>(nindptr - 1);
|
|
|
|
|
long ncol = static_cast<long>(nindptr - 1); // NOLINT(*)
|
|
|
|
|
#pragma omp parallel for schedule(static)
|
|
|
|
|
for (long i = 0; i < ncol; ++i) {
|
|
|
|
|
for (long i = 0; i < ncol; ++i) { // NOLINT(*)
|
|
|
|
|
int tid = omp_get_thread_num();
|
|
|
|
|
for (unsigned j = col_ptr[i]; j < col_ptr[i+1]; ++j) {
|
|
|
|
|
builder.AddBudget(indices[j], tid);
|
|
|
|
|
@@ -148,7 +150,7 @@ extern "C"{
|
|
|
|
|
}
|
|
|
|
|
builder.InitStorage();
|
|
|
|
|
#pragma omp parallel for schedule(static)
|
|
|
|
|
for (long i = 0; i < ncol; ++i) {
|
|
|
|
|
for (long i = 0; i < ncol; ++i) { // NOLINT(*)
|
|
|
|
|
int tid = omp_get_thread_num();
|
|
|
|
|
for (unsigned j = col_ptr[i]; j < col_ptr[i+1]; ++j) {
|
|
|
|
|
builder.Push(indices[j],
|
|
|
|
|
@@ -159,8 +161,8 @@ extern "C"{
|
|
|
|
|
mat.info.info.num_row = mat.row_ptr_.size() - 1;
|
|
|
|
|
mat.info.info.num_col = static_cast<size_t>(ncol);
|
|
|
|
|
return p_mat;
|
|
|
|
|
}
|
|
|
|
|
void* XGDMatrixCreateFromMat(const float *data,
|
|
|
|
|
}
|
|
|
|
|
void* XGDMatrixCreateFromMat(const float *data,
|
|
|
|
|
bst_ulong nrow,
|
|
|
|
|
bst_ulong ncol,
|
|
|
|
|
float missing) {
|
|
|
|
|
@@ -185,8 +187,8 @@ extern "C"{
|
|
|
|
|
mat.row_ptr_.push_back(mat.row_ptr_.back() + nelem);
|
|
|
|
|
}
|
|
|
|
|
return p_mat;
|
|
|
|
|
}
|
|
|
|
|
void* XGDMatrixSliceDMatrix(void *handle,
|
|
|
|
|
}
|
|
|
|
|
void* XGDMatrixSliceDMatrix(void *handle,
|
|
|
|
|
const int *idxset,
|
|
|
|
|
bst_ulong len) {
|
|
|
|
|
DMatrixSimple tmp;
|
|
|
|
|
@@ -231,80 +233,80 @@ extern "C"{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return p_ret;
|
|
|
|
|
}
|
|
|
|
|
void XGDMatrixFree(void *handle) {
|
|
|
|
|
}
|
|
|
|
|
void XGDMatrixFree(void *handle) {
|
|
|
|
|
delete static_cast<DataMatrix*>(handle);
|
|
|
|
|
}
|
|
|
|
|
void XGDMatrixSaveBinary(void *handle, const char *fname, int silent) {
|
|
|
|
|
}
|
|
|
|
|
void XGDMatrixSaveBinary(void *handle, const char *fname, int silent) {
|
|
|
|
|
SaveDataMatrix(*static_cast<DataMatrix*>(handle), fname, silent != 0);
|
|
|
|
|
}
|
|
|
|
|
void XGDMatrixSetFloatInfo(void *handle, const char *field, const float *info, bst_ulong len) {
|
|
|
|
|
}
|
|
|
|
|
void XGDMatrixSetFloatInfo(void *handle, const char *field, const float *info, bst_ulong len) {
|
|
|
|
|
std::vector<float> &vec =
|
|
|
|
|
static_cast<DataMatrix*>(handle)->info.GetFloatInfo(field);
|
|
|
|
|
vec.resize(len);
|
|
|
|
|
memcpy(BeginPtr(vec), info, sizeof(float) * len);
|
|
|
|
|
}
|
|
|
|
|
void XGDMatrixSetUIntInfo(void *handle, const char *field, const unsigned *info, bst_ulong len) {
|
|
|
|
|
}
|
|
|
|
|
void XGDMatrixSetUIntInfo(void *handle, const char *field, const unsigned *info, bst_ulong len) {
|
|
|
|
|
std::vector<unsigned> &vec =
|
|
|
|
|
static_cast<DataMatrix*>(handle)->info.GetUIntInfo(field);
|
|
|
|
|
vec.resize(len);
|
|
|
|
|
memcpy(BeginPtr(vec), info, sizeof(unsigned) * len);
|
|
|
|
|
}
|
|
|
|
|
void XGDMatrixSetGroup(void *handle, const unsigned *group, bst_ulong len) {
|
|
|
|
|
}
|
|
|
|
|
void XGDMatrixSetGroup(void *handle, const unsigned *group, bst_ulong len) {
|
|
|
|
|
DataMatrix *pmat = static_cast<DataMatrix*>(handle);
|
|
|
|
|
pmat->info.group_ptr.resize(len + 1);
|
|
|
|
|
pmat->info.group_ptr[0] = 0;
|
|
|
|
|
for (uint64_t i = 0; i < len; ++i) {
|
|
|
|
|
pmat->info.group_ptr[i+1] = pmat->info.group_ptr[i] + group[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const float* XGDMatrixGetFloatInfo(const void *handle, const char *field, bst_ulong* len) {
|
|
|
|
|
}
|
|
|
|
|
const float* XGDMatrixGetFloatInfo(const void *handle, const char *field, bst_ulong* len) {
|
|
|
|
|
const std::vector<float> &vec =
|
|
|
|
|
static_cast<const DataMatrix*>(handle)->info.GetFloatInfo(field);
|
|
|
|
|
*len = static_cast<bst_ulong>(vec.size());
|
|
|
|
|
return BeginPtr(vec);
|
|
|
|
|
}
|
|
|
|
|
const unsigned* XGDMatrixGetUIntInfo(const void *handle, const char *field, bst_ulong* len) {
|
|
|
|
|
}
|
|
|
|
|
const unsigned* XGDMatrixGetUIntInfo(const void *handle, const char *field, bst_ulong* len) {
|
|
|
|
|
const std::vector<unsigned> &vec =
|
|
|
|
|
static_cast<const DataMatrix*>(handle)->info.GetUIntInfo(field);
|
|
|
|
|
*len = static_cast<bst_ulong>(vec.size());
|
|
|
|
|
return BeginPtr(vec);
|
|
|
|
|
}
|
|
|
|
|
bst_ulong XGDMatrixNumRow(const void *handle) {
|
|
|
|
|
}
|
|
|
|
|
bst_ulong XGDMatrixNumRow(const void *handle) {
|
|
|
|
|
return static_cast<bst_ulong>(static_cast<const DataMatrix*>(handle)->info.num_row());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// xgboost implementation
|
|
|
|
|
void *XGBoosterCreate(void *dmats[], bst_ulong len) {
|
|
|
|
|
// xgboost implementation
|
|
|
|
|
void *XGBoosterCreate(void *dmats[], bst_ulong len) {
|
|
|
|
|
std::vector<DataMatrix*> mats;
|
|
|
|
|
for (bst_ulong i = 0; i < len; ++i) {
|
|
|
|
|
DataMatrix *dtr = static_cast<DataMatrix*>(dmats[i]);
|
|
|
|
|
mats.push_back(dtr);
|
|
|
|
|
}
|
|
|
|
|
return new Booster(mats);
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterFree(void *handle) {
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterFree(void *handle) {
|
|
|
|
|
delete static_cast<Booster*>(handle);
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterSetParam(void *handle, const char *name, const char *value) {
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterSetParam(void *handle, const char *name, const char *value) {
|
|
|
|
|
static_cast<Booster*>(handle)->SetParam(name, value);
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterUpdateOneIter(void *handle, int iter, void *dtrain) {
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterUpdateOneIter(void *handle, int iter, void *dtrain) {
|
|
|
|
|
Booster *bst = static_cast<Booster*>(handle);
|
|
|
|
|
DataMatrix *dtr = static_cast<DataMatrix*>(dtrain);
|
|
|
|
|
bst->CheckInitModel();
|
|
|
|
|
bst->CheckInit(dtr);
|
|
|
|
|
bst->UpdateOneIter(iter, *dtr);
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterBoostOneIter(void *handle, void *dtrain,
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterBoostOneIter(void *handle, void *dtrain,
|
|
|
|
|
float *grad, float *hess, bst_ulong len) {
|
|
|
|
|
Booster *bst = static_cast<Booster*>(handle);
|
|
|
|
|
DataMatrix *dtr = static_cast<DataMatrix*>(dtrain);
|
|
|
|
|
bst->CheckInitModel();
|
|
|
|
|
bst->CheckInit(dtr);
|
|
|
|
|
bst->BoostOneIter(*dtr, grad, hess, len);
|
|
|
|
|
}
|
|
|
|
|
const char* XGBoosterEvalOneIter(void *handle, int iter, void *dmats[],
|
|
|
|
|
}
|
|
|
|
|
const char* XGBoosterEvalOneIter(void *handle, int iter, void *dmats[],
|
|
|
|
|
const char *evnames[], bst_ulong len) {
|
|
|
|
|
Booster *bst = static_cast<Booster*>(handle);
|
|
|
|
|
std::vector<std::string> names;
|
|
|
|
|
@@ -316,29 +318,31 @@ extern "C"{
|
|
|
|
|
bst->CheckInitModel();
|
|
|
|
|
bst->eval_str = bst->EvalOneIter(iter, mats, names);
|
|
|
|
|
return bst->eval_str.c_str();
|
|
|
|
|
}
|
|
|
|
|
const float *XGBoosterPredict(void *handle, void *dmat, int option_mask, unsigned ntree_limit, bst_ulong *len) {
|
|
|
|
|
return static_cast<Booster*>(handle)->Pred(*static_cast<DataMatrix*>(dmat), option_mask, ntree_limit, len);
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterLoadModel(void *handle, const char *fname) {
|
|
|
|
|
}
|
|
|
|
|
const float *XGBoosterPredict(void *handle, void *dmat, int option_mask,
|
|
|
|
|
unsigned ntree_limit, bst_ulong *len) {
|
|
|
|
|
return static_cast<Booster*>(handle)->Pred(*static_cast<DataMatrix*>(dmat),
|
|
|
|
|
option_mask, ntree_limit, len);
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterLoadModel(void *handle, const char *fname) {
|
|
|
|
|
static_cast<Booster*>(handle)->LoadModel(fname);
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterSaveModel(void *handle, const char *fname) {
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterSaveModel(void *handle, const char *fname) {
|
|
|
|
|
Booster *bst = static_cast<Booster*>(handle);
|
|
|
|
|
bst->CheckInitModel();
|
|
|
|
|
bst->SaveModel(fname, false);
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterLoadModelFromBuffer(void *handle, const void *buf, bst_ulong len) {
|
|
|
|
|
}
|
|
|
|
|
void XGBoosterLoadModelFromBuffer(void *handle, const void *buf, bst_ulong len) {
|
|
|
|
|
static_cast<Booster*>(handle)->LoadModelFromBuffer(buf, len);
|
|
|
|
|
}
|
|
|
|
|
const char *XGBoosterGetModelRaw(void *handle, bst_ulong *out_len) {
|
|
|
|
|
}
|
|
|
|
|
const char *XGBoosterGetModelRaw(void *handle, bst_ulong *out_len) {
|
|
|
|
|
return static_cast<Booster*>(handle)->GetModelRaw(out_len);
|
|
|
|
|
}
|
|
|
|
|
const char** XGBoosterDumpModel(void *handle, const char *fmap, int with_stats, bst_ulong *len){
|
|
|
|
|
}
|
|
|
|
|
const char** XGBoosterDumpModel(void *handle, const char *fmap,
|
|
|
|
|
int with_stats, bst_ulong *len) {
|
|
|
|
|
utils::FeatMap featmap;
|
|
|
|
|
if (strlen(fmap) != 0) {
|
|
|
|
|
featmap.LoadText(fmap);
|
|
|
|
|
}
|
|
|
|
|
return static_cast<Booster*>(handle)->GetModelDump(featmap, with_stats != 0, len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|