pearson_search.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_CF_PEARSON_SEARCH_HPP
13 #define MLPACK_METHODS_CF_PEARSON_SEARCH_HPP
14 
15 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace cf {
20 
46 {
47  public:
57  PearsonSearch(const arma::mat& referenceSet)
58  {
59  // Normalize all vectors in referenceSet.
60  // For each vector x, first subtract mean(x) from each element in x.
61  // Then normalize the vector to unit length.
62  arma::mat normalizedSet(arma::size(referenceSet));
63  normalizedSet = arma::normalise(
64  referenceSet.each_row() - arma::mean(referenceSet));
65 
66  neighborSearch.Train(std::move(normalizedSet));
67  }
68 
78  void Search(const arma::mat& query, const size_t k,
79  arma::Mat<size_t>& neighbors, arma::mat& similarities)
80  {
81  // Normalize all vectors in query.
82  // For each vector x, first subtract mean(x) from each element in x.
83  // Then normalize the vector to unit length.
84  arma::mat normalizedQuery;
85  normalizedQuery = arma::normalise(query.each_row() - arma::mean(query));
86 
87  neighborSearch.Search(normalizedQuery, k, neighbors, similarities);
88 
89  // Resulting similarities from Search() are Euclidean distance.
90  // For normalized vectors a and b, pearson(a, b) = 1 - dis(a, b) ^ 2 / 2,
91  // where dis(a, b) is Euclidean distance.
92  // Furthermore, we restrict the range of similarity to be [0, 1]:
93  // similarities = (pearson(a,b) + 1) / 2.0. As a result we have the
94  // following formula.
95  similarities = 1 - arma::pow(similarities, 2) / 4.0;
96  }
97 
98  private:
100  neighbor::KNN neighborSearch;
101 };
102 
103 } // namespace cf
104 } // namespace mlpack
105 
106 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
The NeighborSearch class is a template class for performing distance-based neighbor searches...
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.
PearsonSearch(const arma::mat &referenceSet)
Constructor with reference set.
Nearest neighbor search with pearson distance (or furthest neighbor search with pearson correlation)...
void Search(const MatType &querySet, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances)
For each point in the query set, compute the nearest neighbors and store the output in the given matr...
void Train(MatType referenceSet)
Set the reference set to a new reference set, and build a tree if necessary.