\section{Introduction}\label{akfntutorial_intro_akfntut}
{\bfseries mlpack} implements multiple strategies for approximate furthest neighbor search in its {\ttfamily mlpack\+\_\+approx\+\_\+kfn} and {\ttfamily mlpack\+\_\+kfn} programs (each program corresponds to different techniques). This tutorial discusses what problems these algorithms solve and how to use each of the techniques that {\bfseries mlpack} implements.

{\bfseries mlpack} implements five approximate furthest neighbor search algorithms\+:


\begin{DoxyItemize}
\item brute-\/force search (in {\ttfamily mlpack\+\_\+kfn})
\item single-\/tree search (in {\ttfamily mlpack\+\_\+kfn})
\item dual-\/tree search (in {\ttfamily mlpack\+\_\+kfn})
\item query-\/dependent approximate furthest neighbor (Q\+D\+A\+FN) (in {\ttfamily mlpack\+\_\+approx\+\_\+kfn})
\item Drusilla\+Select (in {\ttfamily mlpack\+\_\+approx\+\_\+kfn})
\end{DoxyItemize}

These methods are described in the following papers\+:


\begin{DoxyCode}
@inproceedings\{curtin2013tree,
  title=\{Tree-Independent Dual-Tree Algorithms\},
  author=\{Curtin, Ryan R. and March, William B. and Ram, Parikshit and Anderson,
      David V. and Gray, Alexander G. and Isbell Jr., Charles L.\},
  booktitle=\{Proceedings of The 30th International Conference on Machine
      Learning (ICML \textcolor{stringliteral}{'13)\},}
\textcolor{stringliteral}{  pages=\{1435--1443\},}
\textcolor{stringliteral}{  year=\{2013\}}
\textcolor{stringliteral}{\}}
\end{DoxyCode}



\begin{DoxyCode}
@incollection\{pagh2015approximate,
  title=\{Approximate furthest neighbor in high dimensions\},
  author=\{Pagh, Rasmus and Silvestri, Francesco and Sivertsen, Johan and Skala,
      Matthew\},
  booktitle=\{Similarity Search and Applications\},
  pages=\{3--14\},
  year=\{2015\},
  publisher=\{Springer\}
\}
\end{DoxyCode}



\begin{DoxyCode}
@incollection\{curtin2016fast,
  title=\{Fast approximate furthest neighbors with data-dependent candidate
      selection\},
  author=\{Curtin, Ryan R., and Gardner, Andrew B.\},
  booktitle=\{Similarity Search and Applications\},
  pages=\{221--235\},
  year=\{2016\},
  publisher=\{Springer\}
\}
\end{DoxyCode}



\begin{DoxyCode}
@article\{curtin2018exploiting,
  title=\{Exploiting the structure of furthest neighbor search \textcolor{keywordflow}{for} fast
      approximate results\},
  author=\{Curtin, Ryan R., and Echauz, Javier, and Gardner, Andrew B.\},
  journal=\{Information Systems\},
  year=\{2018\},
  publisher=\{Elsevier\}
\}
\end{DoxyCode}


The problem of furthest neighbor search is simple, and is the opposite of the much-\/more-\/studied nearest neighbor search problem. Given a set of reference points $R$ (the set in which we are searching), and a set of query points $Q$ (the set of points for which we want the furthest neighbor), our goal is to return the $k$ furthest neighbors for each query point in $Q$\+:

\[ \operatorname{k-argmax}_{p_r \in R} d(p_q, p_r). \]

In order to solve this problem, {\bfseries mlpack} provides a number of interfaces.


\begin{DoxyItemize}
\item two \doxyref{simple command-\/line executables}{p.}{akfntutorial_cli_akfntut} to calculate approximate furthest neighbors
\item a simple \doxyref{C++ class for Q\+D\+A\+FN}{p.}{akfntutorial_cpp_qdafn_akfntut}
\item a simple \doxyref{C++ class for Drusilla\+Select}{p.}{akfntutorial_cpp_ds_akfntut}
\item a simple C++ class for tree-\/based and brute-\/force search
\end{DoxyItemize}\section{Table of Contents}\label{akfntutorial_toc_akfntut}
A list of all the sections this tutorial contains.


\begin{DoxyItemize}
\item \doxyref{Introduction}{p.}{akfntutorial_intro_akfntut}
\item \doxyref{Table of Contents}{p.}{akfntutorial_toc_akfntut}
\item \doxyref{Which algorithm should be used?}{p.}{akfntutorial_which_akfntut}
\item \doxyref{Command-\/line \textquotesingle{}mlpack\+\_\+approx\+\_\+kfn\textquotesingle{} and \textquotesingle{}mlpack\+\_\+kfn\textquotesingle{}}{p.}{akfntutorial_cli_akfntut}
\begin{DoxyItemize}
\item \doxyref{Calculate 5 furthest neighbors with default options}{p.}{akfntutorial_cli_ex1_akfntut}
\item \doxyref{Specifying algorithm parameters for Drusilla\+Select}{p.}{akfntutorial_cli_ex2_akfntut}
\item \doxyref{Using Q\+D\+A\+FN instead of Drusilla\+Select}{p.}{akfntutorial_cli_ex3_akfntut}
\item \doxyref{Printing results quality with exact distances}{p.}{akfntutorial_cli_ex4_akfntut}
\item \doxyref{Using cached exact distances for quality results}{p.}{akfntutorial_cli_ex5_akfntut}
\item \doxyref{Using tree-\/based approximation with mlpack\+\_\+kfn}{p.}{akfntutorial_cli_ex6_akfntut}
\item \doxyref{Different algorithms with \textquotesingle{}mlpack\+\_\+kfn\textquotesingle{}}{p.}{akfntutorial_cli_ex7_akfntut}
\item \doxyref{Saving a model for later use}{p.}{akfntutorial_cli_ex8_akfntut}
\item \doxyref{Final command-\/line program notes}{p.}{akfntutorial_cli_final_akfntut}
\end{DoxyItemize}
\item \doxyref{Drusilla\+Select C++ class}{p.}{akfntutorial_cpp_ds_akfntut}
\begin{DoxyItemize}
\item \doxyref{Approximate furthest neighbors with defaults}{p.}{akfntutorial_cpp_ex1_ds_akfntut}
\item \doxyref{Custom numbers of tables and projections}{p.}{akfntutorial_cpp_ex2_ds_akfntut}
\item \doxyref{Accessing the candidate set}{p.}{akfntutorial_cpp_ex3_ds_akfntut}
\item \doxyref{Retraining on a new reference set}{p.}{akfntutorial_cpp_ex4_ds_akfntut}
\item \doxyref{Running on sparse data}{p.}{akfntutorial_cpp_ex5_ds_akfntut}
\end{DoxyItemize}
\item \doxyref{Q\+D\+A\+FN C++ class}{p.}{akfntutorial_cpp_qdafn_akfntut}
\begin{DoxyItemize}
\item \doxyref{Approximate furthest neighbors with defaults}{p.}{akfntutorial_cpp_ex1_qdafn_akfntut}
\item \doxyref{Custom numbers of tables and projections}{p.}{akfntutorial_cpp_ex2_qdafn_akfntut}
\item \doxyref{Accessing the candidate set}{p.}{akfntutorial_cpp_ex3_qdafn_akfntut}
\item \doxyref{Retraining on a new reference set}{p.}{akfntutorial_cpp_ex4_qdafn_akfntut}
\item \doxyref{Running on sparse data}{p.}{akfntutorial_cpp_ex5_qdafn_akfntut}
\end{DoxyItemize}
\item \doxyref{K\+FN C++ class}{p.}{akfntutorial_cpp_ns_akfntut}
\begin{DoxyItemize}
\item \doxyref{Simple furthest neighbors example}{p.}{akfntutorial_cpp_ex1_ns_akfntut}
\item \doxyref{Retraining on a new reference set}{p.}{akfntutorial_cpp_ex2_ns_akfntut}
\item \doxyref{Searching in single-\/tree mode}{p.}{akfntutorial_cpp_ex3_ns_akfntut}
\item \doxyref{Searching in brute-\/force mode}{p.}{akfntutorial_cpp_ex4_ns_akfntut}
\end{DoxyItemize}
\item \doxyref{Further documentation}{p.}{akfntutorial_further_doc_akfntut}
\end{DoxyItemize}\section{Which algorithm should be used?}\label{akfntutorial_which_akfntut}
There are three algorithms for furthest neighbor search that {\bfseries mlpack} implements, and each is suited to a different setting. Below is some basic guidance on what should be used. Note that the question of \char`\"{}which algorithm
should be used\char`\"{} is a very difficult question to answer, so the guidance below is just that---guidance---and may not be right for a particular problem.


\begin{DoxyItemize}
\item {\ttfamily Drusilla\+Select} is very fast and will perform extremely well for datasets with outliers or datasets with structure (like low-\/dimensional datasets embedded in high dimensions)
\item {\ttfamily Q\+D\+A\+FN} is a random approach and therefore should be well-\/suited for datasets with little to no structure
\item The tree-\/based approaches (the {\ttfamily K\+FN} class and the {\ttfamily mlpack\+\_\+kfn} program) is best suited for low-\/dimensional datasets, and is most effective when very small levels of approximation are desired, or when exact results are desired.
\item Dual-\/tree search is most useful when the query set is large and structured (like for all-\/furthest-\/neighbor search).
\item Single-\/tree search is more useful when the query set is small.
\end{DoxyItemize}\section{Command-\/line \textquotesingle{}mlpack\+\_\+approx\+\_\+kfn\textquotesingle{} and \textquotesingle{}mlpack\+\_\+kfn\textquotesingle{}}\label{akfntutorial_cli_akfntut}
{\bfseries mlpack} provides two command-\/line programs to solve approximate furthest neighbor search\+:


\begin{DoxyItemize}
\item {\ttfamily mlpack\+\_\+approx\+\_\+kfn}, for the Q\+D\+A\+FN and Drusilla\+Select approaches
\item {\ttfamily mlpack\+\_\+kfn}, for exact and approximate tree-\/based approaches
\end{DoxyItemize}

These two programs allow a large number of algorithms to be used to find approximate furthest neighbors. Note that the {\ttfamily mlpack\+\_\+kfn} program is also documented by the \doxyref{Command-\/\+Line \textquotesingle{}mlpack\+\_\+knn\textquotesingle{}}{p.}{nstutorial_cli_nstut} section of the \doxyref{Neighbor\+Search tutorial (k-\/nearest-\/neighbors)}{p.}{nstutorial} page, as it shares options with the {\ttfamily mlpack\+\_\+knn} program.

Below are several examples of how the {\ttfamily mlpack\+\_\+approx\+\_\+kfn} and {\ttfamily mlpack\+\_\+kfn} programs might be used. The first examples focus on the {\ttfamily mlpack\+\_\+approx\+\_\+kfn} program, and the last few show how {\ttfamily mlpack\+\_\+kfn} can be used to produce approximate results.\subsection{Calculate 5 furthest neighbors with default options}\label{akfntutorial_cli_ex1_akfntut}
Here we have a query dataset {\ttfamily queries.\+csv} and a reference dataset {\ttfamily refs.\+csv} and we wish to find the 5 furthest neighbors of every query point in the reference dataset. We may do that with the {\ttfamily mlpack\+\_\+approx\+\_\+kfn} algorithm, using the default of the {\ttfamily Drusilla\+Select} algorithm with default parameters.


\begin{DoxyCode}
$ mlpack\_approx\_kfn -q queries.csv -r refs.csv -v -k 5 -n n.csv -d d.csv
[INFO ] Loading \textcolor{stringliteral}{'refs.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Building DrusillaSelect model...
[INFO ] Model built.
[INFO ] Loading \textcolor{stringliteral}{'queries.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Searching \textcolor{keywordflow}{for} 5 furthest neighbors with DrusillaSelect...
[INFO ] Search complete.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'n.csv'}.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'d.csv'}.
[INFO ]
[INFO ] Execution parameters:
[INFO ]   algorithm: ds
[INFO ]   calculate\_error: \textcolor{keyword}{false}
[INFO ]   distances\_file: d.csv
[INFO ]   exact\_distances\_file: \textcolor{stringliteral}{""}
[INFO ]   help: \textcolor{keyword}{false}
[INFO ]   info: \textcolor{stringliteral}{""}
[INFO ]   input\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   k: 5
[INFO ]   neighbors\_file: n.csv
[INFO ]   num\_projections: 5
[INFO ]   num\_tables: 5
[INFO ]   output\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   query\_file: queries.csv
[INFO ]   reference\_file: refs.csv
[INFO ]   verbose: \textcolor{keyword}{true}
[INFO ]   version: \textcolor{keyword}{false}
[INFO ]
[INFO ] Program timers:
[INFO ]   drusilla\_select\_construct: 0.000342s
[INFO ]   drusilla\_select\_search: 0.000780s
[INFO ]   loading\_data: 0.010689s
[INFO ]   saving\_data: 0.005585s
[INFO ]   total\_time: 0.018592s
\end{DoxyCode}


Convenient timers for parts of the program operation are printed. The results, saved in {\ttfamily n.\+csv} and {\ttfamily d.\+csv}, indicate the furthest neighbors and distances for each query point. The row of the output file indicates the query point that the results are for. The neighbors are listed from furthest to nearest; so, the 4th element in the 3rd row of {\ttfamily d.\+csv} indicates the distance between the 3rd query point in {\ttfamily queries.\+csv} and its approximate 4th furthest neighbor. Similarly, the same element in {\ttfamily n.\+csv} indicates the index of the approximate 4th furthest neighbor (with respect to {\ttfamily refs.\+csv}).\subsection{Specifying algorithm parameters for Drusilla\+Select}\label{akfntutorial_cli_ex2_akfntut}
The {\ttfamily -\/p} ({\ttfamily --num\+\_\+projections}) and {\ttfamily -\/t} ({\ttfamily --num\+\_\+tables}) parameters affect the running of the {\ttfamily Drusilla\+Select} algorithm and the Q\+D\+A\+FN algorithm. Specifically, larger values for each of these parameters will search more possible candidate furthest neighbors and produce better results (at the cost of runtime). More details on how each of these parameters works is available in the original papers, the {\bfseries mlpack} source, or the documentation given by {\ttfamily --help}.

In the example below, we run {\ttfamily Drusilla\+Select} to find 4 furthest neighbors using 10 tables and 2 points in each table. In this case we have chosen to omit the {\ttfamily -\/n} {\ttfamily n.\+csv} option, meaning that only the output candidate distances will be written to {\ttfamily d.\+csv}.


\begin{DoxyCode}
$ mlpack\_approx\_kfn -q queries.csv -r refs.csv -v -k 4 -n n.csv -d d.csv -t 10 -p 2
[INFO ] Loading \textcolor{stringliteral}{'refs.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Building DrusillaSelect model...
[INFO ] Model built.
[INFO ] Loading \textcolor{stringliteral}{'queries.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Searching \textcolor{keywordflow}{for} 4 furthest neighbors with DrusillaSelect...
[INFO ] Search complete.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'n.csv'}.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'d.csv'}.
[INFO ]
[INFO ] Execution parameters:
[INFO ]   algorithm: ds
[INFO ]   calculate\_error: \textcolor{keyword}{false}
[INFO ]   distances\_file: d.csv
[INFO ]   exact\_distances\_file: \textcolor{stringliteral}{""}
[INFO ]   help: \textcolor{keyword}{false}
[INFO ]   info: \textcolor{stringliteral}{""}
[INFO ]   input\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   k: 4
[INFO ]   neighbors\_file: n.csv
[INFO ]   num\_projections: 2
[INFO ]   num\_tables: 10
[INFO ]   output\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   query\_file: queries.csv
[INFO ]   reference\_file: refs.csv
[INFO ]   verbose: \textcolor{keyword}{true}
[INFO ]   version: \textcolor{keyword}{false}
[INFO ]
[INFO ] Program timers:
[INFO ]   drusilla\_select\_construct: 0.000645s
[INFO ]   drusilla\_select\_search: 0.000551s
[INFO ]   loading\_data: 0.008518s
[INFO ]   saving\_data: 0.003734s
[INFO ]   total\_time: 0.014019s
\end{DoxyCode}
\subsection{Using Q\+D\+A\+F\+N instead of Drusilla\+Select}\label{akfntutorial_cli_ex3_akfntut}
The algorithm to be used for approximate furthest neighbor search can be specified with the {\ttfamily --algorithm} ({\ttfamily -\/a}) option to the {\ttfamily mlpack\+\_\+approx\+\_\+kfn} program. Below, we use the Q\+D\+A\+FN algorithm instead of the default. We leave the {\ttfamily -\/p} and {\ttfamily -\/t} options at their defaults---even though Q\+D\+A\+FN often requires more tables and points to get the same quality of results.


\begin{DoxyCode}
$ mlpack\_approx\_kfn -q queries.csv -r refs.csv -v -k 3 -n n.csv -d d.csv -a qdafn
[INFO ] Loading \textcolor{stringliteral}{'refs.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Building QDAFN model...
[INFO ] Model built.
[INFO ] Loading \textcolor{stringliteral}{'queries.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Searching \textcolor{keywordflow}{for} 3 furthest neighbors with QDAFN...
[INFO ] Search complete.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'n.csv'}.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'d.csv'}.
[INFO ]
[INFO ] Execution parameters:
[INFO ]   algorithm: qdafn
[INFO ]   calculate\_error: \textcolor{keyword}{false}
[INFO ]   distances\_file: d.csv
[INFO ]   exact\_distances\_file: \textcolor{stringliteral}{""}
[INFO ]   help: \textcolor{keyword}{false}
[INFO ]   info: \textcolor{stringliteral}{""}
[INFO ]   input\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   k: 3
[INFO ]   neighbors\_file: n.csv
[INFO ]   num\_projections: 5
[INFO ]   num\_tables: 5
[INFO ]   output\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   query\_file: queries.csv
[INFO ]   reference\_file: refs.csv
[INFO ]   verbose: \textcolor{keyword}{true}
[INFO ]   version: \textcolor{keyword}{false}
[INFO ]
[INFO ] Program timers:
[INFO ]   loading\_data: 0.008380s
[INFO ]   qdafn\_construct: 0.003399s
[INFO ]   qdafn\_search: 0.000886s
[INFO ]   saving\_data: 0.002253s
[INFO ]   total\_time: 0.015465s
\end{DoxyCode}
\subsection{Printing results quality with exact distances}\label{akfntutorial_cli_ex4_akfntut}
The {\ttfamily mlpack\+\_\+approx\+\_\+kfn} program can calculate the quality of the results if the {\ttfamily --calculate\+\_\+error} ({\ttfamily -\/e}) flag is specified. Below we use the program with its default parameters and calculate the error, which is displayed in the output. The error is only calculated for the furthest neighbor, not all k; therefore, in this example we have set {\ttfamily -\/k} to {\ttfamily 1}.


\begin{DoxyCode}
$ mlpack\_approx\_kfn -q queries.csv -r refs.csv -v -k 1 -e -q -n n.csv
[INFO ] Loading \textcolor{stringliteral}{'refs.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Building DrusillaSelect model...
[INFO ] Model built.
[INFO ] Loading \textcolor{stringliteral}{'queries.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Searching \textcolor{keywordflow}{for} 1 furthest neighbors with DrusillaSelect...
[INFO ] Search complete.
[INFO ] Calculating exact distances...
[INFO ] 28891 node combinations were scored.
[INFO ] 37735 base cases were calculated.
[INFO ] Calculation complete.
[INFO ] Average error: 1.08417.
[INFO ] Maximum error: 1.28712.
[INFO ] Minimum error: 1.
[INFO ]
[INFO ] Execution parameters:
[INFO ]   algorithm: ds
[INFO ]   calculate\_error: \textcolor{keyword}{true}
[INFO ]   distances\_file: \textcolor{stringliteral}{""}
[INFO ]   exact\_distances\_file: \textcolor{stringliteral}{""}
[INFO ]   help: \textcolor{keyword}{false}
[INFO ]   info: \textcolor{stringliteral}{""}
[INFO ]   input\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   k: 3
[INFO ]   neighbors\_file: \textcolor{stringliteral}{""}
[INFO ]   num\_projections: 5
[INFO ]   num\_tables: 5
[INFO ]   output\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   query\_file: queries.csv
[INFO ]   reference\_file: refs.csv
[INFO ]   verbose: \textcolor{keyword}{true}
[INFO ]   version: \textcolor{keyword}{false}
[INFO ]
[INFO ] Program timers:
[INFO ]   computing\_neighbors: 0.001476s
[INFO ]   drusilla\_select\_construct: 0.000309s
[INFO ]   drusilla\_select\_search: 0.000495s
[INFO ]   loading\_data: 0.008462s
[INFO ]   total\_time: 0.011670s
[INFO ]   tree\_building: 0.000202s
\end{DoxyCode}


Note that the output includes three lines indicating the error\+:


\begin{DoxyCode}
[INFO ] Average error: 1.08417.
[INFO ] Maximum error: 1.28712.
[INFO ] Minimum error: 1.
\end{DoxyCode}


In this case, a minimum error of 1 indicates an exact result, and over the entire query set the algorithm has returned a furthest neighbor candidate with maximum error 1.\+28712.\subsection{Using cached exact distances for quality results}\label{akfntutorial_cli_ex5_akfntut}
However, for large datasets, calculating the error may take a long time, because the exact furthest neighbors must be calculated. Therefore, if the exact furthest neighbor distances are already known, they may be passed in with the {\ttfamily --exact\+\_\+distances\+\_\+file} ({\ttfamily -\/x}) option in order to avoid the calculation. In the example below, we assume {\ttfamily exact.\+csv} contains the exact furthest neighbor distances. We run the {\ttfamily qdafn} algorithm in this example.

Note that the {\ttfamily -\/e} option must be specified for the {\ttfamily -\/x} option have any effect.


\begin{DoxyCode}
$ mlpack\_approx\_kfn -q queries.csv -r refs.csv -k 1 -e -x exact.csv -n n.csv -v -a qdafn
[INFO ] Loading \textcolor{stringliteral}{'refs.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Building QDAFN model...
[INFO ] Model built.
[INFO ] Loading \textcolor{stringliteral}{'queries.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Searching \textcolor{keywordflow}{for} 1 furthest neighbors with QDAFN...
[INFO ] Search complete.
[INFO ] Loading \textcolor{stringliteral}{'exact.csv'} as raw ASCII formatted data.  Size is 1 x 1000.
[INFO ] Average error: 1.06914.
[INFO ] Maximum error: 1.67407.
[INFO ] Minimum error: 1.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'n.csv'}.
[INFO ]
[INFO ] Execution parameters:
[INFO ]   algorithm: qdafn
[INFO ]   calculate\_error: \textcolor{keyword}{true}
[INFO ]   distances\_file: \textcolor{stringliteral}{""}
[INFO ]   exact\_distances\_file: exact.csv
[INFO ]   help: \textcolor{keyword}{false}
[INFO ]   info: \textcolor{stringliteral}{""}
[INFO ]   input\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   k: 1
[INFO ]   neighbors\_file: n.csv
[INFO ]   num\_projections: 5
[INFO ]   num\_tables: 5
[INFO ]   output\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   query\_file: queries.csv
[INFO ]   reference\_file: refs.csv
[INFO ]   verbose: \textcolor{keyword}{true}
[INFO ]   version: \textcolor{keyword}{false}
[INFO ]
[INFO ] Program timers:
[INFO ]   loading\_data: 0.010348s
[INFO ]   qdafn\_construct: 0.000318s
[INFO ]   qdafn\_search: 0.000793s
[INFO ]   saving\_data: 0.000259s
[INFO ]   total\_time: 0.012254s
\end{DoxyCode}
\subsection{Using tree-\/based approximation with mlpack\+\_\+kfn}\label{akfntutorial_cli_ex6_akfntut}
The {\ttfamily mlpack\+\_\+kfn} algorithm allows specifying a desired approximation level with the {\ttfamily --epsilon} ({\ttfamily -\/e}) option. The parameter must be greater than or equal to 0 and less than 1. A setting of 0 indicates exact search.

The example below runs dual-\/tree furthest neighbor search (the default algorithm) with the approximation parameter set to 0.\+5.


\begin{DoxyCode}
$ mlpack\_kfn -q queries.csv -r refs.csv -v -k 3 -e 0.5 -n n.csv -d d.csv
[INFO ] Loading \textcolor{stringliteral}{'refs.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Loaded reference data from \textcolor{stringliteral}{'refs.csv'} (3x1000).
[INFO ] Building reference tree...
[INFO ] Tree built.
[INFO ] Loading \textcolor{stringliteral}{'queries.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Loaded query data from \textcolor{stringliteral}{'queries.csv'} (3x1000).
[INFO ] Searching \textcolor{keywordflow}{for} 3 neighbors with dual-tree kd-tree search...
[INFO ] 1611 node combinations were scored.
[INFO ] 13938 base cases were calculated.
[INFO ] 1611 node combinations were scored.
[INFO ] 13938 base cases were calculated.
[INFO ] Search complete.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'n.csv'}.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'d.csv'}.
[INFO ]
[INFO ] Execution parameters:
[INFO ]   algorithm: dual\_tree
[INFO ]   distances\_file: d.csv
[INFO ]   epsilon: 0.5
[INFO ]   help: \textcolor{keyword}{false}
[INFO ]   info: \textcolor{stringliteral}{""}
[INFO ]   input\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   k: 3
[INFO ]   leaf\_size: 20
[INFO ]   naive: \textcolor{keyword}{false}
[INFO ]   neighbors\_file: n.csv
[INFO ]   output\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   percentage: 1
[INFO ]   query\_file: queries.csv
[INFO ]   random\_basis: \textcolor{keyword}{false}
[INFO ]   reference\_file: refs.csv
[INFO ]   seed: 0
[INFO ]   single\_mode: \textcolor{keyword}{false}
[INFO ]   tree\_type: kd
[INFO ]   true\_distances\_file: \textcolor{stringliteral}{""}
[INFO ]   true\_neighbors\_file: \textcolor{stringliteral}{""}
[INFO ]   verbose: \textcolor{keyword}{true}
[INFO ]   version: \textcolor{keyword}{false}
[INFO ]
[INFO ] Program timers:
[INFO ]   computing\_neighbors: 0.000442s
[INFO ]   loading\_data: 0.008060s
[INFO ]   saving\_data: 0.002850s
[INFO ]   total\_time: 0.012667s
[INFO ]   tree\_building: 0.000251s
\end{DoxyCode}


Note that the format of the output files {\ttfamily d.\+csv} and {\ttfamily n.\+csv} are the same as for {\ttfamily mlpack\+\_\+approx\+\_\+kfn}.\subsection{Different algorithms with \textquotesingle{}mlpack\+\_\+kfn\textquotesingle{}}\label{akfntutorial_cli_ex7_akfntut}
The {\ttfamily mlpack\+\_\+kfn} program offers a large number of different algorithms that can be used. The {\ttfamily --algorithm} ({\ttfamily -\/a}) may be used to specify three main different algorithm types\+: {\ttfamily naive} (brute-\/force search), {\ttfamily single\+\_\+tree} (single-\/tree search), {\ttfamily dual\+\_\+tree} (dual-\/tree search, the default), and {\ttfamily greedy} (\char`\"{}defeatist\char`\"{} greedy search, which goes to one leaf node of the tree then terminates). The example below uses single-\/tree search to find approximate neighbors with epsilon set to 0.\+1.


\begin{DoxyCode}
mlpack\_kfn -q queries.csv -r refs.csv -v -k 3 -e 0.1 -n n.csv -d d.csv -a single\_tree
[INFO ] Loading \textcolor{stringliteral}{'refs.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Loaded reference data from \textcolor{stringliteral}{'refs.csv'} (3x1000).
[INFO ] Building reference tree...
[INFO ] Tree built.
[INFO ] Loading \textcolor{stringliteral}{'queries.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Loaded query data from \textcolor{stringliteral}{'queries.csv'} (3x1000).
[INFO ] Searching \textcolor{keywordflow}{for} 3 neighbors with single-tree kd-tree search...
[INFO ] 13240 node combinations were scored.
[INFO ] 15924 base cases were calculated.
[INFO ] Search complete.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'n.csv'}.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'d.csv'}.
[INFO ]
[INFO ] Execution parameters:
[INFO ]   algorithm: single\_tree
[INFO ]   distances\_file: d.csv
[INFO ]   epsilon: 0.1
[INFO ]   help: \textcolor{keyword}{false}
[INFO ]   info: \textcolor{stringliteral}{""}
[INFO ]   input\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   k: 3
[INFO ]   leaf\_size: 20
[INFO ]   naive: \textcolor{keyword}{false}
[INFO ]   neighbors\_file: n.csv
[INFO ]   output\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   percentage: 1
[INFO ]   query\_file: queries.csv
[INFO ]   random\_basis: \textcolor{keyword}{false}
[INFO ]   reference\_file: refs.csv
[INFO ]   seed: 0
[INFO ]   single\_mode: \textcolor{keyword}{false}
[INFO ]   tree\_type: kd
[INFO ]   true\_distances\_file: \textcolor{stringliteral}{""}
[INFO ]   true\_neighbors\_file: \textcolor{stringliteral}{""}
[INFO ]   verbose: \textcolor{keyword}{true}
[INFO ]   version: \textcolor{keyword}{false}
[INFO ]
[INFO ] Program timers:
[INFO ]   computing\_neighbors: 0.000850s
[INFO ]   loading\_data: 0.007858s
[INFO ]   saving\_data: 0.003445s
[INFO ]   total\_time: 0.013084s
[INFO ]   tree\_building: 0.000250s
\end{DoxyCode}
\subsection{Saving a model for later use}\label{akfntutorial_cli_ex8_akfntut}
The {\ttfamily mlpack\+\_\+approx\+\_\+kfn} and {\ttfamily mlpack\+\_\+kfn} programs both allow models to be saved and loaded for future use. The {\ttfamily --output\+\_\+model\+\_\+file} ({\ttfamily -\/M}) option allows specifying where to save a model, and the {\ttfamily --input\+\_\+model\+\_\+file} ({\ttfamily -\/m}) option allows a model to be loaded instead of trained. So, if you specify {\ttfamily --input\+\_\+model\+\_\+file} then you do not need to specify {\ttfamily --reference\+\_\+file} ({\ttfamily -\/r}), {\ttfamily --num\+\_\+projections} ({\ttfamily -\/p}), or {\ttfamily --num\+\_\+tables} ({\ttfamily -\/t}).

The example below saves a model with 10 projections and 5 tables. Note that neither {\ttfamily --query\+\_\+file} ({\ttfamily -\/q}) nor {\ttfamily -\/k} are specified; this run only builds the model and saves it to {\ttfamily model.\+bin}.


\begin{DoxyCode}
$ mlpack\_approx\_kfn -r refs.csv -t 5 -p 10 -v -M model.bin
[INFO ] Loading \textcolor{stringliteral}{'refs.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Building DrusillaSelect model...
[INFO ] Model built.
[INFO ]
[INFO ] Execution parameters:
[INFO ]   algorithm: ds
[INFO ]   calculate\_error: \textcolor{keyword}{false}
[INFO ]   distances\_file: \textcolor{stringliteral}{""}
[INFO ]   exact\_distances\_file: \textcolor{stringliteral}{""}
[INFO ]   help: \textcolor{keyword}{false}
[INFO ]   info: \textcolor{stringliteral}{""}
[INFO ]   input\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   k: 0
[INFO ]   neighbors\_file: \textcolor{stringliteral}{""}
[INFO ]   num\_projections: 10
[INFO ]   num\_tables: 5
[INFO ]   output\_model\_file: model.bin
[INFO ]   query\_file: \textcolor{stringliteral}{""}
[INFO ]   reference\_file: refs.csv
[INFO ]   verbose: \textcolor{keyword}{true}
[INFO ]   version: \textcolor{keyword}{false}
[INFO ]
[INFO ] Program timers:
[INFO ]   drusilla\_select\_construct: 0.000321s
[INFO ]   loading\_data: 0.004700s
[INFO ]   total\_time: 0.007320s
\end{DoxyCode}


Now, with the model saved, we can run approximate furthest neighbor search on a query set using the saved model\+:


\begin{DoxyCode}
$ mlpack\_approx\_kfn -m model.bin -q queries.csv -k 3 -d d.csv -n n.csv -v
[INFO ] Loading \textcolor{stringliteral}{'queries.csv'} as CSV data.  Size is 3 x 1000.
[INFO ] Searching \textcolor{keywordflow}{for} 3 furthest neighbors with DrusillaSelect...
[INFO ] Search complete.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'n.csv'}.
[INFO ] Saving CSV data to \textcolor{stringliteral}{'d.csv'}.
[INFO ]
[INFO ] Execution parameters:
[INFO ]   algorithm: ds
[INFO ]   calculate\_error: \textcolor{keyword}{false}
[INFO ]   distances\_file: d.csv
[INFO ]   exact\_distances\_file: \textcolor{stringliteral}{""}
[INFO ]   help: \textcolor{keyword}{false}
[INFO ]   info: \textcolor{stringliteral}{""}
[INFO ]   input\_model\_file: model.bin
[INFO ]   k: 3
[INFO ]   neighbors\_file: n.csv
[INFO ]   num\_projections: 5
[INFO ]   num\_tables: 5
[INFO ]   output\_model\_file: \textcolor{stringliteral}{""}
[INFO ]   query\_file: queries.csv
[INFO ]   reference\_file: \textcolor{stringliteral}{""}
[INFO ]   verbose: \textcolor{keyword}{true}
[INFO ]   version: \textcolor{keyword}{false}
[INFO ]
[INFO ] Program timers:
[INFO ]   drusilla\_select\_search: 0.000878s
[INFO ]   loading\_data: 0.004599s
[INFO ]   saving\_data: 0.003006s
[INFO ]   total\_time: 0.009234s
\end{DoxyCode}


These options work in the same way for both the {\ttfamily mlpack\+\_\+approx\+\_\+kfn} and {\ttfamily mlpack\+\_\+kfn} programs.\subsection{Final command-\/line program notes}\label{akfntutorial_cli_final_akfntut}
Both the {\ttfamily mlpack\+\_\+kfn} and {\ttfamily mlpack\+\_\+approx\+\_\+kfn} programs contain numerous options not fully documented in these short examples. You can run each program with the {\ttfamily --help} ({\ttfamily -\/h}) option for more information.\section{Drusilla\+Select C++ class}\label{akfntutorial_cpp_ds_akfntut}
{\bfseries mlpack} provides a simple {\ttfamily Drusilla\+Select} C++ class that can be used inside of C++ programs to perform approximate furthest neighbor search. The class has only one template parameter---{\ttfamily Mat\+Type---which} specifies the type of matrix to be use. That means the class can be used with either dense data (of type {\ttfamily arma\+::mat}) or sparse data (of type {\ttfamily arma\+::sp\+\_\+mat}).

The following examples show simple usage of this class.\subsection{Approximate furthest neighbors with defaults}\label{akfntutorial_cpp_ex1_ds_akfntut}
The code below builds a {\ttfamily Drusilla\+Select} model with default options on the matrix {\ttfamily dataset}, then queries for the approximate furthest neighbor of every point in the {\ttfamily queries} matrix.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/approx_kfn/drusilla_select.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::mat dataset;
\textcolor{comment}{// The query set.}
\textcolor{keyword}{extern} arma::mat queries;

\textcolor{comment}{// Construct the model with defaults.}
DrusillaSelect<> ds(dataset);

\textcolor{comment}{// Query the model, putting output into the following two matrices.}
arma::mat distances;
arma::Mat<size\_t> neighbors;
ds.Search(queries, 1, neighbors, distances);
\end{DoxyCode}


At the end of this code, both the {\ttfamily distances} and {\ttfamily neighbors} matrices will have number of columns equal to the number of columns in the {\ttfamily queries} matrix. So, each column of the {\ttfamily distances} and {\ttfamily neighbors} matrices are the distances or neighbors of the corresponding column in the {\ttfamily queries} matrix.\subsection{Custom numbers of tables and projections}\label{akfntutorial_cpp_ex2_ds_akfntut}
The following example constructs a Drusilla\+Select model with 10 tables and 5 projections. Once that is done it performs the same task as the previous example.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/approx_kfn/drusilla_select.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::mat dataset;
\textcolor{comment}{// The query set.}
\textcolor{keyword}{extern} arma::mat queries;

\textcolor{comment}{// Construct the model with custom parameters.}
DrusillaSelect<> ds(dataset, 10, 5);

\textcolor{comment}{// Query the model, putting output into the following two matrices.}
arma::mat distances;
arma::Mat<size\_t> neighbors;
ds.Search(queries, 1, neighbors, distances);
\end{DoxyCode}
\subsection{Accessing the candidate set}\label{akfntutorial_cpp_ex3_ds_akfntut}
The {\ttfamily Drusilla\+Select} algorithm merely scans the reference set and extracts a number of points that will be queried in a brute-\/force fashion when the {\ttfamily Search()} method is called. We can access this set with the {\ttfamily Candidate\+Set()} method. The code below prints the fifth point of the candidate set.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/approx_kfn/drusilla_select.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::mat dataset;

\textcolor{comment}{// Construct the model with custom parameters.}
DrusillaSelect<> ds(dataset, 10, 5);

\textcolor{comment}{// Print the fifth point of the candidate set.}
std::cout << ds.CandidateSet().col(4).t();
\end{DoxyCode}
\subsection{Retraining on a new reference set}\label{akfntutorial_cpp_ex4_ds_akfntut}
It is possible to retrain a {\ttfamily Drusilla\+Select} model with new parameters or with a new reference set. This is functionally equivalent to creating a new model. The example code below creates a first {\ttfamily Drusilla\+Select} model using 3 tables and 10 projections, and then retrains this with the same reference set using 10 tables and 3 projections.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/approx_kfn/drusilla_select.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::mat dataset;

\textcolor{comment}{// Construct the model with initial parameters.}
DrusillaSelect<> ds(dataset, 3, 10);

\textcolor{comment}{// Now retrain with different parameters.}
ds.Train(dataset, 10, 3);
\end{DoxyCode}
\subsection{Running on sparse data}\label{akfntutorial_cpp_ex5_ds_akfntut}
We can set the template parameter for {\ttfamily Drusilla\+Select} to {\ttfamily arma\+::sp\+\_\+mat} in order to perform furthest neighbor search on sparse data. This code below creates a {\ttfamily Drusilla\+Select} model using 4 tables and 6 projections with sparse input data, then searches for 3 approximate furthest neighbors.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/approx_kfn/drusilla_select.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::sp\_mat dataset;
\textcolor{comment}{// The query dataset.}
\textcolor{keyword}{extern} arma::sp\_mat querySet;

\textcolor{comment}{// Construct the model on sparse data.}
DrusillaSelect<arma::sp_mat> ds(dataset, 4, 6);

\textcolor{comment}{// Search on query data.}
arma::Mat<size\_t> neighbors;
arma::mat distances;
ds.Search(querySet, 3, neighbors, distances);
\end{DoxyCode}
\section{Q\+D\+A\+F\+N C++ class}\label{akfntutorial_cpp_qdafn_akfntut}
{\bfseries mlpack} also provides a standalone simple {\ttfamily Q\+D\+A\+FN} class for furthest neighbor search. The A\+PI for this class is virtually identical to the {\ttfamily Drusilla\+Select} class, and also has one template parameter to specify the type of matrix to be used (dense or sparse or other).

The following subsections demonstrate usage of the {\ttfamily Q\+D\+A\+FN} class in the same way as the previous section\textquotesingle{}s examples for {\ttfamily Drusilla\+Select}.\subsection{Approximate furthest neighbors with defaults}\label{akfntutorial_cpp_ex1_qdafn_akfntut}
The code below builds a {\ttfamily Q\+D\+A\+FN} model with default options on the matrix {\ttfamily dataset}, then queries for the approximate furthest neighbor of every point in the {\ttfamily queries} matrix.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/approx_kfn/qdafn.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::mat dataset;
\textcolor{comment}{// The query set.}
\textcolor{keyword}{extern} arma::mat queries;

\textcolor{comment}{// Construct the model with defaults.}
QDAFN<> qd(dataset);

\textcolor{comment}{// Query the model, putting output into the following two matrices.}
arma::mat distances;
arma::Mat<size\_t> neighbors;
qd.Search(queries, 1, neighbors, distances);
\end{DoxyCode}


At the end of this code, both the {\ttfamily distances} and {\ttfamily neighbors} matrices will have number of columns equal to the number of columns in the {\ttfamily queries} matrix. So, each column of the {\ttfamily distances} and {\ttfamily neighbors} matrices are the distances or neighbors of the corresponding column in the {\ttfamily queries} matrix.\subsection{Custom numbers of tables and projections}\label{akfntutorial_cpp_ex2_qdafn_akfntut}
The following example constructs a Q\+D\+A\+FN model with 15 tables and 30 projections. Once that is done it performs the same task as the previous example.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/approx_kfn/qdafn.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::mat dataset;
\textcolor{comment}{// The query set.}
\textcolor{keyword}{extern} arma::mat queries;

\textcolor{comment}{// Construct the model with custom parameters.}
QDAFN<> qdafn(dataset, 15, 30);

\textcolor{comment}{// Query the model, putting output into the following two matrices.}
arma::mat distances;
arma::Mat<size\_t> neighbors;
qdafn.Search(queries, 1, neighbors, distances);
\end{DoxyCode}
\subsection{Accessing the candidate set}\label{akfntutorial_cpp_ex3_qdafn_akfntut}
The {\ttfamily Q\+D\+A\+FN} algorithm scans the reference set, extracting points that have been projected onto random directions. Each random direction corresponds to a single table. The {\ttfamily Q\+D\+A\+FN} class stores these points as a vector of matrices, which can be accessed with the {\ttfamily Candidate\+Set()} method. The code below prints the fifth point of the candidate set of the third table.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/approx_kfn/qdafn.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::mat dataset;

\textcolor{comment}{// Construct the model with custom parameters.}
QDAFN<> qdafn(dataset, 10, 5);

\textcolor{comment}{// Print the fifth point of the candidate set.}
std::cout << ds.CandidateSet(2).col(4).t();
\end{DoxyCode}
\subsection{Retraining on a new reference set}\label{akfntutorial_cpp_ex4_qdafn_akfntut}
It is possible to retrain a {\ttfamily Q\+D\+A\+FN} model with new parameters or with a new reference set. This is functionally equivalent to creating a new model. The example code below creates a first {\ttfamily Q\+D\+A\+FN} model using 10 tables and 40 projections, and then retrains this with the same reference set using 15 tables and 25 projections.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/approx_kfn/qdafn.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::mat dataset;

\textcolor{comment}{// Construct the model with initial parameters.}
QDAFN<> qdafn(dataset, 3, 10);

\textcolor{comment}{// Now retrain with different parameters.}
qdafn.Train(dataset, 10, 3);
\end{DoxyCode}
\subsection{Running on sparse data}\label{akfntutorial_cpp_ex5_qdafn_akfntut}
We can set the template parameter for {\ttfamily Q\+D\+A\+FN} to {\ttfamily arma\+::sp\+\_\+mat} in order to perform furthest neighbor search on sparse data. This code below creates a {\ttfamily Q\+D\+A\+FN} model using 20 tables and 60 projections with sparse input data, then searches for 3 approximate furthest neighbors.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/approx_kfn/qdafn.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::sp\_mat dataset;
\textcolor{comment}{// The query dataset.}
\textcolor{keyword}{extern} arma::sp\_mat querySet;

\textcolor{comment}{// Construct the model on sparse data.}
QDAFN<arma::sp_mat> qdafn(dataset, 20, 60);

\textcolor{comment}{// Search on query data.}
arma::Mat<size\_t> neighbors;
arma::mat distances;
qdafn.Search(querySet, 3, neighbors, distances);
\end{DoxyCode}
\section{K\+F\+N C++ class}\label{akfntutorial_cpp_ns_akfntut}
The extensive {\ttfamily Neighbor\+Search} class also provides a way to search for approximate furthest neighbors using a different, tree-\/based technique. For full documentation on this class, see the \doxyref{Neighbor\+Search tutorial}{p.}{nstutorial}. The {\ttfamily K\+FN} class is a convenient typedef of the {\ttfamily Neighbor\+Search} class that can be used to perform the furthest neighbors task with kd-\/trees.

In the following subsections, the {\ttfamily K\+FN} class is used in short code examples.\subsection{Simple furthest neighbors example}\label{akfntutorial_cpp_ex1_ns_akfntut}
The {\ttfamily K\+FN} class has construction semantics similar to {\ttfamily Drusilla\+Select} and {\ttfamily Q\+D\+A\+FN}. The example below constructs a {\ttfamily K\+FN} object (which will build the tree on the reference set), but note that the third parameter to the constructor allows us to specify our desired level of approximation. In this example we choose epsilon = 0.\+05. Then, the code searches for 3 approximate furthest neighbors.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/neighbor_search/neighbor_search.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference dataset.}
\textcolor{keyword}{extern} arma::mat dataset;
\textcolor{comment}{// The query set.}
\textcolor{keyword}{extern} arma::mat querySet;

\textcolor{comment}{// Construct the object, performing the default dual-tree search with}
\textcolor{comment}{// approximation level epsilon = 0.05.}
KFN kfn(dataset, KFN::DUAL_TREE_MODE, 0.05);

\textcolor{comment}{// Search for approximate furthest neighbors.}
arma::Mat<size\_t> neighbors;
arma::mat distances;
kfn.Search(querySet, 3, neighbors, distances);
\end{DoxyCode}
\subsection{Retraining on a new reference set}\label{akfntutorial_cpp_ex2_ns_akfntut}
Like the {\ttfamily Q\+D\+A\+FN} and {\ttfamily Drusilla\+Select} classes, the {\ttfamily K\+FN} class is capable of retraining on a new reference set. The code below demonstrates this.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/neighbor_search/neighbor_search.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The original reference set we train on.}
\textcolor{keyword}{extern} arma::mat dataset;
\textcolor{comment}{// The new reference set we retrain on.}
\textcolor{keyword}{extern} arma::mat newDataset;

\textcolor{comment}{// Construct the object with approximation level 0.1.}
KFN kfn(dataset, DUAL_TREE_MODE, 0.1);

\textcolor{comment}{// Retrain on the new reference set.}
kfn.Train(newDataset);
\end{DoxyCode}
\subsection{Searching in single-\/tree mode}\label{akfntutorial_cpp_ex3_ns_akfntut}
The particular mode to be used in search can be specified in the constructor. In this example, we use single-\/tree search (as opposed to the default of dual-\/tree search).


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/neighbor_search/neighbor_search.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference set.}
\textcolor{keyword}{extern} arma::mat dataset;
\textcolor{comment}{// The query set.}
\textcolor{keyword}{extern} arma::mat querySet;

\textcolor{comment}{// Construct the object with approximation level 0.25 and in single tree search}
\textcolor{comment}{// mode.}
KFN kfn(dataset, SINGLE_TREE_MODE, 0.25);

\textcolor{comment}{// Search for 5 approximate furthest neighbors.}
arma::Mat<size\_t> neighbors;
arma::mat distances;
kfn.Search(querySet, 5, neighbors, distances);
\end{DoxyCode}
\subsection{Searching in brute-\/force mode}\label{akfntutorial_cpp_ex4_ns_akfntut}
If desired, brute-\/force search (\char`\"{}naive search\char`\"{}) can be used to find the furthest neighbors; however, the result will not be approximate---it will be exact (since every possibility will be considered). The code below performs exact furthest neighbor search by using the {\ttfamily K\+FN} class in brute-\/force mode.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <mlpack/methods/neighbor_search/neighbor_search.hpp>}

\textcolor{keyword}{using namespace }mlpack::neighbor;

\textcolor{comment}{// The reference set.}
\textcolor{keyword}{extern} arma::mat dataset;
\textcolor{comment}{// The query set.}
\textcolor{keyword}{extern} arma::mat querySet;

\textcolor{comment}{// Construct the object in brute-force mode.  We can leave the approximation}
\textcolor{comment}{// parameter to its default (0) since brute-force will provide exact results.}
KFN kfn(dataset, NAIVE_MODE);

\textcolor{comment}{// Perform the search for 2 furthest neighbors.}
arma::Mat<size\_t> neighbors;
arma::mat distances;
kfn.Search(querySet, 2, neighbors, distances);
\end{DoxyCode}
\section{Further documentation}\label{akfntutorial_further_doc_akfntut}
For further documentation on the approximate furthest neighbor facilities offered by {\bfseries mlpack}, consult the following documentation\+:


\begin{DoxyItemize}
\item \doxyref{Neighbor\+Search tutorial (k-\/nearest-\/neighbors)}{p.}{nstutorial}
\item \doxyref{Q\+D\+A\+FN class documentation}{p.}{classmlpack_1_1neighbor_1_1QDAFN}
\item \doxyref{Drusilla\+Select class documentation}{p.}{classmlpack_1_1neighbor_1_1DrusillaSelect}
\item \doxyref{Neighbor\+Search class documentation}{p.}{classmlpack_1_1neighbor_1_1NeighborSearch} 
\end{DoxyItemize}