Files
mlpack/doc/html/metrics.html
T

123 lines
12 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>mlpack: The MetricType policy in mlpack</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="extra-stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">mlpack
&#160;<span id="projectnumber">master</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">The MetricType policy in mlpack </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>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 (city-block) 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.</p>
<p>mlpack algorithms, when possible, allow the use of an arbitrary metric via the use of the <code>MetricType</code> template parameter. Any metric passed as a <code>MetricType</code> template parameter will need to have</p>
<ul>
<li>an <code>Evaluate</code> function</li>
<li>a default constructor.</li>
</ul>
<p>The signature of the <code>Evaluate</code> function is straightforward:</p>
<div class="fragment"><div class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> VecTypeA, <span class="keyword">typename</span> VecTypeB&gt;</div><div class="line"><span class="keywordtype">double</span> Evaluate(<span class="keyword">const</span> VecTypeA&amp; a, <span class="keyword">const</span> VecTypeB&amp; b);</div></div><!-- fragment --><p>The function takes two vector arguments, <code>a</code> and <code>b</code>, and returns a <code>double</code> that is the evaluation of the metric between the two arguments. So, for a particular metric <img class="formulaInl" alt="$d(\cdot, \cdot)$" src="form_159.png"/>, the <code>Evaluate()</code> function should return <img class="formulaInl" alt="$d(a, b)$" src="form_160.png"/>.</p>
<p>The arguments <code>a</code> and <code>b</code>, of types <code>VecTypeA</code> and <code>VecTypeB</code>, respectively, will be an Armadillo-like vector type (usually <code>arma::vec</code>, <code>arma::sp_vec</code>, or similar). In general it should be valid to assume that <code>VecTypeA</code> is a class with the same API as <code>arma::vec</code>.</p>
<p>Note that for metrics that do not hold any state, the <code>Evaluate()</code> method can be marked as <code>static</code>.</p>
<p>Overall, the <code>MetricType</code> template policy is quite simple (much like the <a class="el" href="kernels.html">The KernelType policy in mlpack</a> KernelType policy). Below is an example metric class, which implements the L2 distance:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ExampleMetric</div><div class="line">{</div><div class="line"> <span class="comment">// Default constructor is required.</span></div><div class="line"> ExampleMetric() { }</div><div class="line"></div><div class="line"> <span class="comment">// The example metric holds no state, so we can mark Evaluate() as static.</span></div><div class="line"> <span class="keyword">template</span>&lt;<span class="keyword">typename</span> VecTypeA, <span class="keyword">typename</span> VecTypeB&gt;</div><div class="line"> <span class="keyword">static</span> <span class="keywordtype">double</span> Evaluate(<span class="keyword">const</span> VecTypeA&amp; a, <span class="keyword">const</span> VecTypeB&amp; b)</div><div class="line"> {</div><div class="line"> <span class="comment">// Return the L2 norm of the difference between the points, which is the</span></div><div class="line"> <span class="comment">// same as the L2 distance.</span></div><div class="line"> <span class="keywordflow">return</span> arma::norm(a - b);</div><div class="line"> }</div><div class="line">};</div></div><!-- fragment --><p>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 <code>ExampleKernel</code>, by instantiating a <code><a class="el" href="classmlpack_1_1range_1_1RangeSearch.html" title="The RangeSearch class is a template class for performing range searches. ">mlpack::range::RangeSearch</a></code> object that uses the <code>ExampleKernel</code>. Then, the number of results are printed. The <code>RangeSearch</code> class takes three template parameters: <code>MetricType</code>, <code>MatType</code>, and <code>TreeType</code>. (All three have defaults, so we will just leave <code>MatType</code> and <code>TreeType</code> to their defaults.)</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="core_8hpp.html">mlpack/core.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="range__search_8hpp.html">mlpack/methods/range_search/range_search.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;example_metric.hpp&quot;</span> <span class="comment">// A file that contains ExampleKernel.</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacemlpack.html">mlpack</a>;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacemlpack_1_1range.html">mlpack::range</a>;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacestd.html">std</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line"> <span class="comment">// Create a random dataset with 10 dimensions and 5000 points.</span></div><div class="line"> arma::mat data = arma::randu&lt;arma::mat&gt;(10, 5000);</div><div class="line"></div><div class="line"> <span class="comment">// Instantiate the RangeSearch object with the ExampleKernel.</span></div><div class="line"> <a class="code" href="classmlpack_1_1range_1_1RangeSearch.html">RangeSearch&lt;ExampleKernel&gt;</a> rs(data);</div><div class="line"></div><div class="line"> <span class="comment">// These vectors will store the results.</span></div><div class="line"> vector&lt;vector&lt;size_t&gt;&gt; neighbors;</div><div class="line"> vector&lt;vector&lt;double&gt;&gt; distances;</div><div class="line"></div><div class="line"> <span class="comment">// Create a random 10-dimensional query point.</span></div><div class="line"> arma::vec query = arma::randu&lt;arma::vec&gt;(10);</div><div class="line"></div><div class="line"> <span class="comment">// Find those points with distance (according to ExampleMetric) between 1 and</span></div><div class="line"> <span class="comment">// 2 from the query point.</span></div><div class="line"> rs.Search(query, <a class="code" href="namespacemlpack_1_1math.html#a927179bb8d2d0fb6164df385ab51cbce">math::Range</a>(1.0, 2.0), neighbors, distances);</div><div class="line"></div><div class="line"> <span class="comment">// Now, print the number of points inside the desired range. We know that</span></div><div class="line"> <span class="comment">// neighbors and distances will have length 1, since there was only one query</span></div><div class="line"> <span class="comment">// point.</span></div><div class="line"> cout &lt;&lt; neighbors[0].size() &lt;&lt; <span class="stringliteral">&quot; points within the range [1.0, 2.0] of the &quot;</span></div><div class="line"> &lt;&lt; <span class="stringliteral">&quot;query point!&quot;</span> &lt;&lt; endl;</div><div class="line">}</div></div><!-- fragment --><p>mlpack comes with a number of pre-written metrics that satisfy the <code>MetricType</code> policy:</p>
<ul>
<li><a class="el" href="namespacemlpack_1_1metric.html#aa206bc9eea1ad29826b0bf9890e69aaf" title="The Manhattan (L1) distance. ">mlpack::metric::ManhattanDistance</a></li>
<li><a class="el" href="namespacemlpack_1_1metric.html#a012695bde5dbf7ded7f7a180b5f3f512" title="The Euclidean (L2) distance. ">mlpack::metric::EuclideanDistance</a></li>
<li><a class="el" href="namespacemlpack_1_1metric.html#a112690bd2c4cba99c4c4e7ab2263155e" title="The L-infinity distance. ">mlpack::metric::ChebyshevDistance</a></li>
<li><a class="el" href="classmlpack_1_1metric_1_1MahalanobisDistance.html" title="The Mahalanobis distance, which is essentially a stretched Euclidean distance. ">mlpack::metric::MahalanobisDistance</a></li>
<li><a class="el" href="classmlpack_1_1metric_1_1LMetric.html" title="The L_p metric for arbitrary integer p, with an option to take the root. ">mlpack::metric::LMetric</a> (for arbitrary L-metrics)</li>
<li><a class="el" href="classmlpack_1_1metric_1_1IPMetric.html" title="The inner product metric, IPMetric, takes a given Mercer kernel (KernelType), and when Evaluate() is ...">mlpack::metric::IPMetric</a> (requires a <a class="el" href="kernels.html">The KernelType policy in mlpack</a> KernelType parameter) </li>
</ul>
</div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
<script type="text/javascript">
var x = document.getElementsByClassName("formulaDsp");
var i;
for (i = 0; i < x.length; i++)
{
x[i].width /= 4;
}
</script>
</html>