| <!-- HTML header for doxygen 1.8.4--> |
| <!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.10"/> |
| <meta name="keywords" content="madlib,postgres,greenplum,machine learning,data mining,deep learning,ensemble methods,data science,market basket analysis,affinity analysis,pca,lda,regression,elastic net,huber white,proportional hazards,k-means,latent dirichlet allocation,bayes,support vector machines,svm"/> |
| <title>MADlib: Norms and Distance functions</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="navtree.css" rel="stylesheet" type="text/css"/> |
| <script type="text/javascript" src="resize.js"></script> |
| <script type="text/javascript" src="navtreedata.js"></script> |
| <script type="text/javascript" src="navtree.js"></script> |
| <script type="text/javascript"> |
| $(document).ready(initResizable); |
| $(window).load(resizeHeight); |
| </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> |
| <script type="text/x-mathjax-config"> |
| MathJax.Hub.Config({ |
| extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"], |
| jax: ["input/TeX","output/HTML-CSS"], |
| }); |
| </script><script src="../mathjax/MathJax.js"></script> |
| <!-- hack in the navigation tree --> |
| <script type="text/javascript" src="navtree_hack.js"></script> |
| <link href="doxygen.css" rel="stylesheet" type="text/css" /> |
| <link href="madlib_extra.css" rel="stylesheet" type="text/css"/> |
| <!-- google analytics --> |
| <script> |
| (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
| (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
| m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
| })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); |
| ga('create', 'UA-45382226-1', 'auto'); |
| ga('send', 'pageview'); |
| </script> |
| </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="projectlogo"><a href="http://madlib.incubator.apache.org"><img alt="Logo" src="madlib.png" height="50" style="padding-left:0.5em;" border="0"/ ></a></td> |
| <td style="padding-left: 0.5em;"> |
| <div id="projectname"> |
| <span id="projectnumber">1.8</span> |
| </div> |
| <div id="projectbrief">User Documentation for MADlib</div> |
| </td> |
| <td> <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> |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| </div> |
| <!-- end header part --> |
| <!-- Generated by Doxygen 1.8.10 --> |
| <script type="text/javascript"> |
| var searchBox = new SearchBox("searchBox", "search",false,'Search'); |
| </script> |
| </div><!-- top --> |
| <div id="side-nav" class="ui-resizable side-nav-resizable"> |
| <div id="nav-tree"> |
| <div id="nav-tree-contents"> |
| <div id="nav-sync" class="sync"></div> |
| </div> |
| </div> |
| <div id="splitbar" style="-moz-user-select:none;" |
| class="ui-resizable-handle"> |
| </div> |
| </div> |
| <script type="text/javascript"> |
| $(document).ready(function(){initNavTree('group__grp__linalg.html','');}); |
| </script> |
| <div id="doc-content"> |
| <!-- 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 class="header"> |
| <div class="headertitle"> |
| <div class="title">Norms and Distance functions<div class="ingroups"><a class="el" href="group__grp__datatrans.html">Data Types and Transforms</a> » <a class="el" href="group__grp__arraysmatrix.html">Arrays and Matrices</a></div></div> </div> |
| </div><!--header--> |
| <div class="contents"> |
| <div class="toc"><b>Contents</b> </p><ul> |
| <li class="level1"> |
| <a href="#functions">Linear Algebra Utility Functions</a> </li> |
| <li class="level1"> |
| <a href="#examples">Examples</a> </li> |
| <li class="level1"> |
| <a href="#literature">Literature</a> </li> |
| <li class="level1"> |
| <a href="#related">Related Functions</a> </li> |
| </ul> |
| </div><p>The linalg module consists of useful utility functions for basic linear algebra operations. Several of these functions can be used while implementing new algorithms. These functions operate on vectors (1-D FLOAT8 array) and matrices (2-D FLOAT8 array). Note that other array types may need to be casted into FLOAT8[] before calling the functions.</p> |
| <p>Refer to the <a class="el" href="linalg_8sql__in.html" title="SQL functions for linear algebra. ">linalg.sql_in</a> file for documentation on each of the utility functions.</p> |
| <p><a class="anchor" id="functions"></a></p><dl class="section user"><dt>Linear Algebra Utility Functions</dt><dd><table class="output"> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#a300300fe4b8576ba0b97b95d8dea3057" title="1-norm of a vector ">norm1()</a> </th><td><p class="starttd">1-norm of a vector, \(\|\vec{a}\|_1\).</p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#a50fdfe30cc0edc6888a909dbb4b4c239" title="2-norm of a vector ">norm2()</a> </th><td><p class="starttd">2-norm of a vector, \(\|\vec{a}\|_2\). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#aad193850e79c4b9d811ca9bc53e13476" title="1-norm of the difference between two vectors ">dist_norm1()</a> </th><td><p class="starttd">1-norm of the difference between two vectors, \(\|\vec{a} - \vec{b}\|_1\). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#aa58e51526edea6ea98db30b6f250adb4" title="2-norm of the difference between two vectors ">dist_norm2()</a> </th><td><p class="starttd">2-norm of the difference between two vectors, \(\|\vec{a} - \vec{b}\|_2\). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#ad9cc156ae57bf7c0a2fe90798259105a" title="p-norm of the difference between two vectors ">dist_pnorm()</a> </th><td><p class="starttd">Generic p-norm of the difference between two vectors, \(\|\vec{a} - \vec{b}\|_p, p > 0\). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#a31fa9f2f5b45507c09f136464fdad1db" title="Infinity-norm of the difference between two vectors. ">dist_inf_norm()</a> </th><td><p class="starttd">Infinity-norm of the difference between two vectors, \(\|\vec{a} - \vec{b}\|_\infty\). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#a00a08e69f27524f2096032214e15b668" title="Squared 2-norm of the difference between two vectors. ">squared_dist_norm2()</a> </th><td><p class="starttd">Squared 2-norm of the difference between two vectors, \(\|\vec{a} - \vec{b}\|_2^2\). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#a1782f2ba00d9f9fab20894a576079f87" title="cosine similarity score between two vectors ">cosine_similarity()</a> </th><td><p class="starttd">Cosine score between two vectors, \(\frac{\vec{a} \cdot \vec{b}}{\|\vec{a}\|_2 \|\vec{b}\|_2}\). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#a8c7b9281a72ff22caf06161701b27e84" title="Angle between two vectors. ">dist_angle()</a> </th><td><p class="starttd">Angle between two vectors in an Euclidean space, \(\cos^{-1}(\frac{\vec{a} \cdot \vec{b}}{\|\vec{a}\|_2 \|\vec{b}\|_2})\). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#afa13b4c6122b99422d666dedea136c18" title="Tanimoto distance between two vectors. ">dist_tanimoto()</a> </th><td><p class="starttd">Tanimoto distance between two vectors. [1] </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#ac1397ac9f4a35b3b67c3be05b5e1a828" title="Jaccard distance between two vectors (treated as sets) ">dist_jaccard()</a> </th><td><p class="starttd">Jaccard distance between two varchar vectors treated as sets. </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#af6b905fcac7746ef0ed0c36df4a1e070" title="Get an indexed row of the given matrix (2-D array) ">get_row()</a> </th><td><p class="starttd">Return the indexed row of a matrix (2-D array). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#a20f34c9e661191e5225cca7bc23252c5" title="Get an indexed col of the given matrix (2-D array) ">get_col()</a> </th><td><p class="starttd">Return the indexed col of a matrix (2-D array). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#a1aa37f73fb1cd8d7d106aa518dd8c0b4" title="Compute the average of vectors. ">avg()</a> </th><td><p class="starttd">Compute the average of vectors. </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#a0b04663ca206f03e66aed5ea2b4cc461" title="Compute the normalized average of vectors. ">normalized_avg()</a> </th><td><p class="starttd">Compute the normalized average of vectors (unit vector in an Euclidean space). </p> |
| <p class="endtd"></p> |
| </td></tr> |
| <tr> |
| <th><a class="el" href="linalg_8sql__in.html#a9c439706f35d6cac89f151d553a5f111" title="Combine vectors to a matrix. ">matrix_agg()</a> </th><td><p class="starttd">Combine vectors to a matrix. </p> |
| <p class="endtd"></p> |
| </td></tr> |
| </table> |
| </dd></dl> |
| <p><a class="anchor" id="examples"></a></p> |
| <p><b>Vector Norms and Distances</b></p> |
| <ol type="1"> |
| <li>Create a database table with two vector columns and add some data. <pre class="example"> |
| CREATE TABLE two_vectors( |
| id integer, |
| a float8[], |
| b float8[]); |
| </pre> <pre class="example"> |
| INSERT INTO two_vectors VALUES |
| (1, '{3,4}', '{4,5}'), |
| (2, '{1,1,0,-4,5,3,4,106,14}', '{1,1,0,6,-3,1,2,92,2}'); |
| </pre></li> |
| <li>Invoke norm functions. <pre class="example"> |
| SELECT |
| id, |
| madlib.norm1(a), |
| madlib.norm2(a) |
| FROM two_vectors; |
| </pre> Result: <pre class="result"> |
| id | norm1 | norm2 |
| ----+-------+------------------ |
| 1 | 7 | 5 |
| 2 | 138 | 107.238052947636 |
| (2 rows) |
| </pre></li> |
| <li>Invoke distance functions. <pre class="example"> |
| SELECT |
| id, |
| madlib.dist_norm1(a, b), |
| madlib.dist_norm2(a, b), |
| madlib.dist_pnorm(a, b, 5) AS norm5, |
| madlib.dist_inf_norm(a, b), |
| madlib.squared_dist_norm2(a, b) AS sq_dist_norm2, |
| madlib.cosine_similarity(a, b), |
| madlib.dist_angle(a, b), |
| madlib.dist_tanimoto(a, b), |
| madlib.dist_jaccard(a::text[], b::text[]) |
| FROM two_vectors; |
| </pre> Result: <pre class="result"> |
| id | dist_norm1 | dist_norm2 | norm5 | dist_inf_norm | sq_dist_norm2 | cosine_similarity | dist_angle | dist_tanimoto | dist_jaccard |
| ----+------------+------------------+------------------+---------------+---------------+-------------------+--------------------+--------------------+------------------- |
| 1 | 2 | 1.4142135623731 | 1.14869835499704 | 1 | 2 | 0.999512076087079 | 0.0312398334302684 | 0.0588235294117647 | 0.666666666666667 |
| 2 | 48 | 22.6274169979695 | 15.585086360695 | 14 | 512 | 0.985403348449008 | 0.17106899659286 | 0.0498733684005455 | 0.833333333333333 |
| (2 rows) |
| </pre></li> |
| </ol> |
| <p><b>Matrix Functions</b></p> |
| <ol type="1"> |
| <li>Create a database table with a matrix column. <pre class="example"> |
| CREATE TABLE matrix( |
| id integer, |
| m float8[]); |
| </pre> <pre class="example"> |
| INSERT INTO matrix VALUES |
| (1, '{{4,5},{3,5},{9,0}}'); |
| </pre></li> |
| <li>Invoke matrix functions. <pre class="example"> |
| SELECT |
| madlib.get_row(m, 1) AS row_1, |
| madlib.get_row(m, 2) AS row_2, |
| madlib.get_row(m, 3) AS row_3, |
| madlib.get_col(m, 1) AS col_1, |
| madlib.get_col(m, 2) AS col_2 |
| FROM matrix; |
| </pre> Result: <pre class="result"> |
| row_1 | row_2 | row_3 | col_1 | col_2 |
| -------+-------+-------+---------+--------- |
| {4,5} | {3,5} | {9,0} | {4,3,9} | {5,5,0} |
| (1 row) |
| </pre></li> |
| </ol> |
| <p><b>Aggregate Functions</b></p> |
| <ol type="1"> |
| <li>Create a database table with a vector column. <pre class="example"> |
| CREATE TABLE vector( |
| id integer, |
| v float8[]); |
| </pre> <pre class="example"> |
| INSERT INTO vector VALUES |
| (1, '{4,3}'), |
| (2, '{8,6}'), |
| (3, '{12,9}'); |
| </pre></li> |
| <li>Invoke aggregate functions. <pre class="example"> |
| SELECT |
| madlib.avg(v), |
| madlib.normalized_avg(v), |
| madlib.matrix_agg(v) |
| FROM vector; |
| </pre> Result: <pre class="result"> |
| avg | normalized_avg | matrix_agg |
| -------+----------------+---------------------- |
| {8,6} | {0.8,0.6} | {{4,3},{8,6},{12,9}} |
| (1 row) |
| </pre></li> |
| </ol> |
| <p><a class="anchor" id="literature"></a></p><dl class="section user"><dt>Literature</dt><dd></dd></dl> |
| <p>[1] <a href="http://en.wikipedia.org/wiki/Jaccard_index#Tanimoto_similarity_and_distance">http://en.wikipedia.org/wiki/Jaccard_index#Tanimoto_similarity_and_distance</a></p> |
| <p><a class="anchor" id="related"></a></p><dl class="section user"><dt>Related Topics</dt><dd>File <a class="el" href="linalg_8sql__in.html" title="SQL functions for linear algebra. ">linalg.sql_in</a> documenting the SQL functions. </dd></dl> |
| </div><!-- contents --> |
| </div><!-- doc-content --> |
| <!-- start footer part --> |
| <div id="nav-path" class="navpath"><!-- id is needed for treeview function! --> |
| <ul> |
| <li class="footer">Generated on Mon Jul 27 2015 20:37:45 for MADlib by |
| <a href="http://www.doxygen.org/index.html"> |
| <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.10 </li> |
| </ul> |
| </div> |
| </body> |
| </html> |