| 1 | package PDL::ML::Metrics; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use PDL::Core (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use PDL::Lite (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use PDL::Ufunc (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 13 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 14 | accuracy | ||||||||||||||||||||||||||||
| 15 | precision_recall_f1 | ||||||||||||||||||||||||||||
| 16 | confusion_matrix | ||||||||||||||||||||||||||||
| 17 | mse | ||||||||||||||||||||||||||||
| 18 | rmse | ||||||||||||||||||||||||||||
| 19 | mae | ||||||||||||||||||||||||||||
| 20 | r2_score | ||||||||||||||||||||||||||||
| 21 | cosine_similarity | ||||||||||||||||||||||||||||
| 22 | roc_auc | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | sub accuracy { | ||||||||||||||||||||||||||
|
Subroutine
accuracy: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 1 | my ($y_true, $y_pred) = @_; | |||||||||||||||||||||||||||
| 27 | 1 | return ($y_true == $y_pred)->double->avg; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub confusion_matrix { | ||||||||||||||||||||||||||
|
Subroutine
confusion_matrix: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 1 | my ($y_true, $y_pred, $num_classes) = @_; | |||||||||||||||||||||||||||
| 32 | 1 | my $n = $y_true->nelem; | |||||||||||||||||||||||||||
| 33 | 1 | ▶ | $num_classes //= int(($y_true->max > $y_pred->max ? $y_true->max : $y_pred->max)) + 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$num_classes //= int($y_true->max > $y_pred->max ? $y_true->max : $y_pred->max) + 1
MC/DC0%0/1
$num_classes //= int($y_true->max > $y_pred->max ? $y_true->max : $y_pred->max) + 1
$num_classes
Truth table50%1/2
A
A= $num_classes
| |||||||||||||||||||||||||||||
| 34 | 1 | my $cm = PDL->zeroes(PDL::long(), $num_classes, $num_classes); | |||||||||||||||||||||||||||
| 35 | 1 | for my $i (0 .. $n - 1) { | |||||||||||||||||||||||||||
| 36 | 5 | my $t = int($y_true->at($i)); | |||||||||||||||||||||||||||
| 37 | 5 | my $p = int($y_pred->at($i)); | |||||||||||||||||||||||||||
| 38 | 5 | $cm->set($p, $t, $cm->at($p, $t) + 1); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 1 | return $cm; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | sub precision_recall_f1 { | ||||||||||||||||||||||||||
|
Subroutine
precision_recall_f1: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 1 | my ($y_true, $y_pred, $pos_label) = @_; | |||||||||||||||||||||||||||
| 45 | 1 | ▶ | $pos_label //= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$pos_label //= 1
MC/DC0%0/1
$pos_label //= 1
$pos_label
Truth table50%1/2
A
A= $pos_label
| |||||||||||||||||||||||||||||
| 46 | 1 | my $tp = (($y_true == $pos_label) & ($y_pred == $pos_label))->double->sum; | |||||||||||||||||||||||||||
| 47 | 1 | my $fp = (($y_true != $pos_label) & ($y_pred == $pos_label))->double->sum; | |||||||||||||||||||||||||||
| 48 | 1 | my $fn = (($y_true == $pos_label) & ($y_pred != $pos_label))->double->sum; | |||||||||||||||||||||||||||
| 49 | 1 | ▶ | my $prec = ($tp + $fp) > 0 ? $tp / ($tp + $fp) : 0.0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | my $rec = ($tp + $fn) > 0 ? $tp / ($tp + $fn) : 0.0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | my $f1 = ($prec + $rec) > 0 ? 2 * $prec * $rec / ($prec + $rec) : 0.0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 1 | return ($prec, $rec, $f1); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub mse { | ||||||||||||||||||||||||||
|
Subroutine
mse: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 2 | my ($y_true, $y_pred) = @_; | |||||||||||||||||||||||||||
| 57 | 2 | my $diff = $y_true->double - $y_pred->double; | |||||||||||||||||||||||||||
| 58 | 2 | return ($diff * $diff)->avg; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | sub rmse { | ||||||||||||||||||||||||||
|
Subroutine
rmse: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 1 | my ($y_true, $y_pred) = @_; | |||||||||||||||||||||||||||
| 63 | 1 | return sqrt(mse($y_true, $y_pred)); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub mae { | ||||||||||||||||||||||||||
|
Subroutine
mae: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 1 | my ($y_true, $y_pred) = @_; | |||||||||||||||||||||||||||
| 68 | 1 | return abs($y_true->double - $y_pred->double)->avg; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | ▶ | sub r2_score { | ||||||||||||||||||||||||||
|
Subroutine
r2_score: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 1 | my ($y_true, $y_pred) = @_; | |||||||||||||||||||||||||||
| 73 | 1 | my $yt = $y_true->double; | |||||||||||||||||||||||||||
| 74 | 1 | my $yp = $y_pred->double; | |||||||||||||||||||||||||||
| 75 | 1 | my $ss_res = (($yt - $yp) ** 2)->sum; | |||||||||||||||||||||||||||
| 76 | 1 | my $ss_tot = (($yt - $yt->avg) ** 2)->sum; | |||||||||||||||||||||||||||
| 77 | 1 | ▶ | return $ss_tot == 0 ? 0.0 : 1.0 - ($ss_res / $ss_tot); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | sub cosine_similarity { | ||||||||||||||||||||||||||
|
Subroutine
cosine_similarity: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 1 | my ($a, $b) = @_; | |||||||||||||||||||||||||||
| 82 | 1 | my $ad = $a->double; | |||||||||||||||||||||||||||
| 83 | 1 | my $bd = $b->double; | |||||||||||||||||||||||||||
| 84 | 1 | my $dot = ($ad * $bd)->sumover; | |||||||||||||||||||||||||||
| 85 | 1 | my $na = sqrt(($ad * $ad)->sumover); | |||||||||||||||||||||||||||
| 86 | 1 | my $nb = sqrt(($bd * $bd)->sumover); | |||||||||||||||||||||||||||
| 87 | 1 | my $den = $na * $nb; | |||||||||||||||||||||||||||
| 88 | 1 | $den->where($den == 0) .= 1.0; | |||||||||||||||||||||||||||
| 89 | 1 | my $res = $dot / $den; | |||||||||||||||||||||||||||
| 90 | 1 | ▶ | return $res->nelem == 1 ? $res->sclr : $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub roc_auc { | ||||||||||||||||||||||||||
|
Subroutine
roc_auc: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 1 | my ($y_true, $y_score) = @_; | |||||||||||||||||||||||||||
| 95 | 1 | my $n = $y_true->nelem; | |||||||||||||||||||||||||||
| 96 | 4 | my @pairs = sort { $b->[0] <=> $a->[0] } | |||||||||||||||||||||||||||
| 97 | 1 | ▶ | map { [ $y_score->at($_), $y_true->at($_) ? 1 : 0 ] } (0 .. $n - 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | my $n_pos = grep { $_->[1] == 1 } @pairs; | |||||||||||||||||||||||||||
| 100 | 1 | my $n_neg = $n - $n_pos; | |||||||||||||||||||||||||||
| 101 | 1 | ▶ | return 0.0 if $n_pos == 0 || $n_neg == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$n_pos == 0 or $n_neg == 0
MC/DC0%0/2
$n_pos == 0 or $n_neg == 0
$n_pos == 0
$n_neg == 0
Truth table33%1/3
A or B
A= $n_pos == 0B= $n_neg == 0
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | my ($tp, $fp, $prev_tp, $prev_fp, $auc) = (0, 0, 0, 0, 0.0); | |||||||||||||||||||||||||||
| 104 | 1 | for my $p (@pairs) { | |||||||||||||||||||||||||||
| 105 | 4 | ▶ | if ($p->[1]) { $tp++ } else { $fp++ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 4 | $auc += ($fp - $prev_fp) * ($tp + $prev_tp) / 2.0; | |||||||||||||||||||||||||||
| 107 | 4 | ($prev_tp, $prev_fp) = ($tp, $fp); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 1 | return $auc / ($n_pos * $n_neg); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | 1; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||