blib/lib/AI/XGBoost.pm untested

filter: Statementstmt 0.0% 0 / 26 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 34 SCAR 17.9
CC
2
Cov
0%
train
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 6.0
1 package AI::XGBoost;
2 use strict;
3 use warnings;
4  
5 use AI::XGBoost::Booster;
6 use Exporter::Easy ( OK => ['train'] );
7  
8 our $VERSION = '0.1'; # VERSION
9  
10 # ABSTRACT: Perl wrapper for XGBoost library L<https://github.com/dmlc/xgboost>
11  
12 sub train {
13 my %args = @_;
14 my ( $params, $data, $number_of_rounds ) = @args{qw(params data number_of_rounds)};
15  
16 my $booster = AI::XGBoost::Booster->new( cache => [$data] );
17 if ( defined $params ) {
18 while ( my ( $name, $value ) = each %$params ) {
19 $booster->set_param( $name, $value );
20 }
21 }
22 for my $iteration ( 0 .. $number_of_rounds - 1 ) {
23 $booster->update( dtrain => $data, iteration => $iteration );
24 }
25 return $booster;
26 }
27  
28 1;
29  
30 __END__
31  
32 =pod
33  
34 =encoding utf-8
35  
36 =head1 NAME
37  
38 AI::XGBoost - Perl wrapper for XGBoost library L<https://github.com/dmlc/xgboost>
39  
40 =head1 VERSION
41  
42 version 0.1
43  
44 =head1 SYNOPSIS
45  
46 use 5.010;
47 use aliased 'AI::XGBoost::DMatrix';
48 use AI::XGBoost qw(train);
49
50 # We are going to solve a binary classification problem:
51 # Mushroom poisonous or not
52
53 my $train_data = DMatrix->From(file => 'agaricus.txt.train');
54 my $test_data = DMatrix->From(file => 'agaricus.txt.test');
55
56 # With XGBoost we can solve this problem using 'gbtree' booster
57 # and as loss function a logistic regression 'binary:logistic'
58 # (Gradient Boosting Regression Tree)
59 # XGBoost Tree Booster has a lot of parameters that we can tune
60 # (https://github.com/dmlc/xgboost/blob/master/doc/parameter.md)
61
62 my $booster = train(data => $train_data, number_of_rounds => 10, params => {
63 objective => 'binary:logistic',
64 eta => 1.0,
65 max_depth => 2,
66 silent => 1
67 });
68
69 # For binay classification predictions are probability confidence scores in [0, 1]
70 # indicating that the label is positive (1 in the first column of agaricus.txt.test)
71 my $predictions = $booster->predict(data => $test_data);
72
73 say join "\n", @$predictions[0 .. 10];
74  
75 use aliased 'AI::XGBoost::DMatrix';
76 use AI::XGBoost qw(train);
77 use Data::Dataset::Classic::Iris;
78
79 # We are going to solve a multiple classification problem:
80 # determining plant species using a set of flower's measures
81
82 # XGBoost uses number for "class" so we are going to codify classes
83 my %class = (
84 setosa => 0,
85 versicolor => 1,
86 virginica => 2
87 );
88
89 my $iris = Data::Dataset::Classic::Iris::get();
90
91 # Split train and test, label and features
92 my $train_dataset = [map {$iris->{$_}} grep {$_ ne 'species'} keys %$iris];
93 my $test_dataset = [map {$iris->{$_}} grep {$_ ne 'species'} keys %$iris];
94
95 sub transpose {
96 # Transposing without using PDL, Data::Table, Data::Frame or other modules
97 # to keep minimal dependencies
98 my $array = shift;
99 my @aux = ();
100 for my $row (@$array) {
101 for my $column (0 .. scalar @$row - 1) {
102 push @{$aux[$column]}, $row->[$column];
103 }
104 }
105 return \@aux;
106 }
107
108 $train_dataset = transpose($train_dataset);
109 $test_dataset = transpose($test_dataset);
110
111 my $train_label = [map {$class{$_}} @{$iris->{'species'}}];
112 my $test_label = [map {$class{$_}} @{$iris->{'species'}}];
113
114 my $train_data = DMatrix->From(matrix => $train_dataset, label => $train_label);
115 my $test_data = DMatrix->From(matrix => $test_dataset, label => $test_label);
116
117 # Multiclass problems need a diferent objective function and the number
118 # of classes, in this case we are using 'multi:softprob' and
119 # num_class => 3
120 my $booster = train(data => $train_data, number_of_rounds => 20, params => {
121 max_depth => 3,
122 eta => 0.3,
123 silent => 1,
124 objective => 'multi:softprob',
125 num_class => 3
126 });
127
128 my $predictions = $booster->predict(data => $test_data);
129  
130 =head1 DESCRIPTION
131  
132 Perl wrapper for XGBoost library.
133  
134 The easiest way to use the wrapper is using C<train>, but beforehand
135 you need the data to be used contained in a C<DMatrix> object
136  
137 This is a work in progress, feedback, comments, issues, suggestion and
138 pull requests are welcome!!
139  
140 Currently this module need the xgboost binary available in your system.
141 I'm going to make an Alien module for xgboost but meanwhile you need to
142 compile yourself xgboost: L<https://github.com/dmlc/xgboost>
143  
144 =head1 FUNCTIONS
145  
146 =head2 train
147  
148 Performs gradient boosting using the data and parameters passed
149  
150 Returns a trained AI::XGBoost::Booster used
151  
152 =head3 Parameters
153  
154 =over 4
155  
156 =item params
157  
158 Parameters for the booster object.
159  
160 Full list available: https://github.com/dmlc/xgboost/blob/master/doc/parameter.md
161  
162 =item data
163  
164 AI::XGBoost::DMatrix object used for training
165  
166 =item number_of_rounds
167  
168 Number of boosting iterations
169  
170 =back
171  
172 =head1 ROADMAP
173  
174 The goal is to make a full wrapper for XGBoost.
175  
176 =head2 VERSIONS
177  
178 =over 4
179  
180 =item 0.2
181  
182 Full C API "easy" to use, with PDL support as L<AI::XGBoost::CAPI>
183  
184 Easy means clients don't have to use L<FFI::Platypus> or modules dealing
185 with C structures
186  
187 =item 0.25
188  
189 Alien package for libxgboost.so/xgboost.dll
190  
191 =item 0.3
192  
193 Object oriented API Moose based with DMatrix and Booster classes
194  
195 =item 0.4
196  
197 Complete object oriented API
198  
199 =item 0.5
200  
201 Use perl signatures (L<https://metacpan.org/pod/distribution/perl/pod/perlexperiment.pod#Subroutine-signatures>)
202  
203 =back
204  
205 =head1 SEE ALSO
206  
207 =over 4
208  
209 =item L<AI::MXNet>
210  
211 =item L<FFI::Platypus>
212  
213 =item L<NativeCall>
214  
215 =back
216  
217 =head1 AUTHOR
218  
219 Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com>
220  
221 =head1 COPYRIGHT AND LICENSE
222  
223 Copyright (c) 2017 by Pablo Rodríguez González.
224  
225 =head1 CONTRIBUTOR
226  
227 =for stopwords Ruben
228  
229 Ruben <me@ruben.tech>
230  
231 =cut