TheAlgorithms-C/d6/d76/k__means__clustering_8c.html

445 lines
36 KiB
HTML
Raw Normal View History

<!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" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.6"/>
<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 id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C<span id="projectnumber">&#160;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.9.6 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @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:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(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:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(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">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</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="124"><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 id="nested-classes" 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 id="define-members" name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:a525335710b53cb064ca56b936120431e"><td class="memItemLeft" align="right" valign="top"><a id="a525335710b53cb064ca56b936120431e" name="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 id="typedef-members" 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 id="func-members" 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"><a class="el" href="../../d8/d71/group__k__means.html#gad339c41d3ee9e6729aca9e9ab3f7d2d9">calculateNearst</a> (<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"><a class="el" href="../../d8/d71/group__k__means.html#gadee39a3f17bf5144df5592e48dbfc9f7">calculateCentroid</a> (<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:gaf6259432561e794dea0e060f482d15e2"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../d1/d99/structcluster.html">cluster</a> *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d71/group__k__means.html#gaf6259432561e794dea0e060f482d15e2">kMeans</a> (<a class="el" href="../../d1/d5e/structobservation.html">observation</a> observations[], size_t size, int k)</td></tr>
<tr class="separator:gaf6259432561e794dea0e060f482d15e2"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a5b4ba704e02672e59cfa35f82e3db28a"><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#a5b4ba704e02672e59cfa35f82e3db28a">printEPS</a> (<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">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d76/k__means__clustering_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0283886819c7c140a023582b7269e2d0"><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#a0283886819c7c140a023582b7269e2d0">test2</a> ()</td></tr>
<tr class="separator:a0283886819c7c140a023582b7269e2d0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d76/k__means__clustering_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</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" target="_blank">Lakhan Nad</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>This function calls the test function </p>
<div class="fragment"><div class="line"><span class="lineno"> 385</span>{</div>
<div class="line"><span class="lineno"> 386</span> srand(time(NULL));</div>
<div class="line"><span class="lineno"> 387</span> <a class="code hl_function" href="../../d6/d76/k__means__clustering_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a>();</div>
<div class="line"><span class="lineno"> 388</span> <span class="comment">/* test2(); */</span></div>
<div class="line"><span class="lineno"> 389</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 390</span>}</div>
<div class="ttc" id="ak__means__clustering_8c_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../d6/d76/k__means__clustering_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdef"><b>Definition:</b> k_means_clustering.c:321</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d6/d76/k__means__clustering_8c_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="447" height="110"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a5b4ba704e02672e59cfa35f82e3db28a" name="a5b4ba704e02672e59cfa35f82e3db28a"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a5b4ba704e02672e59cfa35f82e3db28a">&#9670;&#160;</a></span>printEPS()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void printEPS </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d1/d5e/structobservation.html">observation</a>&#160;</td>
<td class="paramname"><em>pts</em>[], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>len</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="el" href="../../d1/d99/structcluster.html">cluster</a>&#160;</td>
<td class="paramname"><em>cent</em>[], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>k</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>A function to print observations and clusters The code is taken from <a href="http://rosettacode.org/wiki/K-means%2B%2B_clustering">http://rosettacode.org/wiki/K-means%2B%2B_clustering</a>. Even the K Means code is also inspired from it</p>
<dl class="section note"><dt>Note</dt><dd>To print in a file use pipeline operator <div class="fragment"><div class="line">./k_means_clustering &gt; image.eps</div>
</div><!-- fragment --></dd></dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">observations</td><td>observations array </td></tr>
<tr><td class="paramname">len</td><td>size of observation array </td></tr>
<tr><td class="paramname">cent</td><td>clusters centroid's array </td></tr>
<tr><td class="paramname">k</td><td>size of cent array </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 238</span>{</div>
<div class="line"><span class="lineno"> 239</span> <span class="keywordtype">int</span> W = 400, H = 400;</div>
<div class="line"><span class="lineno"> 240</span> <span class="keywordtype">double</span> min_x = DBL_MAX, max_x = DBL_MIN, min_y = DBL_MAX, max_y = DBL_MIN;</div>
<div class="line"><span class="lineno"> 241</span> <span class="keywordtype">double</span> scale = 0, cx = 0, cy = 0;</div>
<div class="line"><span class="lineno"> 242</span> <span class="keywordtype">double</span>* colors = (<span class="keywordtype">double</span>*)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>) * (k * 3));</div>
<div class="line"><span class="lineno"> 243</span> <span class="keywordtype">int</span> i;</div>
<div class="line"><span class="lineno"> 244</span> <span class="keywordtype">size_t</span> j;</div>
<div class="line"><span class="lineno"> 245</span> <span class="keywordtype">double</span> kd = k * 1.0;</div>
<div class="line"><span class="lineno"> 246</span> <span class="keywordflow">for</span> (i = 0; i &lt; k; i++)</div>
<div class="line"><span class="lineno"> 247</span> {</div>
<div class="line"><span class="lineno"> 248</span> *(colors + 3 * i) = (3 * (i + 1) % k) / kd;</div>
<div class="line"><span class="lineno"> 249</span> *(colors + 3 * i + 1) = (7 * i % k) / kd;</div>
<div class="line"><span class="lineno"> 250</span> *(colors + 3 * i + 2) = (9 * i % k) / kd;</div>
<div class="line"><span class="lineno"> 251</span> }</div>
<div class="line"><span class="lineno"> 252</span> </div>
<div class="line"><span class="lineno"> 253</span> <span class="keywordflow">for</span> (j = 0; j &lt; len; j++)</div>
<div class="line"><span class="lineno"> 254</span> {</div>
<div class="line"><span class="lineno"> 255</span> <span class="keywordflow">if</span> (max_x &lt; pts[j].x)</div>
<div class="line"><span class="lineno"> 256</span> {</div>
<div class="line"><span class="lineno"> 257</span> max_x = pts[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>;</div>
<div class="line"><span class="lineno"> 258</span> }</div>
<div class="line"><span class="lineno"> 259</span> <span class="keywordflow">if</span> (min_x &gt; pts[j].x)</div>
<div class="line"><span class="lineno"> 260</span> {</div>
<div class="line"><span class="lineno"> 261</span> min_x = pts[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>;</div>
<div class="line"><span class="lineno"> 262</span> }</div>
<div class="line"><span class="lineno"> 263</span> <span class="keywordflow">if</span> (max_y &lt; pts[j].y)</div>
<div class="line"><span class="lineno"> 264</span> {</div>
<div class="line"><span class="lineno"> 265</span> max_y = pts[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>;</div>
<div class="line"><span class="lineno"> 266</span> }</div>
<div class="line"><span class="lineno"> 267</span> <span class="keywordflow">if</span> (min_y &gt; pts[j].y)</div>
<div class="line"><span class="lineno"> 268</span> {</div>
<div class="line"><span class="lineno"> 269</span> min_y = pts[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>;</div>
<div class="line"><span class="lineno"> 270</span> }</div>
<div class="line"><span class="lineno"> 271</span> }</div>
<div class="line"><span class="lineno"> 272</span> scale = W / (max_x - min_x);</div>
<div class="line"><span class="lineno"> 273</span> <span class="keywordflow">if</span> (scale &gt; (H / (max_y - min_y)))</div>
<div class="line"><span class="lineno"> 274</span> {</div>
<div class="line"><span class="lineno"> 275</span> scale = H / (max_y - min_y);</div>
<div class="line"><span class="lineno"> 276</span> };</div>
<div class="line"><span class="lineno"> 277</span> cx = (max_x + min_x) / 2;</div>
<div class="line"><span class="lineno"> 278</span> cy = (max_y + min_y) / 2;</div>
<div class="line"><span class="lineno"> 279</span> </div>
<div class="line"><span class="lineno"> 280</span> printf(<span class="stringliteral">&quot;%%!PS-Adobe-3.0 EPSF-3.0\n%%%%BoundingBox: -5 -5 %d %d\n&quot;</span>, W + 10,</div>
<div class="line"><span class="lineno"> 281</span> H + 10);</div>
<div class="line"><span class="lineno"> 282</span> printf(</div>
<div class="line"><span class="lineno"> 283</span> <span class="stringliteral">&quot;/l {rlineto} def /m {rmoveto} def\n&quot;</span></div>
<div class="line"><span class="lineno"> 284</span> <span class="stringliteral">&quot;/c { .25 sub exch .25 sub exch .5 0 360 arc fill } def\n&quot;</span></div>
<div class="line"><span class="lineno"> 285</span> <span class="stringliteral">&quot;/s { moveto -2 0 m 2 2 l 2 -2 l -2 -2 l closepath &quot;</span></div>
<div class="line"><span class="lineno"> 286</span> <span class="stringliteral">&quot; gsave 1 setgray fill grestore gsave 3 setlinewidth&quot;</span></div>
<div class="line"><span class="lineno"> 287</span> <span class="stringliteral">&quot; 1 setgray stroke grestore 0 setgray stroke }def\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 288</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; k; i++)</div>
<div class="line"><span class="lineno"> 289</span> {</div>
<div class="line"><span class="lineno"> 290</span> printf(<span class="stringliteral">&quot;%g %g %g setrgbcolor\n&quot;</span>, *(colors + 3 * i),</div>
<div class="line"><span class="lineno"> 291</span> *(colors + 3 * i + 1), *(colors + 3 * i + 2));</div>
<div class="line"><span class="lineno"> 292</span> <span class="keywordflow">for</span> (j = 0; j &lt; len; j++)</div>
<div class="line"><span class="lineno"> 293</span> {</div>
<div class="line"><span class="lineno"> 294</span> <span class="keywordflow">if</span> (pts[j].group != i)</div>
<div class="line"><span class="lineno"> 295</span> {</div>
<div class="line"><span class="lineno"> 296</span> <span class="keywordflow">continue</span>;</div>
<div class="line"><span class="lineno"> 297</span> }</div>
<div class="line"><span class="lineno"> 298</span> printf(<span class="stringliteral">&quot;%.3f %.3f c\n&quot;</span>, (pts[j].x - cx) * scale + W / 2,</div>
<div class="line"><span class="lineno"> 299</span> (pts[j].y - cy) * scale + H / 2);</div>
<div class="line"><span class="lineno"> 300</span> }</div>
<div class="line"><span class="lineno"> 301</span> printf(<span class="stringliteral">&quot;\n0 setgray %g %g s\n&quot;</span>, (cent[i].x - cx) * scale + W / 2,</div>
<div class="line"><span class="lineno"> 302</span> (cent[i].y - cy) * scale + H / 2);</div>
<div class="line"><span class="lineno"> 303</span> }</div>
<div class="line"><span class="lineno"> 304</span> printf(<span class="stringliteral">&quot;\n%%%%EOF&quot;</span>);</div>
<div class="line"><span class="lineno"> 305</span> </div>
<div class="line"><span class="lineno"> 306</span> <span class="comment">// free accquired memory</span></div>
<div class="line"><span class="lineno"> 307</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(colors);</div>
<div class="line"><span class="lineno"> 308</span>}</div>
<div class="ttc" id="amalloc__dbg_8h_html_a725f50ecaf1959d96de79b36b4788fee"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a></div><div class="ttdeci">#define malloc(bytes)</div><div class="ttdoc">This macro replace the standard malloc function with malloc_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:18</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><div class="ttdeci">#define free(ptr)</div><div class="ttdoc">This macro replace the standard free function with free_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:26</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:40</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:41</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="aa8dca7b867074164d5f45b0f3851269d" name="aa8dca7b867074164d5f45b0f3851269d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">&#9670;&#160;</a></span>test()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">static void test </td>
<td>(</td>
<td class="paramtype">void&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>A function to test the kMeans function Generates 100000 points in a circle of radius 20.0 with center at (0,0) and cluster them into 5 clusters</p>
<p><img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/machine_learning/k_means_clustering/kMeansTest1.png" alt="Output for 100000 points divided in 5 clusters" width="400px" heiggt="400px" class="inline"/> </p><dl class="section return"><dt>Returns</dt><dd>None </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 322</span>{</div>
<div class="line"><span class="lineno"> 323</span> <span class="keywordtype">size_t</span> size = 100000L;</div>
<div class="line"><span class="lineno"> 324</span> <a class="code hl_struct" href="../../d1/d5e/structobservation.html">observation</a>* observations =</div>
<div class="line"><span class="lineno"> 325</span> (<a class="code hl_struct" href="../../d1/d5e/structobservation.html">observation</a>*)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<a class="code hl_struct" href="../../d1/d5e/structobservation.html">observation</a>) * size);</div>
<div class="line"><span class="lineno"> 326</span> <span class="keywordtype">double</span> maxRadius = 20.00;</div>
<div class="line"><span class="lineno"> 327</span> <span class="keywordtype">double</span> radius = 0;</div>
<div class="line"><span class="lineno"> 328</span> <span class="keywordtype">double</span> ang = 0;</div>
<div class="line"><span class="lineno"> 329</span> <span class="keywordtype">size_t</span> i = 0;</div>
<div class="line"><span class="lineno"> 330</span> <span class="keywordflow">for</span> (; i &lt; size; i++)</div>
<div class="line"><span class="lineno"> 331</span> {</div>
<div class="line"><span class="lineno"> 332</span> radius = maxRadius * ((double)rand() / RAND_MAX);</div>
<div class="line"><span class="lineno"> 333</span> ang = 2 * M_PI * ((double)rand() / RAND_MAX);</div>
<div class="line"><span class="lineno"> 334</span> observations[i].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a> = radius * cos(ang);</div>
<div class="line"><span class="lineno"> 335</span> observations[i].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a> = radius * sin(ang);</div>
<div class="line"><span class="lineno"> 336</span> }</div>
<div class="line"><span class="lineno"> 337</span> <span class="keywordtype">int</span> k = 5; <span class="comment">// No of clusters</span></div>
<div class="line"><span class="lineno"> 338</span> <a class="code hl_struct" href="../../d1/d99/structcluster.html">cluster</a>* clusters = <a class="code hl_function" href="../../d8/d71/group__k__means.html#gaf6259432561e794dea0e060f482d15e2">kMeans</a>(observations, size, k);</div>
<div class="line"><span class="lineno"> 339</span> <a class="code hl_function" href="../../d6/d76/k__means__clustering_8c.html#a5b4ba704e02672e59cfa35f82e3db28a">printEPS</a>(observations, size, clusters, k);</div>
<div class="line"><span class="lineno"> 340</span> <span class="comment">// Free the accquired memory</span></div>
<div class="line"><span class="lineno"> 341</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(observations);</div>
<div class="line"><span class="lineno"> 342</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(clusters);</div>
<div class="line"><span class="lineno"> 343</span>}</div>
<div class="ttc" id="agroup__k__means_html_gaf6259432561e794dea0e060f482d15e2"><div class="ttname"><a href="../../d8/d71/group__k__means.html#gaf6259432561e794dea0e060f482d15e2">kMeans</a></div><div class="ttdeci">cluster * kMeans(observation observations[], size_t size, int k)</div><div class="ttdef"><b>Definition:</b> k_means_clustering.c:134</div></div>
<div class="ttc" id="ak__means__clustering_8c_html_a5b4ba704e02672e59cfa35f82e3db28a"><div class="ttname"><a href="../../d6/d76/k__means__clustering_8c.html#a5b4ba704e02672e59cfa35f82e3db28a">printEPS</a></div><div class="ttdeci">void printEPS(observation pts[], size_t len, cluster cent[], int k)</div><div class="ttdef"><b>Definition:</b> k_means_clustering.c:237</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:53</div></div>
<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:39</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d6/d76/k__means__clustering_8c_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="348" height="110"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a0283886819c7c140a023582b7269e2d0" name="a0283886819c7c140a023582b7269e2d0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0283886819c7c140a023582b7269e2d0">&#9670;&#160;</a></span>test2()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void 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><dl class="section return"><dt>Returns</dt><dd>None </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 357</span>{</div>
<div class="line"><span class="lineno"> 358</span> <span class="keywordtype">size_t</span> size = 1000000L;</div>
<div class="line"><span class="lineno"> 359</span> <a class="code hl_struct" href="../../d1/d5e/structobservation.html">observation</a>* observations =</div>
<div class="line"><span class="lineno"> 360</span> (<a class="code hl_struct" href="../../d1/d5e/structobservation.html">observation</a>*)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<a class="code hl_struct" href="../../d1/d5e/structobservation.html">observation</a>) * size);</div>
<div class="line"><span class="lineno"> 361</span> <span class="keywordtype">double</span> maxRadius = 20.00;</div>
<div class="line"><span class="lineno"> 362</span> <span class="keywordtype">double</span> radius = 0;</div>
<div class="line"><span class="lineno"> 363</span> <span class="keywordtype">double</span> ang = 0;</div>
<div class="line"><span class="lineno"> 364</span> <span class="keywordtype">size_t</span> i = 0;</div>
<div class="line"><span class="lineno"> 365</span> <span class="keywordflow">for</span> (; i &lt; size; i++)</div>
<div class="line"><span class="lineno"> 366</span> {</div>
<div class="line"><span class="lineno"> 367</span> radius = maxRadius * ((double)rand() / RAND_MAX);</div>
<div class="line"><span class="lineno"> 368</span> ang = 2 * M_PI * ((double)rand() / RAND_MAX);</div>
<div class="line"><span class="lineno"> 369</span> observations[i].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a> = radius * cos(ang);</div>
<div class="line"><span class="lineno"> 370</span> observations[i].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a> = radius * sin(ang);</div>
<div class="line"><span class="lineno"> 371</span> }</div>
<div class="line"><span class="lineno"> 372</span> <span class="keywordtype">int</span> k = 11; <span class="comment">// No of clusters</span></div>
<div class="line"><span class="lineno"> 373</span> <a class="code hl_struct" href="../../d1/d99/structcluster.html">cluster</a>* clusters = <a class="code hl_function" href="../../d8/d71/group__k__means.html#gaf6259432561e794dea0e060f482d15e2">kMeans</a>(observations, size, k);</div>
<div class="line"><span class="lineno"> 374</span> <a class="code hl_function" href="../../d6/d76/k__means__clustering_8c.html#a5b4ba704e02672e59cfa35f82e3db28a">printEPS</a>(observations, size, clusters, k);</div>
<div class="line"><span class="lineno"> 375</span> <span class="comment">// Free the accquired memory</span></div>
<div class="line"><span class="lineno"> 376</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(observations);</div>
<div class="line"><span class="lineno"> 377</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(clusters);</div>
<div class="line"><span class="lineno"> 378</span>}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d6/d76/k__means__clustering_8c_a0283886819c7c140a023582b7269e2d0_cgraph.svg" width="355" height="110"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- 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="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.6 </li>
</ul>
</div>
</body>
</html>