TheAlgorithms-C/d8/d71/group__k__means.html

414 lines
35 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" 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.8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: K-Means Clustering Algorithm</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.8 -->
<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('d8/d71/group__k__means.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="#typedef-members">Typedefs</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">K-Means Clustering Algorithm<div class="ingroups"><a class="el" href="../../d9/d66/group__machine__learning.html">Machine learning algorithms</a></div></div></div>
</div><!--header-->
<div class="contents">
<div class="dynheader">
Collaboration diagram for K-Means Clustering Algorithm:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d71/group__k__means.svg" width="379" height="52"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></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="typedef-members" name="typedef-members"></a>
Typedefs</h2></td></tr>
<tr class="memitem:ga8cc0f5e9645e6faf40d1746acf777944" id="r_ga8cc0f5e9645e6faf40d1746acf777944"><td class="memItemLeft" align="right" valign="top"><a id="ga8cc0f5e9645e6faf40d1746acf777944" name="ga8cc0f5e9645e6faf40d1746acf777944"></a>
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" id="r_gabe256d797c00b793041eac33f07804c1"><td class="memItemLeft" align="right" valign="top"><a id="gabe256d797c00b793041eac33f07804c1" name="gabe256d797c00b793041eac33f07804c1"></a>
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" id="r_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" id="r_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" id="r_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>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<h2 class="groupheader">Function Documentation</h2>
<a id="gadee39a3f17bf5144df5592e48dbfc9f7" name="gadee39a3f17bf5144df5592e48dbfc9f7"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gadee39a3f17bf5144df5592e48dbfc9f7">&#9670;&#160;</a></span>calculateCentroid()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void calculateCentroid </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d1/d5e/structobservation.html">observation</a>&#160;</td>
<td class="paramname"><em>observations</em>[], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>size</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>centroid</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Calculate centoid and assign it to the cluster variable</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">observations</td><td>an array of observations whose centroid is calculated </td></tr>
<tr><td class="paramname">size</td><td>size of the observations array </td></tr>
<tr><td class="paramname">centroid</td><td>a reference to cluster object to store information of centroid </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 99</span>{</div>
<div class="line"><span class="lineno"> 100</span> <span class="keywordtype">size_t</span> i = 0;</div>
<div class="line"><span class="lineno"> 101</span> centroid-&gt;<a class="code hl_variable" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> = 0;</div>
<div class="line"><span class="lineno"> 102</span> centroid-&gt;<a class="code hl_variable" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> = 0;</div>
<div class="line"><span class="lineno"> 103</span> centroid-&gt;<a class="code hl_variable" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a> = size;</div>
<div class="line"><span class="lineno"> 104</span> <span class="keywordflow">for</span> (; i &lt; size; i++)</div>
<div class="line"><span class="lineno"> 105</span> {</div>
<div class="line"><span class="lineno"> 106</span> centroid-&gt;<a class="code hl_variable" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> += observations[i].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>;</div>
<div class="line"><span class="lineno"> 107</span> centroid-&gt;<a class="code hl_variable" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> += observations[i].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>;</div>
<div class="line"><span class="lineno"> 108</span> observations[i].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">group</a> = 0;</div>
<div class="line"><span class="lineno"> 109</span> }</div>
<div class="line"><span class="lineno"> 110</span> centroid-&gt;<a class="code hl_variable" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> /= centroid-&gt;<a class="code hl_variable" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a>;</div>
<div class="line"><span class="lineno"> 111</span> centroid-&gt;<a class="code hl_variable" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> /= centroid-&gt;<a class="code hl_variable" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a>;</div>
<div class="line"><span class="lineno"> 112</span>}</div>
<div class="ttc" id="astructcluster_html_a10fa7010c12d0f03a422d68321495479"><div class="ttname"><a href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">cluster::y</a></div><div class="ttdeci">double y</div><div class="ttdoc">ordinate of centroid of this cluster</div><div class="ttdef"><b>Definition</b> k_means_clustering.c:55</div></div>
<div class="ttc" id="astructcluster_html_a13278ef636c1d9bd9ce8fad736f4c570"><div class="ttname"><a href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">cluster::x</a></div><div class="ttdeci">double x</div><div class="ttdoc">abscissa centroid of this cluster</div><div class="ttdef"><b>Definition</b> k_means_clustering.c:54</div></div>
<div class="ttc" id="astructcluster_html_aaacf0562ee2d9e8866c66ddaa6527c2b"><div class="ttname"><a href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">cluster::count</a></div><div class="ttdeci">size_t count</div><div class="ttdoc">count of observations present in this cluster</div><div class="ttdef"><b>Definition</b> k_means_clustering.c:56</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_a2db8ace685c08aa7b52f5a28b0843aab"><div class="ttname"><a href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">observation::group</a></div><div class="ttdeci">int group</div><div class="ttdoc">the group no in which this observation would go</div><div class="ttdef"><b>Definition</b> k_means_clustering.c:42</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="gad339c41d3ee9e6729aca9e9ab3f7d2d9" name="gad339c41d3ee9e6729aca9e9ab3f7d2d9"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gad339c41d3ee9e6729aca9e9ab3f7d2d9">&#9670;&#160;</a></span>calculateNearst()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int calculateNearst </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d1/d5e/structobservation.html">observation</a> *&#160;</td>
<td class="paramname"><em>o</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>clusters</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>Returns the index of centroid nearest to given observation</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">o</td><td>observation </td></tr>
<tr><td class="paramname">clusters</td><td>array of cluster having centroids coordinates </td></tr>
<tr><td class="paramname">k</td><td>size of clusters array</td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>the index of nearest centroid for given observation </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 70</span>{</div>
<div class="line"><span class="lineno"> 71</span> <span class="keywordtype">double</span> minD = DBL_MAX;</div>
<div class="line"><span class="lineno"> 72</span> <span class="keywordtype">double</span> dist = 0;</div>
<div class="line"><span class="lineno"> 73</span> <span class="keywordtype">int</span> index = -1;</div>
<div class="line"><span class="lineno"> 74</span> <span class="keywordtype">int</span> i = 0;</div>
<div class="line"><span class="lineno"> 75</span> <span class="keywordflow">for</span> (; i &lt; k; i++)</div>
<div class="line"><span class="lineno"> 76</span> {</div>
<div class="line"><span class="lineno"> 77</span> <span class="comment">/* Calculate Squared Distance*/</span></div>
<div class="line"><span class="lineno"> 78</span> dist = (clusters[i].<a class="code hl_variable" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> - o-&gt;<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>) * (clusters[i].x - o-&gt;<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>) +</div>
<div class="line"><span class="lineno"> 79</span> (clusters[i].<a class="code hl_variable" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> - o-&gt;<a class="code hl_variable" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>) * (clusters[i].y - o-&gt;<a class="code hl_variable" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>);</div>
<div class="line"><span class="lineno"> 80</span> <span class="keywordflow">if</span> (dist &lt; minD)</div>
<div class="line"><span class="lineno"> 81</span> {</div>
<div class="line"><span class="lineno"> 82</span> minD = dist;</div>
<div class="line"><span class="lineno"> 83</span> index = i;</div>
<div class="line"><span class="lineno"> 84</span> }</div>
<div class="line"><span class="lineno"> 85</span> }</div>
<div class="line"><span class="lineno"> 86</span> <span class="keywordflow">return</span> index;</div>
<div class="line"><span class="lineno"> 87</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="gaf6259432561e794dea0e060f482d15e2" name="gaf6259432561e794dea0e060f482d15e2"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gaf6259432561e794dea0e060f482d15e2">&#9670;&#160;</a></span>kMeans()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="el" href="../../d1/d99/structcluster.html">cluster</a> * kMeans </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../d1/d5e/structobservation.html">observation</a>&#160;</td>
<td class="paramname"><em>observations</em>[], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>size</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>&ndash;K Means Algorithm&ndash;</p><ol type="1">
<li>Assign each observation to one of k groups creating a random initial clustering</li>
<li>Find the centroid of observations for each cluster to form new centroids</li>
<li>Find the centroid which is nearest for each observation among the calculated centroids</li>
<li>Assign the observation to its nearest centroid to create a new clustering.</li>
<li>Repeat step 2,3,4 until there is no change the current clustering and is same as last clustering.</li>
</ol>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">observations</td><td>an array of observations to cluster </td></tr>
<tr><td class="paramname">size</td><td>size of observations array </td></tr>
<tr><td class="paramname">k</td><td>no of clusters to be made</td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>pointer to cluster object </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 135</span>{</div>
<div class="line"><span class="lineno"> 136</span> <a class="code hl_struct" href="../../d1/d99/structcluster.html">cluster</a>* clusters = NULL;</div>
<div class="line"><span class="lineno"> 137</span> <span class="keywordflow">if</span> (k &lt;= 1)</div>
<div class="line"><span class="lineno"> 138</span> {</div>
<div class="line"><span class="lineno"> 139</span> <span class="comment">/*</span></div>
<div class="line"><span class="lineno"> 140</span><span class="comment"> If we have to cluster them only in one group</span></div>
<div class="line"><span class="lineno"> 141</span><span class="comment"> then calculate centroid of observations and</span></div>
<div class="line"><span class="lineno"> 142</span><span class="comment"> that will be a ingle cluster</span></div>
<div class="line"><span class="lineno"> 143</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 144</span> clusters = (<a class="code hl_struct" href="../../d1/d99/structcluster.html">cluster</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/d99/structcluster.html">cluster</a>));</div>
<div class="line"><span class="lineno"> 145</span> memset(clusters, 0, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="../../d1/d99/structcluster.html">cluster</a>));</div>
<div class="line"><span class="lineno"> 146</span> <a class="code hl_function" href="../../d8/d71/group__k__means.html#gadee39a3f17bf5144df5592e48dbfc9f7">calculateCentroid</a>(observations, size, clusters);</div>
<div class="line"><span class="lineno"> 147</span> }</div>
<div class="line"><span class="lineno"> 148</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (k &lt; size)</div>
<div class="line"><span class="lineno"> 149</span> {</div>
<div class="line"><span class="lineno"> 150</span> clusters = <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/d99/structcluster.html">cluster</a>) * k);</div>
<div class="line"><span class="lineno"> 151</span> memset(clusters, 0, k * <span class="keyword">sizeof</span>(<a class="code hl_struct" href="../../d1/d99/structcluster.html">cluster</a>));</div>
<div class="line"><span class="lineno"> 152</span> <span class="comment">/* STEP 1 */</span></div>
<div class="line"><span class="lineno"> 153</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> j = 0; j &lt; size; j++)</div>
<div class="line"><span class="lineno"> 154</span> {</div>
<div class="line"><span class="lineno"> 155</span> observations[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">group</a> = rand() % k;</div>
<div class="line"><span class="lineno"> 156</span> }</div>
<div class="line"><span class="lineno"> 157</span> <span class="keywordtype">size_t</span> changed = 0;</div>
<div class="line"><span class="lineno"> 158</span> <span class="keywordtype">size_t</span> minAcceptedError =</div>
<div class="line"><span class="lineno"> 159</span> size /</div>
<div class="line"><span class="lineno"> 160</span> 10000; <span class="comment">// Do until 99.99 percent points are in correct cluster</span></div>
<div class="line"><span class="lineno"> 161</span> <span class="keywordtype">int</span> t = 0;</div>
<div class="line"><span class="lineno"> 162</span> <span class="keywordflow">do</span></div>
<div class="line"><span class="lineno"> 163</span> {</div>
<div class="line"><span class="lineno"> 164</span> <span class="comment">/* Initialize clusters */</span></div>
<div class="line"><span class="lineno"> 165</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; k; i++)</div>
<div class="line"><span class="lineno"> 166</span> {</div>
<div class="line"><span class="lineno"> 167</span> clusters[i].<a class="code hl_variable" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> = 0;</div>
<div class="line"><span class="lineno"> 168</span> clusters[i].<a class="code hl_variable" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> = 0;</div>
<div class="line"><span class="lineno"> 169</span> clusters[i].<a class="code hl_variable" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a> = 0;</div>
<div class="line"><span class="lineno"> 170</span> }</div>
<div class="line"><span class="lineno"> 171</span> <span class="comment">/* STEP 2*/</span></div>
<div class="line"><span class="lineno"> 172</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> j = 0; j &lt; size; j++)</div>
<div class="line"><span class="lineno"> 173</span> {</div>
<div class="line"><span class="lineno"> 174</span> t = observations[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">group</a>;</div>
<div class="line"><span class="lineno"> 175</span> clusters[t].<a class="code hl_variable" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> += observations[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>;</div>
<div class="line"><span class="lineno"> 176</span> clusters[t].<a class="code hl_variable" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> += observations[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>;</div>
<div class="line"><span class="lineno"> 177</span> clusters[t].<a class="code hl_variable" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a>++;</div>
<div class="line"><span class="lineno"> 178</span> }</div>
<div class="line"><span class="lineno"> 179</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; k; i++)</div>
<div class="line"><span class="lineno"> 180</span> {</div>
<div class="line"><span class="lineno"> 181</span> clusters[i].<a class="code hl_variable" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> /= clusters[i].<a class="code hl_variable" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a>;</div>
<div class="line"><span class="lineno"> 182</span> clusters[i].<a class="code hl_variable" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> /= clusters[i].<a class="code hl_variable" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a>;</div>
<div class="line"><span class="lineno"> 183</span> }</div>
<div class="line"><span class="lineno"> 184</span> <span class="comment">/* STEP 3 and 4 */</span></div>
<div class="line"><span class="lineno"> 185</span> changed = 0; <span class="comment">// this variable stores change in clustering</span></div>
<div class="line"><span class="lineno"> 186</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> j = 0; j &lt; size; j++)</div>
<div class="line"><span class="lineno"> 187</span> {</div>
<div class="line"><span class="lineno"> 188</span> t = <a class="code hl_function" href="../../d8/d71/group__k__means.html#gad339c41d3ee9e6729aca9e9ab3f7d2d9">calculateNearst</a>(observations + j, clusters, k);</div>
<div class="line"><span class="lineno"> 189</span> <span class="keywordflow">if</span> (t != observations[j].group)</div>
<div class="line"><span class="lineno"> 190</span> {</div>
<div class="line"><span class="lineno"> 191</span> changed++;</div>
<div class="line"><span class="lineno"> 192</span> observations[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">group</a> = t;</div>
<div class="line"><span class="lineno"> 193</span> }</div>
<div class="line"><span class="lineno"> 194</span> }</div>
<div class="line"><span class="lineno"> 195</span> } <span class="keywordflow">while</span> (changed &gt; minAcceptedError); <span class="comment">// Keep on grouping until we have</span></div>
<div class="line"><span class="lineno"> 196</span> <span class="comment">// got almost best clustering</span></div>
<div class="line"><span class="lineno"> 197</span> }</div>
<div class="line"><span class="lineno"> 198</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 199</span> {</div>
<div class="line"><span class="lineno"> 200</span> <span class="comment">/* If no of clusters is more than observations</span></div>
<div class="line"><span class="lineno"> 201</span><span class="comment"> each observation can be its own cluster</span></div>
<div class="line"><span class="lineno"> 202</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 203</span> clusters = (<a class="code hl_struct" href="../../d1/d99/structcluster.html">cluster</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/d99/structcluster.html">cluster</a>) * k);</div>
<div class="line"><span class="lineno"> 204</span> memset(clusters, 0, k * <span class="keyword">sizeof</span>(<a class="code hl_struct" href="../../d1/d99/structcluster.html">cluster</a>));</div>
<div class="line"><span class="lineno"> 205</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; size; j++)</div>
<div class="line"><span class="lineno"> 206</span> {</div>
<div class="line"><span class="lineno"> 207</span> clusters[j].<a class="code hl_variable" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> = observations[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>;</div>
<div class="line"><span class="lineno"> 208</span> clusters[j].<a class="code hl_variable" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> = observations[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>;</div>
<div class="line"><span class="lineno"> 209</span> clusters[j].<a class="code hl_variable" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a> = 1;</div>
<div class="line"><span class="lineno"> 210</span> observations[j].<a class="code hl_variable" href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">group</a> = j;</div>
<div class="line"><span class="lineno"> 211</span> }</div>
<div class="line"><span class="lineno"> 212</span> }</div>
<div class="line"><span class="lineno"> 213</span> <span class="keywordflow">return</span> clusters;</div>
<div class="line"><span class="lineno"> 214</span>}</div>
<div class="ttc" id="agroup__k__means_html_gad339c41d3ee9e6729aca9e9ab3f7d2d9"><div class="ttname"><a href="../../d8/d71/group__k__means.html#gad339c41d3ee9e6729aca9e9ab3f7d2d9">calculateNearst</a></div><div class="ttdeci">int calculateNearst(observation *o, cluster clusters[], int k)</div><div class="ttdef"><b>Definition</b> k_means_clustering.c:69</div></div>
<div class="ttc" id="agroup__k__means_html_gadee39a3f17bf5144df5592e48dbfc9f7"><div class="ttname"><a href="../../d8/d71/group__k__means.html#gadee39a3f17bf5144df5592e48dbfc9f7">calculateCentroid</a></div><div class="ttdeci">void calculateCentroid(observation observations[], size_t size, cluster *centroid)</div><div class="ttdef"><b>Definition</b> k_means_clustering.c:97</div></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="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><!-- 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="../../d8/d71/group__k__means_gaf6259432561e794dea0e060f482d15e2_cgraph.svg" width="248" height="86"><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="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.8 </li>
</ul>
</div>
</body>
</html>