File Coverage

blib/lib/AI/XGBoost/CAPI/RAW.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 5 80.0
pod n/a
total 16 17 94.1


line stmt bran cond sub pod time code
1             package AI::XGBoost::CAPI::RAW;
2 3     3   136996 use strict;
  3         6  
  3         78  
3 3     3   12 use warnings;
  3         9  
  3         79  
4              
5 3     3   477 use parent 'NativeCall';
  3         466  
  3         15  
6              
7             our $VERSION = '0.1'; # VERSION
8              
9             # ABSTRACT: Perl wrapper for XGBoost C API https://github.com/dmlc/xgboost
10              
11 3     3   25497 sub XGBGetLastError : Args() : Native(xgboost) : Returns(string) { }
  3     0   2495  
  3         14  
12              
13             sub XGDMatrixCreateFromFile : Args(string, int, opaque*) : Native(xgboost) : Returns(int) { }
14              
15             sub XGDMatrixCreateFromCSREx : Args(size_t[], uint[], float[], size_t, size_t, size_t, opaque*) : Native(xgboost) :
16             Returns(int) { }
17              
18             sub XGDMatrixCreateFromCSCEx : Args(size_t[], uint[], float[], size_t, size_t, size_t, opaque*) : Native(xgboost) :
19             Returns(int) { }
20              
21             sub XGDMatrixCreateFromMat : Args(float[], uint64, uint64, float, opaque*) : Native(xgboost) : Returns(int) { }
22              
23             sub XGDMatrixCreateFromMat_omp : Args(float[], uint64, uint64, float, opaque*, int) : Native(xgboost) : Returns(int) { }
24              
25             sub XGDMatrixSliceDMatrix : Args(opaque, int[], uint64, opaque*) : Native(xgboost) : Returns(int) { }
26              
27             sub XGDMatrixNumRow : Args(opaque, uint64*) : Native(xgboost) : Returns(int) { }
28              
29             sub XGDMatrixNumCol : Args(opaque, uint64*) : Native(xgboost) : Returns(int) { }
30              
31             sub XGDMatrixSaveBinary : Args(opaque, string, int) : Native(xgboost) : Returns(int) { }
32              
33             sub XGDMatrixSetFloatInfo : Args(opaque, string, float[], uint64) : Native(xgboost) : Returns(int) { }
34              
35             sub XGDMatrixSetUIntInfo : Args(opaque, string, uint32 *, uint64) : Native(xgboost) : Returns(int) { }
36              
37             sub XGDMatrixSetGroup : Args(opaque, uint32 *, uint64) : Native(xgboost) : Returns(int) { }
38              
39             sub XGDMatrixGetFloatInfo : Args(opaque, string, uint64 *, opaque *) : Native(xgboost) : Returns(int) { }
40              
41             sub XGDMatrixGetUIntInfo : Args(opaque, string, uint64 *, opaque *) : Native(xgboost) : Returns(int) { }
42              
43             sub XGDMatrixFree : Args(opaque) : Native(xgboost) : Returns(int) { }
44              
45             sub XGBoosterCreate : Args(opaque[], uint64, opaque*) : Native(xgboost) : Returns(int) { }
46              
47             sub XGBoosterFree : Args(opaque) : Native(xgboost) : Returns(int) { }
48              
49             sub XGBoosterSetParam : Args(opaque, string, string) : Native(xgboost) : Returns(int) { }
50              
51             sub XGBoosterBoostOneIter : Args(opaque, opaque, float[], float[], uint64) : Native(xgboost) : Returns(int) { }
52              
53             sub XGBoosterUpdateOneIter : Args(opaque, int, opaque) : Native(xgboost) : Returns(int) { }
54              
55             sub XGBoosterEvalOneIter : Args(opaque, int, opaque[], opaque[], uint64, opaque*) : Native(xgboost) : Returns(int) { }
56              
57             sub XGBoosterPredict : Args(opaque, opaque, int, uint, uint64*, opaque*) : Native(xgboost) : Returns(int) { }
58              
59             sub XGBoosterLoadModel : Args(opaque, string) : Native(xgboost) : Returns(int) { }
60              
61             sub XGBoosterSaveModel : Args(opaque, string) : Native(xgboost) : Returns(int) { }
62              
63             sub XGBoosterLoadModelFromBuffer : Args(opaque, opaque, uint64) : Native(xgboost) : Returns(int) { }
64              
65             sub XGBoosterGetModelRaw : Args(opaque, uint64*, opaque*) : Native(xgboost) : Returns(int) { }
66              
67             sub XGBoosterDumpModel : Args(opaque, string, int, uint64*, opaque*) : Native(xgboost) : Returns(int) { }
68              
69             sub XGBoosterDumpModelEx : Args(opaque, string, int, string, uint64*, opaque*) : Native(xgboost) : Returns(int) { }
70              
71             sub XGBoosterDumpModelWithFeatures : Args(opaque, int, opaque[], opaque[], int, uint64*, opaque*) Native(xgboost) :
72             Returns(int) { }
73              
74             sub XGBoosterDumpModelExWithFeatures : Args(opaque, int, opaque[], opaque[], int, string, uint64*, opaque*)
75             Native(xgboost) : Returns(int) { }
76              
77             sub XGBoosterSetAttr : Args(opaque, string, string) : Native(xgboost) : Returns(int) { }
78              
79             sub XGBoosterGetAttr : Args(opaque, string, opaque*, int*) : Native(xgboost) : Returns(int) { }
80              
81             sub XGBoosterGetAttrNames : Args(opaque, uint64*, opaque*) : Native(xgboost) : Returns(int) { }
82              
83             sub XGBoosterLoadRabitCheckpoint : Args(opaque, int) : Native(xgboost) : Returns(int) { }
84              
85             sub XGBoosterSaveRabitCheckpoint : Args(opaque) : Native(xgboost) : Returns(int) { }
86              
87             1;
88              
89             __END__
90              
91             =pod
92              
93             =encoding utf-8
94              
95             =head1 NAME
96              
97             AI::XGBoost::CAPI::RAW - Perl wrapper for XGBoost C API https://github.com/dmlc/xgboost
98              
99             =head1 VERSION
100              
101             version 0.1
102              
103             =head1 SYNOPSIS
104              
105             use 5.010;
106             use AI::XGBoost::CAPI::RAW;
107             use FFI::Platypus;
108            
109             my $silent = 0;
110             my ($dtrain, $dtest) = (0, 0);
111            
112             AI::XGBoost::CAPI::RAW::XGDMatrixCreateFromFile('agaricus.txt.test', $silent, \$dtest);
113             AI::XGBoost::CAPI::RAW::XGDMatrixCreateFromFile('agaricus.txt.train', $silent, \$dtrain);
114            
115             my ($rows, $cols) = (0, 0);
116             AI::XGBoost::CAPI::RAW::XGDMatrixNumRow($dtrain, \$rows);
117             AI::XGBoost::CAPI::RAW::XGDMatrixNumCol($dtrain, \$cols);
118             say "Dimensions: $rows, $cols";
119            
120             my $booster = 0;
121            
122             AI::XGBoost::CAPI::RAW::XGBoosterCreate( [$dtrain] , 1, \$booster);
123            
124             for my $iter (0 .. 10) {
125             AI::XGBoost::CAPI::RAW::XGBoosterUpdateOneIter($booster, $iter, $dtrain);
126             }
127            
128             my $out_len = 0;
129             my $out_result = 0;
130            
131             AI::XGBoost::CAPI::RAW::XGBoosterPredict($booster, $dtest, 0, 0, \$out_len, \$out_result);
132             my $ffi = FFI::Platypus->new();
133             my $predictions = $ffi->cast(opaque => "float[$out_len]", $out_result);
134            
135             #say join "\n", @$predictions;
136            
137             AI::XGBoost::CAPI::RAW::XGBoosterFree($booster);
138             AI::XGBoost::CAPI::RAW::XGDMatrixFree($dtrain);
139             AI::XGBoost::CAPI::RAW::XGDMatrixFree($dtest);
140              
141             =head1 DESCRIPTION
142              
143             Wrapper for the C API.
144              
145             The doc for the methods is extracted from doxygen comments: https://github.com/dmlc/xgboost/blob/master/include/xgboost/c_api.h
146              
147             =head1 FUNCTIONS
148              
149             =head2 XGBGetLastError
150              
151             Get string message of the last error
152              
153             All functions in this file will return 0 when success
154             and -1 when an error occurred,
155             XGBGetLastError can be called to retrieve the error
156              
157             This function is thread safe and can be called by different thread
158              
159             Returns string error information
160              
161             =head2 XGDMatrixCreateFromFile
162              
163             Load a data matrix
164              
165             Parameters:
166              
167             =over 4
168              
169             =item filename
170              
171             the name of the file
172              
173             =item silent
174              
175             whether print messages during loading
176              
177             =item out
178              
179             a loaded data matrix
180              
181             =back
182              
183             =head2 XGDMatrixCreateFromCSREx
184              
185             Create a matrix content from CSR fromat
186              
187             Parameters:
188              
189             =over 4
190              
191             =item indptr
192              
193             pointer to row headers
194              
195             =item indices
196              
197             findex
198              
199             =item data
200              
201             fvalue
202              
203             =item nindptr
204              
205             number of rows in the matrix + 1
206              
207             =item nelem
208              
209             number of nonzero elements in the matrix
210              
211             =item num_col
212              
213             number of columns; when it's set to 0, then guess from data
214              
215             =item out
216              
217             created dmatrix
218              
219             =back
220              
221             =head2 XGDMatrixCreateFromCSCEx
222              
223             Create a matrix content from CSC format
224              
225             Parameters:
226              
227             =over 4
228              
229             =item col_ptr
230              
231             pointer to col headers
232              
233             =item indices
234              
235             findex
236              
237             =item data
238              
239             fvalue
240              
241             =item nindptr
242              
243             number of rows in the matrix + 1
244              
245             =item nelem
246              
247             number of nonzero elements in the matrix
248              
249             =item num_row
250              
251             number of rows; when it's set to 0, then guess from data
252              
253             =back
254              
255             =head2 XGDMatrixCreateFromMat
256              
257             Create matrix content from dense matrix
258              
259             Parameters:
260              
261             =over 4
262              
263             =item data
264              
265             pointer to the data space
266              
267             =item nrow
268              
269             number of rows
270              
271             =item ncol
272              
273             number columns
274              
275             =item missing
276              
277             which value to represent missing value
278              
279             =item out
280              
281             created dmatrix
282              
283             =back
284              
285             =head2 XGDMatrixCreateFromMat_omp
286              
287             Create matrix content from dense matrix
288              
289             Parameters:
290              
291             =over 4
292              
293             =item data
294              
295             pointer to the data space
296              
297             =item nrow
298              
299             number of rows
300              
301             =item ncol
302              
303             number columns
304              
305             =item missing
306              
307             which value to represent missing value
308              
309             =item out
310              
311             created dmatrix
312              
313             =item nthread
314              
315             number of threads (up to maximum cores available, if <=0 use all cores)
316              
317             =back
318              
319             =head2 XGDMatrixSliceDMatrix
320              
321             Create a new dmatrix from sliced content of existing matrix
322              
323             Parameters:
324              
325             =over 4
326              
327             =item handle
328              
329             instance of data matrix to be sliced
330              
331             =item idxset
332              
333             index set
334              
335             =item len
336              
337             length of index set
338              
339             =item out
340              
341             a sliced new matrix
342              
343             =back
344              
345             =head2 XGDMatrixNumRow
346              
347             Get number of rows.
348              
349             Parameters:
350              
351             =over 4
352              
353             =item handle
354              
355             the handle to the DMatrix
356              
357             =item out
358              
359             The address to hold number of rows.
360              
361             =back
362              
363             =head2 XGDMatrixNumCol
364              
365             Get number of cols.
366              
367             Parameters:
368              
369             =over 4
370              
371             =item handle
372              
373             the handle to the DMatrix
374              
375             =item out
376              
377             The address to hold number of cols.
378              
379             =back
380              
381             =head2 XGDMatrixSaveBinary
382              
383             load a data matrix into binary file
384              
385             Parameters:
386              
387             =over 4
388              
389             =item handle
390              
391             a instance of data matrix
392              
393             =item fname
394              
395             file name
396              
397             =item silent
398              
399             print statistics when saving
400              
401             =back
402              
403             =head2 XGDMatrixSetFloatInfo
404              
405             Set float vector to a content in info
406              
407             Parameters:
408              
409             =over 4
410              
411             =item handle
412              
413             a instance of data matrix
414              
415             =item field
416              
417             field name, can be label, weight
418              
419             =item array
420              
421             pointer to float vector
422              
423             =item len
424              
425             length of array
426              
427             =back
428              
429             =head2 XGDMatrixSetUIntInfo
430              
431             Set uint32 vector to a content in info
432              
433             Parameters:
434              
435             =over 4
436              
437             =item handle
438              
439             a instance of data matrix
440              
441             =item field
442              
443             field name, can be label, weight
444              
445             =item array
446              
447             pointer to unsigned int vector
448              
449             =item len
450              
451             length of array
452              
453             =back
454              
455             =head2 XGDMatrixSetGroup
456              
457             Set label of the training matrix
458              
459             Parameters:
460              
461             =over 4
462              
463             =item handle
464              
465             a instance of data matrix
466              
467             =item group
468              
469             pointer to group size
470              
471             =item len
472              
473             length of the array
474              
475             =back
476              
477             =head2 XGDMatrixGetFloatInfo
478              
479             Get float info vector from matrix
480              
481             Parameters:
482              
483             =over 4
484              
485             =item handle
486              
487             a instance of data matrix
488              
489             =item field
490              
491             field name
492              
493             =item out_len
494              
495             used to set result length
496              
497             =item out_dptr
498              
499             pointer to the result
500              
501             =back
502              
503             =head2 XGDMatrixGetUIntInfo
504              
505             Get uint32 info vector from matrix
506              
507             Parameters:
508              
509             =over 4
510              
511             =item handle
512              
513             a instance of data matrix
514              
515             =item field
516              
517             field name
518              
519             =item out_len
520              
521             The length of the field
522              
523             =item out_dptr
524              
525             pointer to the result
526              
527             =back
528              
529             =head2 XGDMatrixFree
530              
531             Free space in data matrix
532              
533             =head2 XGBoosterCreate
534              
535             Create xgboost learner
536              
537             Parameters:
538              
539             =over 4
540              
541             =item dmats
542              
543             matrices that are set to be cached
544              
545             =item len
546              
547             length of dmats
548              
549             =item out
550              
551             handle to the result booster
552              
553             =back
554              
555             =head2 XGBoosterFree
556              
557             Free obj in handle
558              
559             Parameters:
560              
561             =over 4
562              
563             =item handle
564              
565             handle to be freed
566              
567             =back
568              
569             =head2 XGBoosterSetParam
570              
571             Update the model in one round using dtrain
572              
573             Parameters:
574              
575             =over 4
576              
577             =item handle
578              
579             handle
580              
581             =item name
582              
583             parameter name
584              
585             =item value
586              
587             value of parameter
588              
589             =back
590              
591             =head2 XGBoosterBoostOneIter
592              
593             Update the modelo, by directly specify grandient and second order gradient,
594             this can be used to replace UpdateOneIter, to support customized loss function
595              
596             Parameters:
597              
598             =over 4
599              
600             =item handle
601              
602             handle
603              
604             =item dtrain
605              
606             training data
607              
608             =item grad
609              
610             gradient statistics
611              
612             =item hess
613              
614             second order gradinet statistics
615              
616             =item len
617              
618             length of grad/hess array
619              
620             =back
621              
622             =head2 XGBoosterUpdateOneIter
623              
624             Update the model in one round using dtrain
625              
626             Parameters:
627              
628             =over 4
629              
630             =item handle
631              
632             handle
633              
634             =item iter
635              
636             current iteration rounds
637              
638             =item dtrain
639              
640             training data
641              
642             =back
643              
644             =head2 XGBoosterEvalOneIter
645              
646             =head2 XGBoosterPredict
647              
648             Make prediction based on dmat
649              
650             Parameters:
651              
652             =over 4
653              
654             =item handle
655              
656             handle
657              
658             =item dmat
659              
660             data matrix
661              
662             =item option_mask
663              
664             bit-mask of options taken in prediction, possible values
665              
666             =over 4
667              
668             =item
669              
670             0: normal prediction
671              
672             =item
673              
674             1: output margin instead of transformed value
675              
676             =item
677              
678             2: output leaf index of trees instead of leaf value, note leaf index is unique per tree
679              
680             =item
681              
682             4: output feature contributions to individual predictions
683              
684             =back
685              
686             =item ntree_limit
687              
688             limit number of trees used for prediction, this is only valid for boosted trees
689             when the parameter is set to 0, we will use all the trees
690              
691             =item out_len
692              
693             used to store length of returning result
694              
695             =item out_result
696              
697             used to set a pointer to array
698              
699             =back
700              
701             =head2 XGBoosterLoadModel
702              
703             Load model form existing file
704              
705             Parameters:
706              
707             =over 4
708              
709             =item handle
710              
711             handle
712              
713             =item fname
714              
715             file name
716              
717             =back
718              
719             =head2 XGBoosterSaveModel
720              
721             Save model into existing file
722              
723             Parameters:
724              
725             =over 4
726              
727             =item handle
728              
729             handle
730              
731             =item fname
732              
733             file name
734              
735             =back
736              
737             =head2 XGBoosterLoadModelFromBuffer
738              
739             =head2 XGBoosterGetModelRaw
740              
741             =head2 XGBoosterDumpModel
742              
743             =head2 XGBoosterDumpModelEx
744              
745             =head2 XGBoosterDumpModelWithFeatures
746              
747             =head2 XGBoosterDumpModelExWithFeatures
748              
749             =head2 XGBoosterSetAttr
750              
751             =head2 XGBoosterGetAttr
752              
753             =head2 XGBoosterGetAttrNames
754              
755             =head2 XGBoosterLoadRabitCheckpoint
756              
757             =head2 XGBoosterSaveRabitCheckpoint
758              
759             =head1 AUTHOR
760              
761             Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com>
762              
763             =head1 COPYRIGHT AND LICENSE
764              
765             Copyright (c) 2017 by Pablo Rodríguez González.
766              
767             =cut