The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Data::CosineSimilarity - Compute the Cosine Similarity

SYNOPSIS

$cs = Data::CosineSimilarity->new;
$cs->add( label1 => { feature1 => 3, feature2 => 1, feature3 => 10 } );
$cs->add( label2 => ... );
$cs->add( label3 => ... );

# computes the cosine similarity
my $r = $cs->similarity( 'label1', 'label2' );

# the result object
my $cosine = $r->cosine;
my $radian = $r->radian;
my $degree = $r->degree;
my ($label1, $label2) = $r->labels;

# computes all the cosine similarity between 'label1' and the others.
my @all = $cs->all_for_label('label1');

# computes all, and returns the best
my ($best_label, $r) = $cs->best_for_label('label2');

# computes all, and returns the worst
my ($worst_label, $r) = $cs->worst_for_label('label2');

DESCRIPTION

Compute the cosine similarities between a set of vectors.

$class->new( %opts )

If all the feature vectors are normed then the computation of the cosine becomes just the dot product of the vectors. In this case, specify the option normed => 1, the performance will be greatly improved.

$self->add( label => $features )

$self->similarity( $label1, $label2 )

$self->all_for_label( $label )

$self->best_for_label( $label )

$self->worst_for_label( $label )

AUTHOR

Antoine Imbert, <antoine.imbert at gmail.com>

LICENSE AND COPYRIGHT

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.