mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
411 lines
36 KiB
HTML
411 lines
36 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: 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 style="height: 56px;">
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">Algorithms_in_C
|
|
 <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&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&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&dn=gpl-2.0.txt GPL-v2 */
|
|
$(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">
|
|
<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> |
|
|
<a href="#typedef-members">Typedefs</a> |
|
|
<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="380" height="51"><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 name="nested-classes"></a>
|
|
Data Structures</h2></td></tr>
|
|
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct  </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"> </td></tr>
|
|
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct  </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"> </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"><a id="ga8cc0f5e9645e6faf40d1746acf777944"></a>
|
|
typedef struct <a class="el" href="../../d1/d5e/structobservation.html">observation</a> </td><td class="memItemRight" valign="bottom"><b>observation</b></td></tr>
|
|
<tr class="separator:ga8cc0f5e9645e6faf40d1746acf777944"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:gabe256d797c00b793041eac33f07804c1"><td class="memItemLeft" align="right" valign="top"><a id="gabe256d797c00b793041eac33f07804c1"></a>
|
|
typedef struct <a class="el" href="../../d1/d99/structcluster.html">cluster</a> </td><td class="memItemRight" valign="bottom"><b>cluster</b></td></tr>
|
|
<tr class="separator:gabe256d797c00b793041eac33f07804c1"><td class="memSeparator" colspan="2"> </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 </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"> </td></tr>
|
|
<tr class="memitem:gadee39a3f17bf5144df5592e48dbfc9f7"><td class="memItemLeft" align="right" valign="top">void </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"> </td></tr>
|
|
<tr class="memitem:gad229b1dc406cb5ea510f26a373ed8bfd"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../d1/d99/structcluster.html">cluster</a> * </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d71/group__k__means.html#gad229b1dc406cb5ea510f26a373ed8bfd">kMeans</a> (<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"> </td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
|
<h2 class="groupheader">Function Documentation</h2>
|
|
<a id="gadee39a3f17bf5144df5592e48dbfc9f7"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#gadee39a3f17bf5144df5592e48dbfc9f7">◆ </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> </td>
|
|
<td class="paramname"><em>observations</em>[], </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">size_t </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> * </td>
|
|
<td class="paramname"><em>centroid</em> </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"><a name="l00099"></a><span class="lineno"> 99</span> {</div>
|
|
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span>  <span class="keywordtype">size_t</span> i = 0;</div>
|
|
<div class="line"><a name="l00101"></a><span class="lineno"> 101</span>  centroid-><a class="code" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> = 0;</div>
|
|
<div class="line"><a name="l00102"></a><span class="lineno"> 102</span>  centroid-><a class="code" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> = 0;</div>
|
|
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>  centroid-><a class="code" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a> = size;</div>
|
|
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>  <span class="keywordflow">for</span> (; i < size; i++)</div>
|
|
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>  {</div>
|
|
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>  centroid-><a class="code" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> += observations[i].<a class="code" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>;</div>
|
|
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>  centroid-><a class="code" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> += observations[i].<a class="code" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>;</div>
|
|
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>  observations[i].<a class="code" href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">group</a> = 0;</div>
|
|
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>  }</div>
|
|
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>  centroid-><a class="code" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> /= centroid-><a class="code" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a>;</div>
|
|
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>  centroid-><a class="code" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> /= centroid-><a class="code" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a>;</div>
|
|
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="gad339c41d3ee9e6729aca9e9ab3f7d2d9"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#gad339c41d3ee9e6729aca9e9ab3f7d2d9">◆ </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> * </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> </td>
|
|
<td class="paramname"><em>clusters</em>[], </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>k</em> </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"><a name="l00070"></a><span class="lineno"> 70</span> {</div>
|
|
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>  <span class="keywordtype">double</span> minD = DBL_MAX;</div>
|
|
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>  <span class="keywordtype">double</span> dist = 0;</div>
|
|
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>  <span class="keywordtype">int</span> index = -1;</div>
|
|
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>  <span class="keywordtype">int</span> i = 0;</div>
|
|
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>  <span class="keywordflow">for</span> (; i < k; i++)</div>
|
|
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>  {</div>
|
|
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>  <span class="comment">/* Calculate Squared Distance*/</span></div>
|
|
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>  dist = (clusters[i].<a class="code" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> - o-><a class="code" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>) * (clusters[i].x - o-><a class="code" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>) +</div>
|
|
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>  (clusters[i].<a class="code" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> - o-><a class="code" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>) * (clusters[i].y - o-><a class="code" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>);</div>
|
|
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span>  <span class="keywordflow">if</span> (dist < minD)</div>
|
|
<div class="line"><a name="l00081"></a><span class="lineno"> 81</span>  {</div>
|
|
<div class="line"><a name="l00082"></a><span class="lineno"> 82</span>  minD = dist;</div>
|
|
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>  index = i;</div>
|
|
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>  }</div>
|
|
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>  }</div>
|
|
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>  <span class="keywordflow">return</span> index;</div>
|
|
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="gad229b1dc406cb5ea510f26a373ed8bfd"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#gad229b1dc406cb5ea510f26a373ed8bfd">◆ </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> </td>
|
|
<td class="paramname"><em>observations</em>[], </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">size_t </td>
|
|
<td class="paramname"><em>size</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>k</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>–K Means Algorithm–</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"><a name="l00135"></a><span class="lineno"> 135</span> {</div>
|
|
<div class="line"><a name="l00136"></a><span class="lineno"> 136</span>  <a class="code" href="../../d1/d99/structcluster.html">cluster</a>* clusters = NULL;</div>
|
|
<div class="line"><a name="l00137"></a><span class="lineno"> 137</span>  <span class="keywordflow">if</span> (k <= 1)</div>
|
|
<div class="line"><a name="l00138"></a><span class="lineno"> 138</span>  {</div>
|
|
<div class="line"><a name="l00139"></a><span class="lineno"> 139</span>  <span class="comment">/*</span></div>
|
|
<div class="line"><a name="l00140"></a><span class="lineno"> 140</span> <span class="comment"> If we have to cluster them only in one group</span></div>
|
|
<div class="line"><a name="l00141"></a><span class="lineno"> 141</span> <span class="comment"> then calculate centroid of observations and</span></div>
|
|
<div class="line"><a name="l00142"></a><span class="lineno"> 142</span> <span class="comment"> that will be a ingle cluster</span></div>
|
|
<div class="line"><a name="l00143"></a><span class="lineno"> 143</span> <span class="comment"> */</span></div>
|
|
<div class="line"><a name="l00144"></a><span class="lineno"> 144</span>  clusters = (<a class="code" href="../../d1/d99/structcluster.html">cluster</a>*)malloc(<span class="keyword">sizeof</span>(<a class="code" href="../../d1/d99/structcluster.html">cluster</a>));</div>
|
|
<div class="line"><a name="l00145"></a><span class="lineno"> 145</span>  memset(clusters, 0, <span class="keyword">sizeof</span>(<a class="code" href="../../d1/d99/structcluster.html">cluster</a>));</div>
|
|
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>  <a class="code" href="../../d8/d71/group__k__means.html#gadee39a3f17bf5144df5592e48dbfc9f7">calculateCentroid</a>(observations, size, clusters);</div>
|
|
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span>  }</div>
|
|
<div class="line"><a name="l00148"></a><span class="lineno"> 148</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (k < size)</div>
|
|
<div class="line"><a name="l00149"></a><span class="lineno"> 149</span>  {</div>
|
|
<div class="line"><a name="l00150"></a><span class="lineno"> 150</span>  clusters = malloc(<span class="keyword">sizeof</span>(<a class="code" href="../../d1/d99/structcluster.html">cluster</a>) * k);</div>
|
|
<div class="line"><a name="l00151"></a><span class="lineno"> 151</span>  memset(clusters, 0, k * <span class="keyword">sizeof</span>(<a class="code" href="../../d1/d99/structcluster.html">cluster</a>));</div>
|
|
<div class="line"><a name="l00152"></a><span class="lineno"> 152</span>  <span class="comment">/* STEP 1 */</span></div>
|
|
<div class="line"><a name="l00153"></a><span class="lineno"> 153</span>  <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> j = 0; j < size; j++)</div>
|
|
<div class="line"><a name="l00154"></a><span class="lineno"> 154</span>  {</div>
|
|
<div class="line"><a name="l00155"></a><span class="lineno"> 155</span>  observations[j].<a class="code" href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">group</a> = rand() % k;</div>
|
|
<div class="line"><a name="l00156"></a><span class="lineno"> 156</span>  }</div>
|
|
<div class="line"><a name="l00157"></a><span class="lineno"> 157</span>  <span class="keywordtype">size_t</span> changed = 0;</div>
|
|
<div class="line"><a name="l00158"></a><span class="lineno"> 158</span>  <span class="keywordtype">size_t</span> minAcceptedError =</div>
|
|
<div class="line"><a name="l00159"></a><span class="lineno"> 159</span>  size /</div>
|
|
<div class="line"><a name="l00160"></a><span class="lineno"> 160</span>  10000; <span class="comment">// Do until 99.99 percent points are in correct cluster</span></div>
|
|
<div class="line"><a name="l00161"></a><span class="lineno"> 161</span>  <span class="keywordtype">int</span> t = 0;</div>
|
|
<div class="line"><a name="l00162"></a><span class="lineno"> 162</span>  <span class="keywordflow">do</span></div>
|
|
<div class="line"><a name="l00163"></a><span class="lineno"> 163</span>  {</div>
|
|
<div class="line"><a name="l00164"></a><span class="lineno"> 164</span>  <span class="comment">/* Initialize clusters */</span></div>
|
|
<div class="line"><a name="l00165"></a><span class="lineno"> 165</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < k; i++)</div>
|
|
<div class="line"><a name="l00166"></a><span class="lineno"> 166</span>  {</div>
|
|
<div class="line"><a name="l00167"></a><span class="lineno"> 167</span>  clusters[i].<a class="code" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> = 0;</div>
|
|
<div class="line"><a name="l00168"></a><span class="lineno"> 168</span>  clusters[i].<a class="code" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> = 0;</div>
|
|
<div class="line"><a name="l00169"></a><span class="lineno"> 169</span>  clusters[i].<a class="code" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a> = 0;</div>
|
|
<div class="line"><a name="l00170"></a><span class="lineno"> 170</span>  }</div>
|
|
<div class="line"><a name="l00171"></a><span class="lineno"> 171</span>  <span class="comment">/* STEP 2*/</span></div>
|
|
<div class="line"><a name="l00172"></a><span class="lineno"> 172</span>  <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> j = 0; j < size; j++)</div>
|
|
<div class="line"><a name="l00173"></a><span class="lineno"> 173</span>  {</div>
|
|
<div class="line"><a name="l00174"></a><span class="lineno"> 174</span>  t = observations[j].<a class="code" href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">group</a>;</div>
|
|
<div class="line"><a name="l00175"></a><span class="lineno"> 175</span>  clusters[t].<a class="code" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> += observations[j].<a class="code" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>;</div>
|
|
<div class="line"><a name="l00176"></a><span class="lineno"> 176</span>  clusters[t].<a class="code" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> += observations[j].<a class="code" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>;</div>
|
|
<div class="line"><a name="l00177"></a><span class="lineno"> 177</span>  clusters[t].<a class="code" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a>++;</div>
|
|
<div class="line"><a name="l00178"></a><span class="lineno"> 178</span>  }</div>
|
|
<div class="line"><a name="l00179"></a><span class="lineno"> 179</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < k; i++)</div>
|
|
<div class="line"><a name="l00180"></a><span class="lineno"> 180</span>  {</div>
|
|
<div class="line"><a name="l00181"></a><span class="lineno"> 181</span>  clusters[i].<a class="code" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> /= clusters[i].<a class="code" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a>;</div>
|
|
<div class="line"><a name="l00182"></a><span class="lineno"> 182</span>  clusters[i].<a class="code" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> /= clusters[i].<a class="code" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a>;</div>
|
|
<div class="line"><a name="l00183"></a><span class="lineno"> 183</span>  }</div>
|
|
<div class="line"><a name="l00184"></a><span class="lineno"> 184</span>  <span class="comment">/* STEP 3 and 4 */</span></div>
|
|
<div class="line"><a name="l00185"></a><span class="lineno"> 185</span>  changed = 0; <span class="comment">// this variable stores change in clustering</span></div>
|
|
<div class="line"><a name="l00186"></a><span class="lineno"> 186</span>  <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> j = 0; j < size; j++)</div>
|
|
<div class="line"><a name="l00187"></a><span class="lineno"> 187</span>  {</div>
|
|
<div class="line"><a name="l00188"></a><span class="lineno"> 188</span>  t = <a class="code" href="../../d8/d71/group__k__means.html#gad339c41d3ee9e6729aca9e9ab3f7d2d9">calculateNearst</a>(observations + j, clusters, k);</div>
|
|
<div class="line"><a name="l00189"></a><span class="lineno"> 189</span>  <span class="keywordflow">if</span> (t != observations[j].group)</div>
|
|
<div class="line"><a name="l00190"></a><span class="lineno"> 190</span>  {</div>
|
|
<div class="line"><a name="l00191"></a><span class="lineno"> 191</span>  changed++;</div>
|
|
<div class="line"><a name="l00192"></a><span class="lineno"> 192</span>  observations[j].<a class="code" href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">group</a> = t;</div>
|
|
<div class="line"><a name="l00193"></a><span class="lineno"> 193</span>  }</div>
|
|
<div class="line"><a name="l00194"></a><span class="lineno"> 194</span>  }</div>
|
|
<div class="line"><a name="l00195"></a><span class="lineno"> 195</span>  } <span class="keywordflow">while</span> (changed > minAcceptedError); <span class="comment">// Keep on grouping until we have</span></div>
|
|
<div class="line"><a name="l00196"></a><span class="lineno"> 196</span>  <span class="comment">// got almost best clustering</span></div>
|
|
<div class="line"><a name="l00197"></a><span class="lineno"> 197</span>  }</div>
|
|
<div class="line"><a name="l00198"></a><span class="lineno"> 198</span>  <span class="keywordflow">else</span></div>
|
|
<div class="line"><a name="l00199"></a><span class="lineno"> 199</span>  {</div>
|
|
<div class="line"><a name="l00200"></a><span class="lineno"> 200</span>  <span class="comment">/* If no of clusters is more than observations</span></div>
|
|
<div class="line"><a name="l00201"></a><span class="lineno"> 201</span> <span class="comment"> each observation can be its own cluster</span></div>
|
|
<div class="line"><a name="l00202"></a><span class="lineno"> 202</span> <span class="comment"> */</span></div>
|
|
<div class="line"><a name="l00203"></a><span class="lineno"> 203</span>  clusters = (<a class="code" href="../../d1/d99/structcluster.html">cluster</a>*)malloc(<span class="keyword">sizeof</span>(<a class="code" href="../../d1/d99/structcluster.html">cluster</a>) * k);</div>
|
|
<div class="line"><a name="l00204"></a><span class="lineno"> 204</span>  memset(clusters, 0, k * <span class="keyword">sizeof</span>(<a class="code" href="../../d1/d99/structcluster.html">cluster</a>));</div>
|
|
<div class="line"><a name="l00205"></a><span class="lineno"> 205</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j < size; j++)</div>
|
|
<div class="line"><a name="l00206"></a><span class="lineno"> 206</span>  {</div>
|
|
<div class="line"><a name="l00207"></a><span class="lineno"> 207</span>  clusters[j].<a class="code" href="../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570">x</a> = observations[j].<a class="code" href="../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade">x</a>;</div>
|
|
<div class="line"><a name="l00208"></a><span class="lineno"> 208</span>  clusters[j].<a class="code" href="../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479">y</a> = observations[j].<a class="code" href="../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7">y</a>;</div>
|
|
<div class="line"><a name="l00209"></a><span class="lineno"> 209</span>  clusters[j].<a class="code" href="../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b">count</a> = 1;</div>
|
|
<div class="line"><a name="l00210"></a><span class="lineno"> 210</span>  observations[j].<a class="code" href="../../d1/d5e/structobservation.html#a2db8ace685c08aa7b52f5a28b0843aab">group</a> = j;</div>
|
|
<div class="line"><a name="l00211"></a><span class="lineno"> 211</span>  }</div>
|
|
<div class="line"><a name="l00212"></a><span class="lineno"> 212</span>  }</div>
|
|
<div class="line"><a name="l00213"></a><span class="lineno"> 213</span>  <span class="keywordflow">return</span> clusters;</div>
|
|
<div class="line"><a name="l00214"></a><span class="lineno"> 214</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="../../d8/d71/group__k__means_gad229b1dc406cb5ea510f26a373ed8bfd_cgraph.svg" width="250" height="88"><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 -->
|
|
<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="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="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="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="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_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="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="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>
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<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>
|