mlpack
2.2.1

Many machine learning methods operate with some sort of metric, and often, this metric can be any arbitrary metric. For instance, consider the problem of nearest neighbor search; one can find the nearest neighbor of a point with respect to the standard Euclidean distance, or the Manhattan (cityblock) distance. The actual search techniques, though, remain the same. And this is true of many machine learning methods: the specific metric that is used can be any valid metric.
mlpack algorithms, when possible, allow the use of an arbitrary metric via the use of the MetricType
template parameter. Any metric passed as a MetricType
template parameter will need to have
 an
Evaluate
function  a default constructor.
The signature of the Evaluate
function is straightforward:
The function takes two vector arguments, a
and b
, and returns a double
that is the evaluation of the metric between the two arguments. So, for a particular metric , the Evaluate()
function should return .
The arguments a
and b
, of types VecTypeA
and VecTypeB
, respectively, will be an Armadillolike vector type (usually arma::vec
, arma::sp_vec
, or similar). In general it should be valid to assume that VecTypeA
is a class with the same API as arma::vec
.
Note that for metrics that do not hold any state, the Evaluate()
method can be marked as static
.
Overall, the MetricType
template policy is quite simple (much like the The KernelType policy in mlpack KernelType policy). Below is an example metric class, which implements the L2 distance:
Then, this metric can easily be used inside of other mlpack algorithms. For example, the code below runs range search on a random dataset with the ExampleKernel
, by instantiating a mlpack::range::RangeSearch
object that uses the ExampleKernel
. Then, the number of results are printed. The RangeSearch
class takes three template parameters: MetricType
, MatType
, and TreeType
. (All three have defaults, so we will just leave MatType
and TreeType
to their defaults.)
mlpack comes with a number of prewritten metrics that satisfy the MetricType
policy:
 mlpack::metric::ManhattanDistance
 mlpack::metric::EuclideanDistance
 mlpack::metric::ChebyshevDistance
 mlpack::metric::MahalanobisDistance
 mlpack::metric::LMetric (for arbitrary Lmetrics)
 mlpack::metric::IPMetric (requires a The KernelType policy in mlpack KernelType parameter)
Generated by 1.8.13