ML/Preprocess.pm

filter: Statementstmt 100% 79 / 79 Branchbran 57.1% 16 / 28 Conditioncond 52.9% 9 / 17 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 5 totaltotal 78.2% 119 / 152 CC 23 SCAR 18.0
CC
23
Cov
84%
train_test_split
11.7
one_hot_encode
5.4
standard_scale
5.3
CRAP 6.0
↑ Summary Demos/ML_Preprocess.pm »
1 package PDL::ML::Preprocess;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use PDL::Core ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use PDL::Lite ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use PDL::Ufunc ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '0.012';
13 2 our @EXPORT_OK = qw(
14 train_test_split
15 standard_scale
16 minmax_scale
17 l2_normalize
18 one_hot_encode
19 );
20  
21 3 ▶sub train_test_split {
Subroutine train_test_split: called CC=11 SCAR=24.6
Pod undocumented
22 3 my ($X, $y, %opts) = @_;
23 3 ▶ my $test_size = $opts{test_size} // 0.2;
Condition100%2/2
$opts{"test_size"} // "0.2"
l!l
21
MC/DC100%1/1
$opts{"test_size"} // "0.2"
$opts{"test_size"}
Truth table100%2/2
A
A= $opts{"test_size"}
Aresult
11
00
24 3 ▶ my $shuffle = $opts{shuffle} // 1;
Condition50%1/2
$opts{"shuffle"} // 1
l!l
03
MC/DC0%0/1
$opts{"shuffle"} // 1
$opts{"shuffle"}
Truth table50%1/2
A
A= $opts{"shuffle"}
Aresult
11
00
25 3 my $seed = $opts{seed};
26  
27 3 my $n = $X->dim(-1);
28 3 ▶ croak 'X and y must have matching sample counts'
Branch: 1 branch
BranchTrueFalseTotal
if defined $y and $y->dim(-1) != $n 1 2 3
Condition66%2/3
defined $y and $y->dim(-1) != $n
!ll&&!rl&&r
021
MC/DC50%1/2
defined $y and $y->dim(-1) != $n
defined $y $y->dim(-1) != $n
Truth table66%2/3
A and B
A= defined $yB= $y->dim(-1) != $n
ABresult
0X0
100
111
29 if defined $y && $y->dim(-1) != $n;
30  
31 2 my @idx = (0 .. $n - 1);
32 2 ▶ if ($shuffle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shuffle) 2 0 2
33 2 ▶ srand($seed) if defined $seed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $seed 2 0 2
34 2 ▶ for (my $i = $#idx; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 14 2 16
35 14 my $j = int(rand($i + 1));
36 14 @idx[$i, $j] = @idx[$j, $i];
37 }
38 }
39  
40 2 my $n_test = int($n * $test_size);
41 2 ▶ $n_test = 1 if $n_test < 1 && $n > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n_test < 1 and $n > 1 0 2 2
Condition33%1/3
$n_test < 1 and $n > 1
!ll&&!rl&&r
200
MC/DC0%0/2
$n_test < 1 and $n > 1
$n_test < 1 $n > 1
Truth table33%1/3
A and B
A= $n_test < 1B= $n > 1
ABresult
0X0
100
111
42 2 my $n_train = $n - $n_test;
43  
44 2 my $tr = PDL::Core::pdl(PDL::long(), [ @idx[ 0 .. $n_train - 1 ] ]);
45 2 my $te = PDL::Core::pdl(PDL::long(), [ @idx[ $n_train .. $n - 1 ] ]);
46  
47 2 my $X_tr = $X->dice_axis(-1, $tr);
48 2 my $X_te = $X->dice_axis(-1, $te);
49 2 ▶ return ($X_tr, $X_te) unless defined $y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y 0 2 2
50  
51 2 my $y_tr = $y->dice_axis(-1, $tr);
52 2 my $y_te = $y->dice_axis(-1, $te);
53 2 return ($X_tr, $X_te, $y_tr, $y_te);
54 }
55  
56 2 ▶sub standard_scale {
Subroutine standard_scale: called CC=5 SCAR=16.7
Pod undocumented
57 2 my ($X, %opts) = @_;
58 2 ▶ my $eps = defined($opts{eps}) ? $opts{eps} : 1e-8;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'eps'} ? : 0 2 2
59 2 ▶ my $mean = defined($opts{mean}) ? $opts{mean} : $X->transpose->average;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'mean'} ? : 0 2 2
60 2 my $diff = $X - $mean;
61 2 ▶ my $std = defined($opts{std}) ? $opts{std} : sqrt(($diff * $diff)->transpose->average);
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'std'} ? : 0 2 2
62 2 my $denom = $std->copy;
63 2 $denom->where($denom <= $eps) .= 1.0;
64 2 my $scaled = $diff / $denom;
65 2 ▶ return wantarray ? ($scaled, $mean, $denom) : $scaled;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
66 }
67  
68 2 ▶sub minmax_scale {
Subroutine minmax_scale: called CC=4 SCAR=14.3
Pod undocumented
69 2 my ($X, %opts) = @_;
70 2 ▶ my $min = defined($opts{min}) ? $opts{min} : $X->transpose->minimum;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'min'} ? : 0 2 2
71 2 ▶ my $max = defined($opts{max}) ? $opts{max} : $X->transpose->maximum;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'max'} ? : 0 2 2
72 2 my $range = $max - $min;
73 2 $range->where($range == 0) .= 1.0;
74 2 my $scaled = ($X - $min) / $range;
75 2 ▶ return wantarray ? ($scaled, $min, $max) : $scaled;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
76 }
77  
78 1 ▶sub l2_normalize {
Subroutine l2_normalize: called CC=2 SCAR=7.0
Pod undocumented
79 1 my ($X, %opts) = @_;
80 1 ▶ my $eps = $opts{eps} // 1e-12;
Condition50%1/2
$opts{"eps"} // "1e-12"
l!l
01
MC/DC0%0/1
$opts{"eps"} // "1e-12"
$opts{"eps"}
Truth table50%1/2
A
A= $opts{"eps"}
Aresult
11
00
81 1 my $norm = sqrt(($X * $X)->sumover);
82 1 $norm->where($norm < $eps) .= 1.0;
83 1 return $X / $norm->dummy(0);
84 }
85  
86 2 ▶sub one_hot_encode {
Subroutine one_hot_encode: called CC=5 SCAR=16.8
Pod undocumented
87 2 my ($labels, $num_classes) = @_;
88 2 ▶ $num_classes //= int($labels->max) + 1;
Condition50%1/2
$num_classes //= int($labels->max) + 1
l!l
20
MC/DC0%0/1
$num_classes //= int($labels->max) + 1
$num_classes
Truth table50%1/2
A
A= $num_classes
Aresult
11
00
89 2 my $n = $labels->nelem;
90 2 my $out = PDL->zeroes(PDL::float(), $num_classes, $n);
91 2 for my $i (0 .. $n - 1) {
92 9 my $c = int($labels->at($i));
93 9 ▶ croak "Class index $c out of range [0, $num_classes)" if $c < 0 || $c >= $num_classes;
Branch: 1 branch
BranchTrueFalseTotal
if $c < 0 or $c >= $num_classes 0 9 9
Condition33%1/3
$c < 0 or $c >= $num_classes
l!l&&r!l&&!r
009
MC/DC0%0/2
$c < 0 or $c >= $num_classes
$c < 0 $c >= $num_classes
Truth table33%1/3
A or B
A= $c < 0B= $c >= $num_classes
ABresult
1X1
011
000
94 9 $out->set($c, $i, 1.0);
95 }
96 2 return $out;
97 }
98  
99 2 1;
100  
101 __END__
↑ Summary Demos/ML_Preprocess.pm »