The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

AI::XGBoost::CAPI - Perl wrapper for XGBoost C API https://github.com/dmlc/xgboost

VERSION

version 0.001

SYNOPSIS

 use 5.010;
 use AI::XGBoost::CAPI;
 use FFI::Platypus;
 
 my $silent = 0;
 my ($dtrain, $dtest) = (0, 0);
 
 AI::XGBoost::CAPI::XGDMatrixCreateFromFile('agaricus.txt.test', $silent, \$dtest);
 AI::XGBoost::CAPI::XGDMatrixCreateFromFile('agaricus.txt.train', $silent, \$dtrain);
 
 my ($rows, $cols) = (0, 0);
 AI::XGBoost::CAPI::XGDMatrixNumRow($dtrain, \$rows);
 AI::XGBoost::CAPI::XGDMatrixNumCol($dtrain, \$cols);
 say "Dimensions: $rows, $cols";
 
 my $booster = 0;
 
 AI::XGBoost::CAPI::XGBoosterCreate( [$dtrain] , 1, \$booster);
 
 for my $iter (0 .. 10) {
     AI::XGBoost::CAPI::XGBoosterUpdateOneIter($booster, $iter, $dtrain);
 }
 
 my $out_len = 0;
 my $out_result = 0;
 
 AI::XGBoost::CAPI::XGBoosterPredict($booster, $dtest, 0, 0, \$out_len, \$out_result);
 my $ffi = FFI::Platypus->new();
 my $predictions = $ffi->cast(opaque => "float[$out_len]", $out_result);
 
 #say join "\n", @$predictions;
 
 AI::XGBoost::CAPI::XGBoosterFree($booster);

DESCRIPTION

Wrapper for the C API.

The doc for the methods is extracted from doxygen comments: https://github.com/dmlc/xgboost/blob/master/include/xgboost/c_api.h

FUNCTIONS

XGBGetLastError

Get string message of the last error

All functions in this file will return 0 when success and -1 when an error occurred, XGBGetLastError can be called to retrieve the error

This function is thread safe and can be called by different thread

Returns string error information

XGDMatrixCreateFromFile

Load a data matrix

Parameters:

filename

the name of the file

silent

whether print messages during loading

out

a loaded data matrix

XGDMatrixNumRow

Get number of rows.

Parameters:

handle

the handle to the DMatrix

out

The address to hold number of rows.

XGDMatrixNumCol

Get number of cols.

Parameters:

handle

the handle to the DMatrix

out

The address to hold number of cols.

XGBoosterCreate

Create xgboost learner

Parameters:

dmats

matrices that are set to be cached

len

length of dmats

out

handle to the result booster

XGBoosterFree

Free obj in handle

Parameters:

handle

handle to be freed

XGBoosterUpdateOneIter

Update the model in one round using dtrain

Parameters:

handle

handle

iter

current iteration rounds

dtrain

training data

XGBoosterPredict

Make prediction based on dmat

Parameters:

handle

handle

dmat

data matrix

option_mask

bit-mask of options taken in prediction, possible values

  • 0: normal prediction

  • 1: output margin instead of transformed value

  • 2: output leaf index of trees instead of leaf value, note leaf index is unique per tree

  • 4: output feature contributions to individual predictions

ntree_limit

limit number of trees used for prediction, this is only valid for boosted trees when the parameter is set to 0, we will use all the trees

out_len

used to store length of returning result

out_result

used to set a pointer to array

AUTHOR

Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com>

COPYRIGHT AND LICENSE

Copyright (c) 2017 by Pablo Rodríguez González.

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004