| 1 | package Algorithm::LibLinear::FeatureScaling; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Algorithm::LibLinear::Types qw/Feature FeatureWithLabel/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp qw//; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use List::MoreUtils qw/minmax none/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use List::Util qw/max/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Smart::Args::TypeTiny; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Types::Standard qw/ArrayRef ClassName FileHandle InstanceOf Num Str/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 1 | my $InstanceOfPackage = InstanceOf[__PACKAGE__]; | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 14 | 2 | args | |||||||||||||||||||||||||||||||
| 15 | my $class => ClassName, | ||||||||||||||||||||||||||||||||
| 16 | my $data_set => +{ | ||||||||||||||||||||||||||||||||
| 17 | isa => InstanceOf['Algorithm::LibLinear::DataSet'], | ||||||||||||||||||||||||||||||||
| 18 | optional => 1, | ||||||||||||||||||||||||||||||||
| 19 | }, | ||||||||||||||||||||||||||||||||
| 20 | my $lower_bound => +{ isa => Num, default => 0, }, | ||||||||||||||||||||||||||||||||
| 21 | my $min_max_values => +{ | ||||||||||||||||||||||||||||||||
| 22 | isa => ArrayRef[ArrayRef[Num]], | ||||||||||||||||||||||||||||||||
| 23 | optional => 1, | ||||||||||||||||||||||||||||||||
| 24 | }, | ||||||||||||||||||||||||||||||||
| 25 | my $upper_bound => +{ isa => Num, default => 1.0, }; | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 2 | ▶ | unless ($data_set or $min_max_values) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$data_set or $min_max_values
MC/DC0%0/2
$data_set or $min_max_values
$data_set
$min_max_values
Truth table33%1/3
A or B
A= $data_setB= $min_max_values
| |||||||||||||||||||||||||||||||||
| 28 | 0 | Carp::croak('Neither "data_set" nor "min_max_values" is specified.'); | |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 2 | my $self = bless +{ | |||||||||||||||||||||||||||||||
| 32 | lower_bound => $lower_bound, | ||||||||||||||||||||||||||||||||
| 33 | upper_bound => $upper_bound, | ||||||||||||||||||||||||||||||||
| 34 | } => $class; | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | $self->{min_max_values} = $min_max_values | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$min_max_values // $self->compute_min_max_values("data_set", $data_set)
MC/DC0%0/2
$min_max_values // $self->compute_min_max_values("data_set", $data_set)
$min_max_values
$self->compute_min_max_values("data_set", $data_set)
Truth table33%1/3
A // B
A= $min_max_valuesB= $self->compute_min_max_values("data_set", $data_set)
| |||||||||||||||||||||||||||||||||
| 37 | // $self->compute_min_max_values(data_set => $data_set); | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub load { | ||||||||||||||||||||||||||||||
|
Subroutine
load: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 43 | 0 | args | |||||||||||||||||||||||||||||||
| 44 | my $class => ClassName, | ||||||||||||||||||||||||||||||||
| 45 | my $fh => +{ isa => FileHandle, optional => 1, }, | ||||||||||||||||||||||||||||||||
| 46 | my $filename => +{ isa => Str, optional => 1, }, | ||||||||||||||||||||||||||||||||
| 47 | my $string => +{ isa => Str, optional => 1, }; | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if (none { defined } ($filename, $fh, $string)) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | 0 | Carp::croak('No source specified.'); | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | 0 | my $source = $fh; | |||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $source //= do { | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$source //= do {
open $fh, "<", $filename // \$string or Carp::croak($!);
$fh
}
MC/DC0%0/2
$source //= do {
open $fh, "<", $filename // \$string or Carp::croak($!);
$fh
}
$source
do {
open $fh, "<", $filename // \$string or Carp::croak($!);
$fh
}
Truth table0%0/3
A //= B
A= $sourceB= do {
open $fh, "<", $filename // \$string or Carp::croak($!);
$fh
}
| |||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | open $fh, '<', +($filename // \$string) or Carp::croak($!); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$filename // \$string
MC/DC0%0/1
$filename // \$string
$filename
Truth table0%0/2
A
A= $filename
| |||||||||||||||||||||||||||||||||
| 55 | 0 | $fh; | |||||||||||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | chomp(my $header = <$source>); | |||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | Carp::croak('At present, y-scaling is not supported.') if $header eq 'y'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | Carp::croak('Invalid format.') if $header ne 'x'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 0 | chomp(my $bounds = <$source>); | |||||||||||||||||||||||||||||||
| 63 | 0 | my ($lower_bound, $upper_bound) = split /\s+/, $bounds; | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 0 | my @min_max_values; | |||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | while (defined(my $min_max_values = <$source>)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | 0 | chomp $min_max_values; | |||||||||||||||||||||||||||||||
| 68 | 0 | my (undef, $min, $max) = split /\s+/, $min_max_values; | |||||||||||||||||||||||||||||||
| 69 | 0 | push @min_max_values, [ $min, $max ]; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | $class->new( | ||||||||||||||||||||||||||||||||
| 73 | 0 | lower_bound => $lower_bound, | |||||||||||||||||||||||||||||||
| 74 | min_max_values => \@min_max_values, | ||||||||||||||||||||||||||||||||
| 75 | upper_bound => $upper_bound, | ||||||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 80 | 0 | args | |||||||||||||||||||||||||||||||
| 81 | my $self => $InstanceOfPackage; | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 0 | my $acc = | |||||||||||||||||||||||||||||||
| 84 | sprintf "x\n%.17g %.17g\n", $self->lower_bound, $self->upper_bound; | ||||||||||||||||||||||||||||||||
| 85 | 0 | my $index = 0; | |||||||||||||||||||||||||||||||
| 86 | 0 | for my $min_max_value (@{ $self->min_max_values }) { | |||||||||||||||||||||||||||||||
| 87 | 0 | $acc .= sprintf "\%d %.17g %.17g\n", ++$index, @$min_max_value; | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | 0 | return $acc; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | sub compute_min_max_values { | ||||||||||||||||||||||||||||||
|
Subroutine
compute_min_max_values: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 93 | 2 | args | |||||||||||||||||||||||||||||||
| 94 | my $self => $InstanceOfPackage, | ||||||||||||||||||||||||||||||||
| 95 | my $data_set => InstanceOf['Algorithm::LibLinear::DataSet']; | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 2 | my @feature_vectors = map { $_->{feature} } @{ $data_set->as_arrayref }; | |||||||||||||||||||||||||||||||
| 98 | 2 | my $last_index = max map { keys %$_ } @feature_vectors; | |||||||||||||||||||||||||||||||
| 99 | 2 | my @min_max_values; | |||||||||||||||||||||||||||||||
| 100 | 2 | for my $i (1 .. $last_index) { | |||||||||||||||||||||||||||||||
| 101 | 8 | ▶ | my ($min, $max) = minmax map { $_->{$i} // 0 } @feature_vectors; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$_->{$i} // 0
MC/DC100%1/1
$_->{$i} // 0
$_->{$i}
Truth table100%2/2
A
A= $_->{$i}
| |||||||||||||||||||||||||||||||||
| 102 | 8 | push @min_max_values, [ $min, $max ]; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | 2 | return \@min_max_values; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 19 | ▶ | sub lower_bound { $_[0]->{lower_bound} } | ||||||||||||||||||||||||||||||
|
Subroutine
lower_bound: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 8 | ▶ | sub min_max_values { $_[0]->{min_max_values} } | ||||||||||||||||||||||||||||||
|
Subroutine
min_max_values: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub save { | ||||||||||||||||||||||||||||||
|
Subroutine
save: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 112 | 0 | args | |||||||||||||||||||||||||||||||
| 113 | my $self => $InstanceOfPackage, | ||||||||||||||||||||||||||||||||
| 114 | my $fh => +{ isa => FileHandle, optional => 1, }, | ||||||||||||||||||||||||||||||||
| 115 | my $filename => +{ isa => Str, optional => 1, }; | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | unless ($filename or $fh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$filename or $fh
MC/DC0%0/2
$filename or $fh
$filename
$fh
Truth table0%0/3
A or B
A= $filenameB= $fh
| |||||||||||||||||||||||||||||||||
| 118 | 0 | Carp::croak('Neither "filename" nor "fh" is given.'); | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | open $fh, '>', $filename or Carp::croak($!) unless $fh; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$fh || (open($fh, '>', $filename) || Carp::croak($!))
open $fh, ">", $filename or Carp::croak($!)
MC/DC0%0/2
$fh || (open($fh, '>', $filename) || Carp::croak($!))
$fh
open $fh, ">", $filename
Truth table0%0/3
A || B
A= $fhB= open $fh, ">", $filename
| |||||||||||||||||||||||||||||||||
| 121 | 0 | print $fh $self->as_string; | |||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | sub scale { | ||||||||||||||||||||||||||||||
|
Subroutine
scale: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 125 | 3 | args_pos | |||||||||||||||||||||||||||||||
| 126 | my $self => $InstanceOfPackage, | ||||||||||||||||||||||||||||||||
| 127 | my $target_type => Str, | ||||||||||||||||||||||||||||||||
| 128 | my $target; | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 3 | my $method = $self->can("scale_$target_type"); | |||||||||||||||||||||||||||||||
| 131 | 3 | ▶ | unless ($method) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | 0 | Carp::croak("Cannot scale such type of target: $target_type."); | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | 3 | $self->$method($target); | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 3 | ▶ | sub scale_data_set { | ||||||||||||||||||||||||||||||
|
Subroutine
scale_data_set: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 138 | 3 | args_pos | |||||||||||||||||||||||||||||||
| 139 | my $self => $InstanceOfPackage, | ||||||||||||||||||||||||||||||||
| 140 | my $data_set => InstanceOf['Algorithm::LibLinear::DataSet']; | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | my @scaled_data_set = | ||||||||||||||||||||||||||||||||
| 143 | 3 | map { $self->scale_labeled_data($_) } @{ $data_set->as_arrayref }; | |||||||||||||||||||||||||||||||
| 144 | 3 | Algorithm::LibLinear::DataSet->new(data_set => \@scaled_data_set); | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 8 | ▶ | sub scale_feature { | ||||||||||||||||||||||||||||||
|
Subroutine
scale_feature: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 148 | 8 | args_pos | |||||||||||||||||||||||||||||||
| 149 | my $self => $InstanceOfPackage, | ||||||||||||||||||||||||||||||||
| 150 | my $feature => Feature; | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 8 | my ($lower_bound, $upper_bound) = ($self->lower_bound, $self->upper_bound); | |||||||||||||||||||||||||||||||
| 153 | 8 | my $min_max_values = $self->min_max_values; | |||||||||||||||||||||||||||||||
| 154 | 8 | my %scaled_feature; | |||||||||||||||||||||||||||||||
| 155 | 8 | for my $index (1 .. @$min_max_values) { | |||||||||||||||||||||||||||||||
| 156 | 34 | ▶ | my $unscaled = $feature->{$index} // 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$feature->{$index} // 0
MC/DC100%1/1
$feature->{$index} // 0
$feature->{$index}
Truth table100%2/2
A
A= $feature->{$index}
| |||||||||||||||||||||||||||||||||
| 157 | 34 | ▶ | my ($min, $max) = @{ $min_max_values->[$index - 1] // [0, 0] }; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$$min_max_values[$index - 1] // [0, 0]
MC/DC0%0/1
$$min_max_values[$index - 1] // [0, 0]
$$min_max_values[$index - 1]
Truth table50%1/2
A
A= $$min_max_values[$index - 1]
| |||||||||||||||||||||||||||||||||
| 158 | 34 | ▶ | next if $min == $max; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | 29 | my $scaled; | |||||||||||||||||||||||||||||||
| 160 | 29 | ▶ | if ($unscaled == $min) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 161 | 11 | $scaled = $lower_bound; | |||||||||||||||||||||||||||||||
| 162 | } elsif ($unscaled == $max) { | ||||||||||||||||||||||||||||||||
| 163 | 9 | $scaled = $upper_bound; | |||||||||||||||||||||||||||||||
| 164 | } else { | ||||||||||||||||||||||||||||||||
| 165 | 9 | my $ratio = ($unscaled - $min) / ($max - $min); | |||||||||||||||||||||||||||||||
| 166 | 9 | $scaled = $lower_bound + ($upper_bound - $lower_bound) * $ratio; | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | 29 | ▶ | $scaled_feature{$index} = $scaled if $scaled != 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | 8 | return \%scaled_feature; | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 8 | ▶ | sub scale_labeled_data { | ||||||||||||||||||||||||||||||
|
Subroutine
scale_labeled_data: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 174 | 8 | args_pos | |||||||||||||||||||||||||||||||
| 175 | my $self => $InstanceOfPackage, | ||||||||||||||||||||||||||||||||
| 176 | my $labeled_data => FeatureWithLabel; | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | +{ | ||||||||||||||||||||||||||||||||
| 179 | feature => $self->scale_feature($labeled_data->{feature}), | ||||||||||||||||||||||||||||||||
| 180 | label => $labeled_data->{label}, | ||||||||||||||||||||||||||||||||
| 181 | 8 | }; | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 19 | ▶ | sub upper_bound { $_[0]->{upper_bound} } | ||||||||||||||||||||||||||||||
|
Subroutine
upper_bound: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | 1 | 1; | |||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | __DATA__ | ||||||||||||||||||||||||||||||||