TheAlgorithms-C/d6/d76/k__means__clustering_8c.html
2020-08-08 14:22:01 +00:00

225 lines
17 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://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.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: machine_learning/k_means_clustering.c File Reference</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>
<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/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.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">Algorithms_in_C
&#160;<span id="projectnumber">1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</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">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('d6/d76/k__means__clustering_8c.html','../../'); initResizable(); });
/* @license-end */
</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="summary">
<a href="#nested-classes">Data Structures</a> &#124;
<a href="#define-members">Macros</a> &#124;
<a href="#typedef-members">Typedefs</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle">
<div class="title">k_means_clustering.c File Reference</div> </div>
</div><!--header-->
<div class="contents">
<p>K Means Clustering Algorithm implemented.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;float.h&gt;</code><br />
<code>#include &lt;math.h&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;string.h&gt;</code><br />
<code>#include &lt;time.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for k_means_clustering.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d5/df1/k__means__clustering_8c__incl.svg" width="504" height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="nested-classes"></a>
Data Structures</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d5e/structobservation.html">observation</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d99/structcluster.html">cluster</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:a525335710b53cb064ca56b936120431e"><td class="memItemLeft" align="right" valign="top"><a id="a525335710b53cb064ca56b936120431e"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>_USE_MATH_DEFINES</b>&#160;&#160;&#160;/* required for MS Visual C */</td></tr>
<tr class="separator:a525335710b53cb064ca56b936120431e"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="typedef-members"></a>
Typedefs</h2></td></tr>
<tr class="memitem:ga8cc0f5e9645e6faf40d1746acf777944"><td class="memItemLeft" align="right" valign="top">
typedef struct <a class="el" href="../../d1/d5e/structobservation.html">observation</a>&#160;</td><td class="memItemRight" valign="bottom"><b>observation</b></td></tr>
<tr class="separator:ga8cc0f5e9645e6faf40d1746acf777944"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gabe256d797c00b793041eac33f07804c1"><td class="memItemLeft" align="right" valign="top">
typedef struct <a class="el" href="../../d1/d99/structcluster.html">cluster</a>&#160;</td><td class="memItemRight" valign="bottom"><b>cluster</b></td></tr>
<tr class="separator:gabe256d797c00b793041eac33f07804c1"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:gad339c41d3ee9e6729aca9e9ab3f7d2d9"><td class="memItemLeft" align="right" valign="top">
int&#160;</td><td class="memItemRight" valign="bottom"><b>calculateNearst</b> (<a class="el" href="../../d1/d5e/structobservation.html">observation</a> *o, <a class="el" href="../../d1/d99/structcluster.html">cluster</a> clusters[], int k)</td></tr>
<tr class="separator:gad339c41d3ee9e6729aca9e9ab3f7d2d9"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gadee39a3f17bf5144df5592e48dbfc9f7"><td class="memItemLeft" align="right" valign="top">
void&#160;</td><td class="memItemRight" valign="bottom"><b>calculateCentroid</b> (<a class="el" href="../../d1/d5e/structobservation.html">observation</a> observations[], size_t size, <a class="el" href="../../d1/d99/structcluster.html">cluster</a> *centroid)</td></tr>
<tr class="separator:gadee39a3f17bf5144df5592e48dbfc9f7"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gad229b1dc406cb5ea510f26a373ed8bfd"><td class="memItemLeft" align="right" valign="top">
<a class="el" href="../../d1/d99/structcluster.html">cluster</a> *&#160;</td><td class="memItemRight" valign="bottom"><b>kMeans</b> (<a class="el" href="../../d1/d5e/structobservation.html">observation</a> observations[], size_t size, int k)</td></tr>
<tr class="separator:gad229b1dc406cb5ea510f26a373ed8bfd"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a5b4ba704e02672e59cfa35f82e3db28a"><td class="memItemLeft" align="right" valign="top"><a id="a5b4ba704e02672e59cfa35f82e3db28a"></a>
void&#160;</td><td class="memItemRight" valign="bottom"><b>printEPS</b> (<a class="el" href="../../d1/d5e/structobservation.html">observation</a> pts[], size_t len, <a class="el" href="../../d1/d99/structcluster.html">cluster</a> cent[], int k)</td></tr>
<tr class="separator:a5b4ba704e02672e59cfa35f82e3db28a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top"><a id="aa8dca7b867074164d5f45b0f3851269d"></a>
static void&#160;</td><td class="memItemRight" valign="bottom"><b>test</b> ()</td></tr>
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ac1e2d86a33f7b563e1d5e317974d9074"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d76/k__means__clustering_8c.html#ac1e2d86a33f7b563e1d5e317974d9074">test2</a> ()</td></tr>
<tr class="separator:ac1e2d86a33f7b563e1d5e317974d9074"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top"><a id="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
int&#160;</td><td class="memItemRight" valign="bottom"><b>main</b> ()</td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>K Means Clustering Algorithm implemented. </p>
<p>This file has K Means algorithm implemmented It prints test output in eps format</p>
<p>Note: Though the code for clustering works for all the 2D data points and can be extended for any size vector by making the required changes, but note that the output method i.e. printEPS is only good for polar data points i.e. in a circle and both test use the same. </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/Lakhan-Nad">Lakhan Nad</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ac1e2d86a33f7b563e1d5e317974d9074"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ac1e2d86a33f7b563e1d5e317974d9074">&#9670;&nbsp;</a></span>test2()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">test2 </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>A function to test the kMeans function Generates 1000000 points in a circle of radius 20.0 with center at (0,0) and cluster them into 11 clusters</p>
<p><img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/machine_learning/k_means_clustering/kMeansTest2.png" alt="Output for 1000000 points divided in 11 clusters" width="400px" heiggt="400px" class="inline"/> </p>
<div class="fragment"><div class="line"><a name="l00305"></a><span class="lineno"> 305</span>&#160; {</div>
<div class="line"><a name="l00306"></a><span class="lineno"> 306</span>&#160; <span class="keywordtype">size_t</span> size = 1000000<a class="code" href="../../df/db3/struct_l.html">L</a>;</div>
<div class="line"><a name="l00307"></a><span class="lineno"> 307</span>&#160; <a class="code" href="../../d1/d5e/structobservation.html">observation</a>* observations = (<a class="code" href="../../d1/d5e/structobservation.html">observation</a>*)malloc(<span class="keyword">sizeof</span>(<a class="code" href="../../d1/d5e/structobservation.html">observation</a>) * size);</div>
<div class="line"><a name="l00308"></a><span class="lineno"> 308</span>&#160; <span class="keywordtype">double</span> maxRadius = 20.00;</div>
<div class="line"><a name="l00309"></a><span class="lineno"> 309</span>&#160; <span class="keywordtype">double</span> radius = 0;</div>
<div class="line"><a name="l00310"></a><span class="lineno"> 310</span>&#160; <span class="keywordtype">double</span> ang = 0;</div>
<div class="line"><a name="l00311"></a><span class="lineno"> 311</span>&#160; <span class="keywordtype">size_t</span> i = 0;</div>
<div class="line"><a name="l00312"></a><span class="lineno"> 312</span>&#160; <span class="keywordflow">for</span> (; i &lt; size; i++) {</div>
<div class="line"><a name="l00313"></a><span class="lineno"> 313</span>&#160; radius = maxRadius * ((double)rand() / RAND_MAX);</div>
<div class="line"><a name="l00314"></a><span class="lineno"> 314</span>&#160; ang = 2 * M_PI * ((double)rand() / RAND_MAX);</div>
<div class="line"><a name="l00315"></a><span class="lineno"> 315</span>&#160; observations[i].<a class="code" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a> = radius * cos(ang);</div>
<div class="line"><a name="l00316"></a><span class="lineno"> 316</span>&#160; observations[i].<a class="code" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a> = radius * sin(ang);</div>
<div class="line"><a name="l00317"></a><span class="lineno"> 317</span>&#160; }</div>
<div class="line"><a name="l00318"></a><span class="lineno"> 318</span>&#160; <span class="keywordtype">int</span> k = 11; <span class="comment">// No of clusters</span></div>
<div class="line"><a name="l00319"></a><span class="lineno"> 319</span>&#160; <a class="code" href="../../d1/d99/structcluster.html">cluster</a>* clusters = kMeans(observations, size, k);</div>
<div class="line"><a name="l00320"></a><span class="lineno"> 320</span>&#160; printEPS(observations, size, clusters, k);</div>
<div class="line"><a name="l00321"></a><span class="lineno"> 321</span>&#160; <span class="comment">// Free the accquired memory</span></div>
<div class="line"><a name="l00322"></a><span class="lineno"> 322</span>&#160; free(observations);</div>
<div class="line"><a name="l00323"></a><span class="lineno"> 323</span>&#160; free(clusters);</div>
<div class="line"><a name="l00324"></a><span class="lineno"> 324</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<div class="ttc" id="astructobservation_html"><div class="ttname"><a href="../../d1/d5e/structobservation.html">observation</a></div><div class="ttdef"><b>Definition:</b> k_means_clustering.c:38</div></div>
<div class="ttc" id="astruct_l_html"><div class="ttname"><a href="../../df/db3/struct_l.html">L</a></div><div class="ttdef"><b>Definition:</b> list.h:8</div></div>
<div class="ttc" id="astructobservation_html_a04f3dcfd59dd91353395e35c9831fade"><div class="ttname"><a href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">observation::x</a></div><div class="ttdeci">double x</div><div class="ttdoc">abscissa of 2D data point</div><div class="ttdef"><b>Definition:</b> k_means_clustering.c:39</div></div>
<div class="ttc" id="astructcluster_html"><div class="ttname"><a href="../../d1/d99/structcluster.html">cluster</a></div><div class="ttdef"><b>Definition:</b> k_means_clustering.c:51</div></div>
<div class="ttc" id="astructobservation_html_ab6be1fa7024b2d5f3a30d6c6b70efdd7"><div class="ttname"><a href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">observation::y</a></div><div class="ttdeci">double y</div><div class="ttdoc">ordinate of 2D data point</div><div class="ttdef"><b>Definition:</b> k_means_clustering.c:40</div></div>
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_3343723ae086de42ee4ca9774da3a13f.html">machine_learning</a></li><li class="navelem"><a class="el" href="../../d6/d76/k__means__clustering_8c.html">k_means_clustering.c</a></li>
<li class="footer">Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
</ul>
</div>
</body>
</html>