AI-XGBoost
view release on metacpan or search on metacpan
lib/AI/XGBoost/CAPI.pm view on Meta::CPAN
my $out_len = 0;
my $out_result = 0;
my $ffi = FFI::Platypus->new();
my $number_of_features = scalar @$feature_names;
my $array_of_opaque_feature_names = [ map { $ffi->cast( string => "opaque", $_ ) } @$feature_names ];
my $array_of_opaque_feature_types = [ map { $ffi->cast( string => "opaque", $_ ) } @$feature_types ];
_CheckCall(
AI::XGBoost::CAPI::RAW::XGBoosterDumpModelWithFeatures( $booster, $number_of_features,
$array_of_opaque_feature_names,
$array_of_opaque_feature_types,
$with_stats, \$out_len, \$out_result
)
);
$out_result = $ffi->cast( opaque => "opaque[$out_len]", $out_result );
return [ map { $ffi->cast( opaque => "string", $_ ) } @$out_result ];
}
sub XGBoosterDumpModelExWithFeatures {
my ( $booster, $feature_names, $feature_types, $with_stats, $format ) = @_;
my $out_len = 0;
my $out_result = 0;
my $ffi = FFI::Platypus->new();
my $number_of_features = scalar @$feature_names;
my $array_of_opaque_feature_names = [ map { $ffi->cast( string => "opaque", $_ ) } @$feature_names ];
my $array_of_opaque_feature_types = [ map { $ffi->cast( string => "opaque", $_ ) } @$feature_types ];
_CheckCall(
AI::XGBoost::CAPI::RAW::XGBoosterDumpModelExWithFeatures( $booster, $number_of_features,
$array_of_opaque_feature_names,
$array_of_opaque_feature_types,
$with_stats, $format, \$out_len, \$out_result
)
);
$out_result = $ffi->cast( opaque => "opaque[$out_len]", $out_result );
return [ map { $ffi->cast( opaque => "string", $_ ) } @$out_result ];
}
sub XGBoosterFree {
my ($booster) = @_;
_CheckCall( AI::XGBoost::CAPI::RAW::XGBoosterFree($booster) );
return ();
}
# _CheckCall
#
# Check return code and if necesary, launch an exception
#
sub _CheckCall {
my ($return_code) = @_;
if ($return_code) {
my $error_message = AI::XGBoost::CAPI::RAW::XGBGetLastError();
XGBoostException->throw( error => $error_message );
}
}
1;
__END__
=pod
=encoding utf-8
=head1 NAME
AI::XGBoost::CAPI - Perl wrapper for XGBoost C API https://github.com/dmlc/xgboost
=head1 VERSION
version 0.11
=head1 SYNOPSIS
use 5.010;
use AI::XGBoost::CAPI qw(:all);
my $dtrain = XGDMatrixCreateFromFile('agaricus.txt.train');
my $dtest = XGDMatrixCreateFromFile('agaricus.txt.test');
my ($rows, $cols) = (XGDMatrixNumRow($dtrain), XGDMatrixNumCol($dtrain));
say "Train dimensions: $rows, $cols";
my $booster = XGBoosterCreate([$dtrain]);
for my $iter (0 .. 10) {
XGBoosterUpdateOneIter($booster, $iter, $dtrain);
}
my $predictions = XGBoosterPredict($booster, $dtest);
# say join "\n", @$predictions;
XGBoosterFree($booster);
XGDMatrixFree($dtrain);
XGDMatrixFree($dtest);
=head1 DESCRIPTION
Perlified wrapper for the C API
=head2 Error handling
XGBoost c api functions returns some int to signal the presence/absence of error.
In this module that is achieved using Exceptions from L<Exception::Class>
=head1 FUNCTIONS
=head2 XGDMatrixCreateFromFile
Load a data matrix
Parameters:
=over 4
=item filename
the name of the file
=item silent
whether print messages during loading
( run in 0.831 second using v1.01-cache-2.11-cpan-5837b0d9d2c )