File Coverage

blib/lib/AI/XGBoost/DMatrix.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package AI::XGBoost::DMatrix;
2              
3 1     1   52516 use strict;
  1         2  
  1         28  
4 1     1   7 use warnings;
  1         9  
  1         47  
5 1     1   5 use utf8;
  1         2  
  1         10  
6              
7             our $VERSION = '0.1'; # VERSION
8              
9             # ABSTRACT: XGBoost class for data
10              
11 1     1   402 use Moose;
  1         394483  
  1         7  
12 1     1   6890 use AI::XGBoost::CAPI qw(:all);
  0            
  0            
13             use Carp;
14             use namespace::autoclean;
15              
16             has handle => ( is => 'ro', );
17              
18             sub From {
19             my ( $package, %args ) = @_;
20             return __PACKAGE__->FromFile( filename => $args{file}, silent => $args{silent} ) if ( defined $args{file} );
21             return __PACKAGE__->FromMat( map { $_ => $args{$_} if defined $_ } qw(matrix missing label) )
22             if ( defined $args{matrix} );
23             Carp::cluck( "I don't know how to build a " . __PACKAGE__ . " with this data: " . join( ", ", %args ) );
24             }
25              
26             sub FromFile {
27             my ( $package, %args ) = @_;
28             my $handle = XGDMatrixCreateFromFile( @args{qw(filename silent)} );
29             return __PACKAGE__->new( handle => $handle );
30             }
31              
32             sub FromMat {
33             my ( $package, %args ) = @_;
34             my $handle = XGDMatrixCreateFromMat( @args{qw(matrix missing)} );
35             my $matrix = __PACKAGE__->new( handle => $handle );
36             if ( defined $args{label} ) {
37             $matrix->set_label( $args{label} );
38             }
39             return $matrix;
40             }
41              
42             sub set_float_info {
43             my $self = shift();
44             my ( $field, $info ) = @_;
45             XGDMatrixSetFloatInfo( $self->handle, $field, $info );
46             return $self;
47             }
48              
49             sub set_float_info_pdl {
50             my $self = shift();
51             my ( $field, $info ) = @_;
52             XGDMatrixSetFloatInfo( $self->handle, $field, $info->flat()->unpdl() );
53             return $self;
54             }
55              
56             sub get_float_info {
57             my $self = shift();
58             my $field = shift();
59             XGDMatrixGetFloatInfo( $self->handle, $field );
60             }
61              
62             sub set_uint_info {
63             my $self = shift();
64             my ( $field, $info ) = @_;
65             XGDMatrixSetUintInfo( $self->handle, $field, $info );
66             return $self;
67             }
68              
69             sub get_uint_info {
70             my $self = shift();
71             my $field = shift();
72             XGDMatrixGetUintInfo( $self->handle, $field );
73             }
74              
75             sub save_binary {
76             my $self = shift();
77             my ( $filename, $silent ) = @_;
78             $silent //= 1;
79             XGDMatrixSaveBinary( $self->handle, $filename, $silent );
80             return $self;
81             }
82              
83             sub set_label {
84             my $self = shift();
85             my $label = shift();
86             $self->set_float_info( 'label', $label );
87             }
88              
89             sub set_label_pdl {
90             my $self = shift();
91             my $label = shift();
92             $self->set_float_info_pdl( 'label', $label->flat()->unpdl() );
93             }
94              
95             sub get_label {
96             my $self = shift();
97             $self->get_float_info('label');
98             }
99              
100             sub set_weight {
101             my $self = shift();
102             my $weight = shift();
103             $self->set_float_info( 'weight', $weight );
104             return $self;
105             }
106              
107             sub set_weight_pdl {
108             my $self = shift();
109             my $weight = shift();
110             $self->set_float_info( 'weight', $weight->flat()->unpdl() );
111             return $self;
112             }
113              
114             sub get_weight {
115             my $self = shift();
116             $self->get_float_info('weight');
117             }
118              
119             sub set_base_margin {
120             my $self = shift();
121             my $margin = shift();
122             $self->set_float_info( 'base_margin', $margin );
123             return $self;
124             }
125              
126             sub get_base_margin {
127             my $self = shift();
128             $self->get_float_info('base_margin');
129             }
130              
131             sub set_group {
132             my $self = shift();
133             my $group = shift();
134             XGDMatrixSetGroup( $self->handle, $group );
135             return $self;
136             }
137              
138             sub num_row {
139             my $self = shift();
140             XGDMatrixNumRow( $self->handle );
141             }
142              
143             sub num_col {
144             my $self = shift();
145             XGDMatrixNumCol( $self->handle );
146             }
147              
148             sub dims {
149             my $self = shift();
150             return ( $self->num_row(), $self->num_col() );
151             }
152              
153             sub slice {
154             my $self = shift;
155             my ($list_of_indices) = @_;
156             my $handle = XGDMatrixSliceDMatrix( $self->handle(), $list_of_indices );
157             return __PACKAGE__->new( handle => $handle );
158             }
159              
160             sub DEMOLISH {
161             my $self = shift();
162             XGDMatrixFree( $self->handle );
163             }
164              
165             __PACKAGE__->meta->make_immutable();
166              
167             1;
168              
169             __END__
170              
171             =pod
172              
173             =encoding utf-8
174              
175             =head1 NAME
176              
177             AI::XGBoost::DMatrix - XGBoost class for data
178              
179             =head1 VERSION
180              
181             version 0.1
182              
183             =head1 SYNOPSIS
184              
185             use aliased 'AI::XGBoost::DMatrix';
186             my $train_data = DMatrix->FromFile(filename => 'agaricus.txt.train');
187              
188             =head1 DESCRIPTION
189              
190             XGBoost DMatrix perl model
191              
192             Work In Progress, the API may change. Comments and suggestions are welcome!
193              
194             =head1 METHODS
195              
196             =head2 From
197              
198             Construct a DMatrix from diferent sources. Based on parameters
199             dispatch to the correct From* method
200              
201             Refer to From* to see what can be done.
202              
203             =head2 FromFile
204              
205             Construct a DMatrix from a file
206              
207             =head3 Parameters
208              
209             =over 4
210              
211             =item filename
212              
213             File to read
214              
215             =item silent
216              
217             Supress messages
218              
219             =back
220              
221             =head2 FromMat
222              
223             Construct a DMatrix from a bidimensional array
224              
225             =head3 Parameters
226              
227             =over 4
228              
229             =item matrix
230              
231             Bidimensional array
232              
233             =item label
234              
235             Array with the labels of the rows of matrix. Optional
236              
237             =item missing
238              
239             Value to identify missing values. Optional, default `NaN`
240              
241             =back
242              
243             =head2 set_float_info
244              
245             Set float type property
246              
247             =head3 Parameters
248              
249             =over 4
250              
251             =item field
252              
253             Field name of the information
254              
255             =item info
256              
257             array with the information
258              
259             =back
260              
261             =head2 set_float_info_pdl
262              
263             Set float type property
264              
265             =head3 Parameters
266              
267             =over 4
268              
269             =item field
270              
271             Field name of the information
272              
273             =item info
274              
275             Piddle with the information
276              
277             =back
278              
279             =head2 get_float_info
280              
281             Get float type property
282              
283             =head3 Parameters
284              
285             =over 4
286              
287             =item field
288              
289             Field name of the information
290              
291             =back
292              
293             =head2 set_uint_info
294              
295             Set uint type property
296              
297             =head3 Parameters
298              
299             =over 4
300              
301             =item field
302              
303             Field name of the information
304              
305             =item info
306              
307             array with the information
308              
309             =back
310              
311             =head2 get_uint_info
312              
313             Get uint type property
314              
315             =head3 Parameters
316              
317             =over 4
318              
319             =item field
320              
321             Field name of the information
322              
323             =back
324              
325             =head2 save_binary
326              
327             Save DMatrix object as a binary file.
328              
329             This file should be used with L<FromFile>
330              
331             =head3 Parameters
332              
333             =over 4
334              
335             =item filename
336              
337             Filename and path
338              
339             =item silent
340              
341             Don't show information messages, optional, default 1
342              
343             =back
344              
345             =head2 set_label
346              
347             Set label of DMatrix. This label is the "classes" in classification problems
348              
349             =head3 Parameters
350              
351             =over 4
352              
353             =item data
354              
355             Array with the labels
356              
357             =back
358              
359             =head2 set_label_pdl
360              
361             Set label of DMatrix. This label is the "classes" in classification problems
362              
363             =head3 Parameters
364              
365             =over 4
366              
367             =item data
368              
369             Piddle with the labels
370              
371             =back
372              
373             =head2 get_label
374              
375             Get label of DMatrix. This label is the "classes" in classification problems
376              
377             =head2 set_weight
378              
379             Set weight of each instance
380              
381             =head3 Parameters
382              
383             =over 4
384              
385             =item weight
386              
387             Array with the weights
388              
389             =back
390              
391             =head2 set_weight_pdl
392              
393             Set weight of each instance
394              
395             =head3 Parameters
396              
397             =over 4
398              
399             =item weight
400              
401             pdl with the weights
402              
403             =back
404              
405             =head2 get_weight
406              
407             Get the weight of each instance
408              
409             =head2 set_base_margin
410              
411             Set base margin of booster to start from
412              
413             =head3 Parameters
414              
415             =over 4
416              
417             =item margin
418              
419             Array with the margins
420              
421             =back
422              
423             =head2 get_base_margin
424              
425             Get the base margin
426              
427             =head2 set_group
428              
429             Set group size
430              
431             =head3 Parameters
432              
433             =over 4
434              
435             =item group
436              
437             Array with the size of each group
438              
439             =back
440              
441             =head2 num_row
442              
443             Number of rows
444              
445             =head2 num_col
446              
447             Number of columns
448              
449             =head2 dims
450              
451             Dimensions of the matrix. That is: rows, columns
452              
453             =head2 slice
454              
455             Slice the DMatrix and return a new DMatrix tha only contains
456             the list of indices
457              
458             =head3 Parameters
459              
460             =over 4
461              
462             =item list_of_indices
463              
464             Reference to an array of indices
465              
466             =back
467              
468             =head2 DEMOLISH
469              
470             Free the DMatrix
471              
472             This method gets called automatically
473              
474             =head1 AUTHOR
475              
476             Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com>
477              
478             =head1 COPYRIGHT AND LICENSE
479              
480             Copyright (c) 2017 by Pablo Rodríguez González.
481              
482             =cut