XGBoost/Booster.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« XGBoost/CAPI.pm ↑ Summary XGBoost/DMatrix.pm »
1 package AI::XGBoost::Booster;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 our $VERSION = '0.11'; # VERSION
8  
9 # ABSTRACT: XGBoost main class for training, prediction and evaluation
10  
11 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use AI::XGBoost::CAPI qw(:all);
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use namespace::autoclean;
14  
15 has _handle => ( is => 'rw',
16 init_arg => undef, );
17  
18 sub update {
19 my $self = shift;
20 my %args = @_;
21 my ( $iteration, $dtrain ) = @args{qw(iteration dtrain)};
22 XGBoosterUpdateOneIter( $self->_handle, $iteration, $dtrain->handle );
23 return $self;
24 }
25  
26 sub boost {
27 my $self = shift;
28 my %args = @_;
29 my ( $dtrain, $grad, $hess ) = @args{qw(dtrain grad hess)};
30 XGBoosterBoostOneIter( $self->_handle, $dtrain, $grad, $hess );
31 return $self;
32 }
33  
34 sub predict {
35 my $self = shift;
36 my %args = @_;
37 my $data = $args{'data'};
38 my $result = XGBoosterPredict( $self->_handle, $data->handle );
39 my $result_size = scalar @$result;
40 my $matrix_rows = $data->num_row;
41 if ( $result_size != $matrix_rows && $result_size % $matrix_rows == 0 ) {
42 my $col_size = $result_size / $matrix_rows;
43 return [ map { [ @$result[ $_ * $col_size .. $_ * $col_size + $col_size - 1 ] ] } 0 .. $matrix_rows - 1 ];
44 }
45 return $result;
46 }
47  
48 sub set_param {
49 my $self = shift;
50 my ( $name, $value ) = @_;
51 XGBoosterSetParam( $self->_handle, $name, $value );
52 return $self;
53 }
54  
55 sub set_attr {
56 my $self = shift;
57 my ( $name, $value ) = @_;
58 XGBoosterSetAttr( $self->_handle, $name, $value );
59 return $self;
60 }
61  
62 sub get_attr {
63 my $self = shift;
64 my ($name) = @_;
65 XGBoosterGetAttr( $self->_handle, $name );
66 }
67  
68 sub get_score {
69 my $self = shift;
70 my %args = @_;
71 my ( $fmap, $importance_type ) = @args{qw(fmap importance_type)};
72  
73 if ( $importance_type eq "weight" ) {
74 my @trees = $self->get_dump;
75 } else {
76  
77 }
78  
79 }
80  
81 sub get_dump {
82 my $self = shift;
83 return XGBoosterDumpModelEx( $self->_handle, "", 1, "text" );
84 }
85  
86 sub attributes {
87 my $self = shift;
88 return { map { $_ => $self->get_attr($_) } @{ XGBoosterGetAttrNames( $self->_handle ) } };
89 }
90  
91 sub TO_JSON {
92 my $self = shift;
93 my $trees = XGBoosterDumpModelEx( $self->_handle, "", 1, "json" );
94 return "[" . join( ',', @$trees ) . "]";
95 }
96  
97 sub BUILD {
98 my $self = shift;
99 my $args = shift;
100 $self->_handle( XGBoosterCreate( [ map { $_->handle } @{ $args->{'cache'} } ] ) );
101 }
102  
103 sub DEMOLISH {
104 my $self = shift();
105 XGBoosterFree( $self->_handle );
106 }
107  
108 __PACKAGE__->meta->make_immutable();
109  
110 1;
111  
112 __END__
113  
114 =pod
115  
116 =encoding utf-8
117  
118 =head1 NAME
119  
120 AI::XGBoost::Booster - XGBoost main class for training, prediction and evaluation
121  
122 =head1 VERSION
123  
124 version 0.11
125  
126 =head1 SYNOPSIS
127  
128 use 5.010;
129 use aliased 'AI::XGBoost::DMatrix';
130 use AI::XGBoost qw(train);
131
132 # We are going to solve a binary classification problem:
133 # Mushroom poisonous or not
134
135 my $train_data = DMatrix->From(file => 'agaricus.txt.train');
136 my $test_data = DMatrix->From(file => 'agaricus.txt.test');
137
138 # With XGBoost we can solve this problem using 'gbtree' booster
139 # and as loss function a logistic regression 'binary:logistic'
140 # (Gradient Boosting Regression Tree)
141 # XGBoost Tree Booster has a lot of parameters that we can tune
142 # (https://github.com/dmlc/xgboost/blob/master/doc/parameter.md)
143
144 my $booster = train(data => $train_data, number_of_rounds => 10, params => {
145 objective => 'binary:logistic',
146 eta => 1.0,
147 max_depth => 2,
148 silent => 1
149 });
150
151 # For binay classification predictions are probability confidence scores in [0, 1]
152 # indicating that the label is positive (1 in the first column of agaricus.txt.test)
153 my $predictions = $booster->predict(data => $test_data);
154
155 say join "\n", @$predictions[0 .. 10];
156  
157 =head1 DESCRIPTION
158  
159 Booster objects control training, prediction and evaluation
160  
161 Work In Progress, the API may change. Comments and suggestions are welcome!
162  
163 =head1 METHODS
164  
165 =head2 update
166  
167 Update one iteration
168  
169 =head3 Parameters
170  
171 =over 4
172  
173 =item iteration
174  
175 Current iteration number
176  
177 =item dtrain
178  
179 Training data (AI::XGBoost::DMatrix)
180  
181 =back
182  
183 =head2 boost
184  
185 Boost one iteration using your own gradient
186  
187 =head3 Parameters
188  
189 =over 4
190  
191 =item dtrain
192  
193 Training data (AI::XGBoost::DMatrix)
194  
195 =item grad
196  
197 Gradient of your objective function (Reference to an array)
198  
199 =item hess
200  
201 Hessian of your objective function, that is, second order gradient (Reference to an array)
202  
203 =back
204  
205 =head2 predict
206  
207 Predict data using the trained model
208  
209 =head3 Parameters
210  
211 =over 4
212  
213 =item data
214  
215 Data to predict
216  
217 =back
218  
219 =head2 set_param
220  
221 Set booster parameter
222  
223 =head3 Example
224  
225 $booster->set_param('objective', 'binary:logistic');
226  
227 =head2 set_attr
228  
229 Set a string attribute
230  
231 =head2 get_attr
232  
233 Get a string attribute
234  
235 =head2 get_score
236  
237 Get importance of each feature
238  
239 =head3 Parameters
240  
241 =over 4
242  
243 =item importance_type
244  
245 Type of importance. Valid values:
246  
247 =over 4
248  
249 =item weight
250  
251 Number of times a feature is used to split the data across all trees
252  
253 =item gain
254  
255 Average gain of the feature when it is used in trees
256  
257 =item cover
258  
259 Average coverage of the feature when it is used in trees
260  
261 =back
262  
263 =item fmap
264  
265 Name of feature map file
266  
267 =back
268  
269 =head2 get_dump
270  
271 =head2 attributes
272  
273 Returns all attributes of the booster as a HASHREF
274  
275 =head2 TO_JSON
276  
277 Serialize the booster to JSON.
278  
279 This method is to be used with the option C<convert_blessed> from L<JSON>.
280 (See L<https://metacpan.org/pod/JSON#OBJECT-SERIALISATION>)
281  
282 Warning: this API is subject to changes
283  
284 =head2 BUILD
285  
286 Use new, this method is just an internal helper
287  
288 =head2 DEMOLISH
289  
290 Internal destructor. This method is called automatically
291  
292 =head1 AUTHOR
293  
294 Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com>
295  
296 =head1 COPYRIGHT AND LICENSE
297  
298 Copyright (c) 2017 by Pablo Rodríguez González.
299  
300 =cut
« XGBoost/CAPI.pm ↑ Summary XGBoost/DMatrix.pm »