| 1 | package Algorithm::KernelKMeans::Util; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.010; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 4 | ▶ | use Exporter::Lite; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 4 | ▶ | use List::Util qw/sum/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 4 | ▶ | use POSIX qw/tanh/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 4 | our @EXPORT_OK = qw/centroid | |||||||||||||||||||||
| 12 | inner_product | ||||||||||||||||||||||
| 13 | generate_polynominal_kernel | ||||||||||||||||||||||
| 14 | generate_gaussian_kernel | ||||||||||||||||||||||
| 15 | generate_sigmoid_kernel/; | ||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 128 | ▶ | sub centroid { | ||||||||||||||||||||
|
Subroutine
centroid: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||
| 18 | 128 | my $cluster = shift; | |||||||||||||||||||||
| 19 | 128 | my %centroid; | |||||||||||||||||||||
| 20 | 128 | for my $vertex (@$cluster) { | |||||||||||||||||||||
| 21 | 5382 | ▶ | while (my ($key, $val) = each %$vertex) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 22 | 16146 | ▶ | $centroid{$key} //= 0; | ||||||||||||||||||||
|
Condition100%2/2
$centroid{$key} //= 0
MC/DC100%1/1
$centroid{$key} //= 0
$centroid{$key}
Truth table100%2/2
A
A= $centroid{$key}
| |||||||||||||||||||||||
| 23 | 16146 | $centroid{$key} += $val; | |||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||
| 26 | 128 | for my $key (keys %centroid) { $centroid{$key} /= @$cluster } | |||||||||||||||||||||
| 27 | 128 | return \%centroid; | |||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 263168 | ▶ | sub inner_product { | ||||||||||||||||||||
|
Subroutine
inner_product: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||
| 31 | 263168 | my ($x1, $x2) = @_; | |||||||||||||||||||||
| 32 | 263168 | my @common_keys = grep { exists $x2->{$_} } keys %$x1; | |||||||||||||||||||||
| 33 | 263168 | ▶ | return 0 if @common_keys == 0; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 34 | 263168 | sum map { $x1->{$_} * $x2->{$_} } @common_keys; | |||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 6 | ▶ | sub generate_polynominal_kernel { | ||||||||||||||||||||
|
Subroutine
generate_polynominal_kernel: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 38 | 6 | my ($l, $p) = @_; | |||||||||||||||||||||
| 39 | 197376 | ▶ | sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 40 | 197376 | my ($x1, $x2) = @_; | |||||||||||||||||||||
| 41 | 197376 | ($l + inner_product($x1, $x2)) ** $p | |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||
| 43 | 6 | } | |||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | 1 | ▶ | sub generate_gaussian_kernel { | ||||||||||||||||||||
|
Subroutine
generate_gaussian_kernel: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 46 | 1 | my $sigma = shift; | |||||||||||||||||||||
| 47 | 1 | my $numer = 2 * ($sigma ** 2); | |||||||||||||||||||||
| 48 | 32896 | ▶ | sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||
| 49 | 32896 | my ($x1, $x2) = @_; | |||||||||||||||||||||
| 50 | 32896 | my %tmp; @tmp{keys %$x1, keys %$x2} = (); | |||||||||||||||||||||
| 51 | my $norm = sqrt sum map { | ||||||||||||||||||||||
| 52 | 32896 | ▶ | my ($e1, $e2) = (($x1->{$_} // 0), ($x2->{$_} // 0)); | ||||||||||||||||||||
|
Condition50%2/4
$x1->{$_} // 0
$x2->{$_} // 0
MC/DC0%0/1
$x1->{$_} // 0
$x1->{$_}
MC/DC0%0/1
$x2->{$_} // 0
$x2->{$_}
Truth table50%1/2
A
A= $x1->{$_}
Truth table50%1/2
A
A= $x2->{$_}
| |||||||||||||||||||||||
| 53 | 98688 | ($e1 - $e2) ** 2; | |||||||||||||||||||||
| 54 | } keys %tmp; | ||||||||||||||||||||||
| 55 | 32896 | exp(-$norm / $numer); | |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||
| 57 | 1 | } | |||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | 1 | ▶ | sub generate_sigmoid_kernel { | ||||||||||||||||||||
|
Subroutine
generate_sigmoid_kernel: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 60 | 1 | my ($s, $theta) = @_; | |||||||||||||||||||||
| 61 | 32896 | ▶ | sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 62 | 32896 | my ($x1, $x2) = @_; | |||||||||||||||||||||
| 63 | 32896 | tanh($s * inner_product($x1, $x2) + $theta); | |||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||
| 65 | 1 | } | |||||||||||||||||||||
| 66 | |||||||||||||||||||||||
| 67 | 4 | 1; | |||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | __END__ | ||||||||||||||||||||||