AI-XGBoost
view release on metacpan or search on metacpan
lib/AI/XGBoost/CAPI.pm view on Meta::CPAN
my ( $booster, $iter, $matrices, $matrices_names ) = @_;
my $out_result = 0;
my $number_of_matrices = scalar @$matrices;
my $ffi = FFI::Platypus->new();
my $array_of_opaque_matrices_names = [ map { $ffi->cast( string => "opaque", $_ ) } @$matrices_names ];
_CheckCall(
AI::XGBoost::CAPI::RAW::XGBoosterEvalOneIter(
$booster, $iter, $matrices, $array_of_opaque_matrices_names,
$number_of_matrices, \$out_result
)
);
$out_result = $ffi->cast( opaque => "opaque[$number_of_matrices]", $out_result );
return [ map { $ffi->cast( opaque => "string", $_ ) } @$out_result ];
}
sub XGBoosterPredict {
my ( $booster, $data_matrix, $option_mask, $ntree_limit ) = @_;
$option_mask //= 0;
$ntree_limit //= 0;
my $out_len = 0;
my $out_result = 0;
_CheckCall(
AI::XGBoost::CAPI::RAW::XGBoosterPredict( $booster, $data_matrix, $option_mask,
$ntree_limit, \$out_len, \$out_result
)
);
my $ffi = FFI::Platypus->new();
return $ffi->cast( opaque => "float[$out_len]", $out_result );
}
sub XGBoosterDumpModel {
my ( $booster, $feature_map, $with_stats ) = @_;
$feature_map //= "";
$with_stats //= 1;
my $out_len = 0;
my $out_result = 0;
_CheckCall(
AI::XGBoost::CAPI::RAW::XGBoosterDumpModel( $booster, $feature_map, $with_stats, \$out_len, \$out_result ) );
my $ffi = FFI::Platypus->new();
$out_result = $ffi->cast( opaque => "opaque[$out_len]", $out_result );
return [ map { $ffi->cast( opaque => "string", $_ ) } @$out_result ];
}
sub XGBoosterDumpModelEx {
my ( $booster, $feature_map, $with_stats, $format ) = @_;
$feature_map //= "";
$with_stats //= 1;
my $out_len = 0;
my $out_result = 0;
_CheckCall(
AI::XGBoost::CAPI::RAW::XGBoosterDumpModelEx(
$booster, $feature_map, $with_stats, $format, \$out_len, \$out_result
)
);
my $ffi = FFI::Platypus->new();
$out_result = $ffi->cast( opaque => "opaque[$out_len]", $out_result );
return [ map { $ffi->cast( opaque => "string", $_ ) } @$out_result ];
}
sub XGBoosterDumpModelWithFeatures {
my ( $booster, $feature_names, $feature_types, $with_stats ) = @_;
$with_stats //= 1;
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;
( run in 1.029 second using v1.01-cache-2.11-cpan-df04353d9ac )