AI-XGBoost
view release on metacpan or search on metacpan
lib/AI/XGBoost/CAPI.pm view on Meta::CPAN
use Exception::Class ( 'XGBoostException' );
our $VERSION = '0.11'; # VERSION
# ABSTRACT: Perl wrapper for XGBoost C API https://github.com/dmlc/xgboost
sub XGDMatrixCreateFromFile {
my ( $filename, $silent ) = @_;
$silent //= 1;
my $matrix = 0;
my $error = AI::XGBoost::CAPI::RAW::XGDMatrixCreateFromFile( $filename, $silent, \$matrix );
_CheckCall($error);
return $matrix;
}
sub XGDMatrixCreateFromMat {
my ( $data, $missing ) = @_;
$missing //= "NaN";
# TODO Support simple arrays
# TODO Support PDL
# TODO ¿Adapters?
my $data_adapter = [ map { @$_ } @$data ];
my $nrows = scalar @$data;
my $ncols = scalar @{ $data->[0] };
my $matrix = 0;
my $error = AI::XGBoost::CAPI::RAW::XGDMatrixCreateFromMat( $data_adapter, $nrows, $ncols, $missing, \$matrix );
_CheckCall($error);
return $matrix;
}
sub XGDMatrixNumRow {
my ($matrix) = @_;
my $rows = 0;
_CheckCall( AI::XGBoost::CAPI::RAW::XGDMatrixNumRow( $matrix, \$rows ) );
return $rows;
}
lib/AI/XGBoost/CAPI.pm view on Meta::CPAN
sub XGDMatrixSaveBinary {
my ( $matrix, $filename, $silent ) = @_;
$silent //= 1;
_CheckCall( AI::XGBoost::CAPI::RAW::XGDMatrixSaveBinary( $matrix, $filename, $silent ) );
}
sub XGDMatrixSliceDMatrix {
my ( $matrix, $list_of_indices ) = @_;
my $new_matrix = 0;
my $error = AI::XGBoost::CAPI::RAW::XGDMatrixSliceDMatrix( $matrix, $list_of_indices, scalar @$list_of_indices,
\$new_matrix );
_CheckCall($error);
return $new_matrix;
}
sub XGDMatrixFree {
my ($matrix) = @_;
_CheckCall( AI::XGBoost::CAPI::RAW::XGDMatrixFree($matrix) );
return ();
}
sub XGBoosterCreate {
lib/AI/XGBoost/CAPI.pm view on Meta::CPAN
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
lib/AI/XGBoost/CAPI.pm view on Meta::CPAN
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:
lib/AI/XGBoost/CAPI/RAW.pm view on Meta::CPAN
=head1 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
=head1 FUNCTIONS
=head2 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
=head2 XGDMatrixCreateFromFile
Load a data matrix
Parameters:
=over 4
=item filename
( run in 0.518 second using v1.01-cache-2.11-cpan-65fba6d93b7 )