Skip to content

Commit b876f73

Browse files
committed
perf:Faster vector result derivation using .top-k-elements-matrix().
refactor:More universal .classify-by-profile signature. (Maybe it is a fix.)
1 parent 33051ec commit b876f73

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

lib/ML/SparseMatrixRecommender.rakumod

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,7 @@ class ML::SparseMatrixRecommender
520520
if $vector-result {
521521

522522
if $nrecs < $rec.rows-count {
523-
my %recs2 = $rec.rules(:names).map({ $_.key.head => $_.value });
524-
my @recs2 = %recs2.grep(*.value > 0).sort(-*.value)>>.key[^$nrecs];
525-
$rec = $rec[@recs2;*].impose-row-names($rec.row-names);
523+
$rec = $rec.top-k-elements-matrix($nrecs)
526524
}
527525

528526
} else {
@@ -595,9 +593,7 @@ class ML::SparseMatrixRecommender
595593
if $vector-result {
596594

597595
if $nrecs < $rec.rows-count {
598-
my %recs2 = $rec.rules(:names).map({ $_.key.head => $_.value });
599-
my @recs2 = %recs2.grep(*.value > 0).sort(-*.value)>>.key[^$nrecs];
600-
$rec = $rec[@recs2;*].impose-row-names($rec.row-names);
596+
$rec = $rec.top-k-elements-matrix($nrecs)
601597
}
602598

603599
} else {
@@ -747,19 +743,26 @@ class ML::SparseMatrixRecommender
747743
#| C<:$normalize> -- Should the scores be normalized?
748744
#| C<:$ignore-unknown> -- Should the unknown tags be ignored or not?
749745
#| C<$object> -- Should the result be an object or not?
750-
multi method classify-by-profile(Str $tagType, @profile, *%args) {
751-
return self.classify-by-profile($tagType, %(@profile X=> 1.0).Mix, |%args);
752-
}
753-
754746
multi method classify-by-profile(Str:D $tag-type,
755-
Mix:D $profile,
747+
$profile is copy,
756748
UInt:D :$n-top-nearest-neighbors = 100,
757749
Bool:D :$voting = False,
758750
Bool:D :$drop-zero-scored-labels = True,
759751
:$max-number-of-labels = Whatever,
760752
Bool:D :$normalize = True,
761753
Bool:D :$warn = False) {
762754

755+
# Process $profile
756+
$profile = do given $profile {
757+
when $_ ~~ Str:D { [$_, ].Mix}
758+
when $_ ~~ (Array:D | List:D | Seq:D) && $_.all ~~ Str:D {$_.Mix}
759+
when $_ ~~ Map:D {$_.Mix}
760+
when $_ ~~ Mix:D || $_ ~~ Math::SparseMatrix:D {$profile}
761+
default {
762+
die 'Do not know how to process the second arugment.'
763+
}
764+
}
765+
763766
# Verify tag type
764767
unless $tag-type self.take-matrices.keys {
765768
die "The value of the first argument is not a known tag type.";
@@ -773,15 +776,19 @@ class ML::SparseMatrixRecommender
773776
:$warn
774777
).take-value;
775778

779+
$recs.to-adapted if $!native;
780+
776781
# "Nothing" result
777-
if $recs.column-sums.head== 0 {
778-
self.set-value(%());
782+
if $recs.column-sums.head == 0 {
783+
self.set-value(Whatever);
779784
return self;
780785
}
781786

782787
# Get the tag type matrix
783788
my $mat-tag-type = self.take-matrices{$tag-type}.clone;
784789

790+
$mat-tag-type.to-adapted if $!native;
791+
785792
# Transpose in place
786793
$recs = $recs.transpose;
787794

0 commit comments

Comments
 (0)