AI-XGBoost
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
examples/basic.pl view on Meta::CPAN
use 5.010;
use aliased 'AI::XGBoost::DMatrix';
use AI::XGBoost qw(train);
# We are going to solve a binary classification problem:
# Mushroom poisonous or not
my $train_data = DMatrix->From(file => 'agaricus.txt.train');
my $test_data = DMatrix->From(file => 'agaricus.txt.test');
# With XGBoost we can solve this problem using 'gbtree' booster
# and as loss function a logistic regression 'binary:logistic'
# (Gradient Boosting Regression Tree)
# XGBoost Tree Booster has a lot of parameters that we can tune
# (https://github.com/dmlc/xgboost/blob/master/doc/parameter.md)
my $booster = train(data => $train_data, number_of_rounds => 10, params => {
objective => 'binary:logistic',
eta => 1.0,
max_depth => 2,
silent => 1
});
# For binay classification predictions are probability confidence scores in [0, 1]
# indicating that the label is positive (1 in the first column of agaricus.txt.test)
my $predictions = $booster->predict(data => $test_data);
say join "\n", @$predictions[0 .. 10];
examples/iris.pl view on Meta::CPAN
# XGBoost uses number for "class" so we are going to codify classes
my %class = (
setosa => 0,
versicolor => 1,
virginica => 2
);
my $iris = Data::Dataset::Classic::Iris::get();
# Split train and test, label and features
my $train_dataset = [map {$iris->{$_}} grep {$_ ne 'species'} keys %$iris];
my $test_dataset = [map {$iris->{$_}} grep {$_ ne 'species'} keys %$iris];
sub transpose {
# Transposing without using PDL, Data::Table, Data::Frame or other modules
# to keep minimal dependencies
my $array = shift;
my @aux = ();
for my $row (@$array) {
for my $column (0 .. scalar @$row - 1) {
push @{$aux[$column]}, $row->[$column];
}
}
return \@aux;
}
$train_dataset = transpose($train_dataset);
$test_dataset = transpose($test_dataset);
my $train_label = [map {$class{$_}} @{$iris->{'species'}}];
my $test_label = [map {$class{$_}} @{$iris->{'species'}}];
my $train_data = DMatrix->From(matrix => $train_dataset, label => $train_label);
my $test_data = DMatrix->From(matrix => $test_dataset, label => $test_label);
# Multiclass problems need a diferent objective function and the number
# of classes, in this case we are using 'multi:softprob' and
# num_class => 3
my $booster = train(data => $train_data, number_of_rounds => 20, params => {
max_depth => 3,
eta => 0.3,
silent => 1,
objective => 'multi:softprob',
num_class => 3
});
my $predictions = $booster->predict(data => $test_data);
lib/AI/XGBoost.pm view on Meta::CPAN
use AI::XGBoost::Booster;
use Exporter::Easy ( OK => ['train'] );
our $VERSION = '0.11'; # VERSION
# ABSTRACT: Perl wrapper for XGBoost library L<https://github.com/dmlc/xgboost>
sub train {
my %args = @_;
my ( $params, $data, $number_of_rounds ) = @args{qw(params data number_of_rounds)};
my $booster = AI::XGBoost::Booster->new( cache => [$data] );
if ( defined $params ) {
while ( my ( $name, $value ) = each %$params ) {
$booster->set_param( $name, $value );
}
}
for my $iteration ( 0 .. $number_of_rounds - 1 ) {
$booster->update( dtrain => $data, iteration => $iteration );
}
return $booster;
}
1;
__END__
=pod
misc/using_capi.c view on Meta::CPAN
#include <stdio.h>
#include <xgboost/c_api.h>
int main() {
DMatrixHandle dtrain;
DMatrixHandle dtest;
// Agaricus files can be found in XGBoost demo/data directory
// Original source: http://archive.ics.uci.edu/ml/datasets/mushroom
XGDMatrixCreateFromFile("agaricus.txt.test", 0, &dtest);
XGDMatrixCreateFromFile("agaricus.txt.train", 0, &dtrain);
DMatrixHandle cache[] = {dtrain};
BoosterHandle booster;
XGBoosterCreate(cache, 1, &booster);
for (int iter = 0; iter < 11; iter++) {
XGBoosterUpdateOneIter(booster, iter, dtrain);
}
bst_ulong out_len;
t/20-dmatrix.t view on Meta::CPAN
use utf8;
use Test::More tests => 6;
BEGIN {
use_ok('AI::XGBoost::DMatrix');
}
{
my $matrix = [ [ 1, 1 ] ];
my $data = AI::XGBoost::DMatrix->FromMat( matrix => $matrix );
is( $data->num_row, scalar @$matrix, 'DMatrix constructed has the right number of rows' );
is( $data->num_col, scalar @{ $matrix->[0] }, 'DMatrix constructed has the right number of cols' );
is_deeply( [ $data->dims ], [ 1, 2 ], 'DMatrix dim method returns correct dimensions' );
}
{
my $matrix = [ map { [$_] } 0 .. 9 ];
my $dmatrix = AI::XGBoost::DMatrix->FromMat( matrix => $matrix );
my $sliced_matrix = $dmatrix->slice( [ map { $_ % 2 ? $_ : () } 0 .. 9 ] );
is( $sliced_matrix->num_row, ( scalar @$matrix ) / 2, 'Sliced DMatrix has right number of rows' );
is( $sliced_matrix->num_col, scalar @{ $matrix->[0] }, 'Sliced DMatrix has right number of cols' );
}
view all matches for this distributionview release on metacpan - search on metacpan
( run in 1.118 second using v1.00-cache-2.02-grep-82fe00e-cpan-4673cadbf75 )