CosineSearch Class Reference

Nearest neighbor search with cosine distance. More...

Public Member Functions

 CosineSearch (const arma::mat &referenceSet)
 Constructor with reference set. More...

 
void Search (const arma::mat &query, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &similarities)
 Given a set of query points, find the nearest k neighbors, and return similarities. More...

 

Detailed Description

Nearest neighbor search with cosine distance.

Note that, with normalized vectors, neighbor search with cosine distance is equivalent to neighbor search with Euclidean distance. Therefore, instead of performing neighbor search directly with cosine distance, we first normalize all vectors to unit length, and then use neighbor::KNN (i.e. NeighborSearch with Euclidean distance, KDTree). Cosine similarities are calculated from Euclidean distance.

An example of how to use CosineSearch in CF is shown below:

extern arma::mat data; // data is a (user, item, rating) table.
// Users for whom recommendations are generated.
extern arma::Col<size_t> users;
arma::Mat<size_t> recommendations; // Resulting recommendations.
CFType<> cf(data);
// Generate 10 recommendations for all users.
cf.template GetRecommendations<CosineSearch>(10, recommendations);

Definition at line 44 of file cosine_search.hpp.

Constructor & Destructor Documentation

◆ CosineSearch()

CosineSearch ( const arma::mat &  referenceSet)
inline

Constructor with reference set.

All vectors in reference set are normalized to unit length.

Parameters
referenceSetSet of reference points.

Definition at line 53 of file cosine_search.hpp.

References NeighborSearch< SortPolicy, MetricType, MatType, TreeType, DualTreeTraversalType, SingleTreeTraversalType >::Train().

Member Function Documentation

◆ Search()

void Search ( const arma::mat &  query,
const size_t  k,
arma::Mat< size_t > &  neighbors,
arma::mat &  similarities 
)
inline

Given a set of query points, find the nearest k neighbors, and return similarities.

Similarities are non-negative and no larger than one.

Parameters
queryA set of query points.
kNumber of neighbors to search.
neighborsNearest neighbors.
similaritiesSimilarities between query point and its neighbors.

Definition at line 70 of file cosine_search.hpp.

References NeighborSearch< SortPolicy, MetricType, MatType, TreeType, DualTreeTraversalType, SingleTreeTraversalType >::Search().


The documentation for this class was generated from the following file:
  • /home/ryan/src/mlpack.org/_src/mlpack-git/src/mlpack/methods/cf/neighbor_search_policies/cosine_search.hpp