Add DMatrix usage examples to c-api-demo (#5854)
* Add DMatrix usage examples to c-api-demo * Add XGDMatrixCreateFromCSREx example * Add XGDMatrixCreateFromCSCEx example
This commit is contained in:
parent
2c4dedb7a0
commit
03b8fdec74
@ -5,6 +5,7 @@
|
|||||||
* \brief A simple example of using xgboost C API.
|
* \brief A simple example of using xgboost C API.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <xgboost/c_api.h>
|
#include <xgboost/c_api.h>
|
||||||
@ -84,6 +85,86 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
{
|
||||||
|
printf("Dense Matrix Example (XGDMatrixCreateFromMat): ");
|
||||||
|
|
||||||
|
const float values[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0,
|
||||||
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
|
||||||
|
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
|
||||||
|
1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
1, 0, 0, 0, 0, 1, 0, 0, 0, 0};
|
||||||
|
|
||||||
|
DMatrixHandle dmat;
|
||||||
|
safe_xgboost(XGDMatrixCreateFromMat(values, 1, 127, 0.0, &dmat));
|
||||||
|
|
||||||
|
bst_ulong out_len = 0;
|
||||||
|
const float* out_result = NULL;
|
||||||
|
|
||||||
|
safe_xgboost(XGBoosterPredict(booster, dmat, 0, 0, 0, &out_len,
|
||||||
|
&out_result));
|
||||||
|
assert(out_len == 1);
|
||||||
|
|
||||||
|
printf("%1.4f \n", out_result[0]);
|
||||||
|
safe_xgboost(XGDMatrixFree(dmat));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
printf("Sparse Matrix Example (XGDMatrixCreateFromCSREx): ");
|
||||||
|
|
||||||
|
const size_t indptr[] = {0, 22};
|
||||||
|
const unsigned indices[] = {1, 9, 19, 21, 24, 34, 36, 39, 42, 53, 56, 65,
|
||||||
|
69, 77, 86, 88, 92, 95, 102, 106, 117, 122};
|
||||||
|
const float data[] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
|
||||||
|
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
|
||||||
|
|
||||||
|
DMatrixHandle dmat;
|
||||||
|
safe_xgboost(XGDMatrixCreateFromCSREx(indptr, indices, data, 2, 22, 127,
|
||||||
|
&dmat));
|
||||||
|
|
||||||
|
bst_ulong out_len = 0;
|
||||||
|
const float* out_result = NULL;
|
||||||
|
|
||||||
|
safe_xgboost(XGBoosterPredict(booster, dmat, 0, 0, 0, &out_len,
|
||||||
|
&out_result));
|
||||||
|
assert(out_len == 1);
|
||||||
|
|
||||||
|
printf("%1.4f \n", out_result[0]);
|
||||||
|
safe_xgboost(XGDMatrixFree(dmat));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
printf("Sparse Matrix Example (XGDMatrixCreateFromCSCEx): ");
|
||||||
|
|
||||||
|
const size_t col_ptr[] = {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
|
||||||
|
2, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8,
|
||||||
|
8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11, 11, 11, 11,
|
||||||
|
11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14,
|
||||||
|
14, 14, 14, 14, 14, 14, 15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18,
|
||||||
|
18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
|
||||||
|
20, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22};
|
||||||
|
|
||||||
|
const unsigned indices[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0};
|
||||||
|
|
||||||
|
const float data[] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
|
||||||
|
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
|
||||||
|
|
||||||
|
DMatrixHandle dmat;
|
||||||
|
safe_xgboost(XGDMatrixCreateFromCSCEx(col_ptr, indices, data, 128, 22, 1,
|
||||||
|
&dmat));
|
||||||
|
|
||||||
|
bst_ulong out_len = 0;
|
||||||
|
const float* out_result = NULL;
|
||||||
|
|
||||||
|
safe_xgboost(XGBoosterPredict(booster, dmat, 0, 0, 0, &out_len,
|
||||||
|
&out_result));
|
||||||
|
assert(out_len == 1);
|
||||||
|
|
||||||
|
printf("%1.4f \n", out_result[0]);
|
||||||
|
safe_xgboost(XGDMatrixFree(dmat));
|
||||||
|
}
|
||||||
|
|
||||||
// free everything
|
// free everything
|
||||||
safe_xgboost(XGBoosterFree(booster));
|
safe_xgboost(XGBoosterFree(booster));
|
||||||
safe_xgboost(XGDMatrixFree(dtrain));
|
safe_xgboost(XGDMatrixFree(dtrain));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user