2020-08-08 20:40:03 +08:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2022-09-08 08:17:03 +08:00
< html xmlns = "http://www.w3.org/1999/xhtml" lang = "en-US" >
2020-08-08 20:40:03 +08:00
< head >
< meta http-equiv = "Content-Type" content = "text/xhtml;charset=UTF-8" / >
2021-10-26 03:34:27 +08:00
< meta http-equiv = "X-UA-Compatible" content = "IE=11" / >
2023-01-20 12:47:59 +08:00
< meta name = "generator" content = "Doxygen 1.9.5" / >
2020-08-08 20:40:03 +08:00
< 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" >
2021-10-26 03:34:27 +08:00
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
2020-08-08 20:40:03 +08:00
});
< / 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 >
2022-01-15 04:11:27 +08:00
< tr id = "projectrow" >
< td id = "projectalign" >
2021-10-26 03:34:27 +08:00
< div id = "projectname" > Algorithms_in_C< span id = "projectnumber" >   1.0.0< / span >
2020-08-08 20:40:03 +08:00
< / div >
< div id = "projectbrief" > Set of algorithms implemented in C.< / div >
< / td >
< / tr >
< / tbody >
< / table >
< / div >
<!-- end header part -->
2023-01-20 12:47:59 +08:00
<!-- Generated by Doxygen 1.9.5 -->
2020-08-08 20:40:03 +08:00
< script type = "text/javascript" >
2021-10-26 03:34:27 +08:00
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699& dn=expat.txt MIT */
2022-09-08 08:17:03 +08:00
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
2020-08-08 20:40:03 +08:00
/* @license-end */
< / script >
< script type = "text/javascript" src = "../../menudata.js" > < / script >
< script type = "text/javascript" src = "../../menu.js" > < / script >
< script type = "text/javascript" >
2021-10-26 03:34:27 +08:00
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699& dn=expat.txt MIT */
2020-08-08 20:40:03 +08:00
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
2021-10-26 03:34:27 +08:00
/* @license-end */
< / script >
2020-08-08 20:40:03 +08:00
< 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" >
2021-10-26 03:34:27 +08:00
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699& dn=expat.txt MIT */
2020-08-08 20:40:03 +08:00
$(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" >
2022-09-08 08:17:03 +08:00
< 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 >
2020-08-08 20:40:03 +08:00
< / 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 >
2021-10-26 03:34:27 +08:00
< 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 >
2020-08-08 20:40:03 +08:00
< / div > <!-- header -->
< div class = "contents" >
< div class = "dynheader" >
Collaboration diagram for K-Means Clustering Algorithm:< / div >
< div class = "dyncontent" >
2021-12-10 01:57:52 +08:00
< 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 >
2020-08-08 20:40:03 +08:00
< / div >
< / div >
< table class = "memberdecls" >
2021-10-26 03:34:27 +08:00
< tr class = "heading" > < td colspan = "2" > < h2 class = "groupheader" > < a id = "nested-classes" name = "nested-classes" > < / a >
2020-08-08 20:40:03 +08:00
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" >
2021-10-26 03:34:27 +08:00
< tr class = "heading" > < td colspan = "2" > < h2 class = "groupheader" > < a id = "typedef-members" name = "typedef-members" > < / a >
2020-08-08 20:40:03 +08:00
Typedefs< / h2 > < / td > < / tr >
2021-10-26 03:34:27 +08:00
< tr class = "memitem:ga8cc0f5e9645e6faf40d1746acf777944" > < td class = "memItemLeft" align = "right" valign = "top" > < a id = "ga8cc0f5e9645e6faf40d1746acf777944" name = "ga8cc0f5e9645e6faf40d1746acf777944" > < / a >
2020-08-08 20:40:03 +08:00
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 >
2021-10-26 03:34:27 +08:00
< tr class = "memitem:gabe256d797c00b793041eac33f07804c1" > < td class = "memItemLeft" align = "right" valign = "top" > < a id = "gabe256d797c00b793041eac33f07804c1" name = "gabe256d797c00b793041eac33f07804c1" > < / a >
2020-08-08 20:40:03 +08:00
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" >
2021-10-26 03:34:27 +08:00
< tr class = "heading" > < td colspan = "2" > < h2 class = "groupheader" > < a id = "func-members" name = "func-members" > < / a >
2020-08-08 20:40:03 +08:00
Functions< / h2 > < / td > < / tr >
2020-08-09 00:29:26 +08:00
< 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 >
2020-08-08 20:40:03 +08:00
< tr class = "separator:gad339c41d3ee9e6729aca9e9ab3f7d2d9" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
2020-08-09 00:29:26 +08:00
< 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 >
2020-08-08 20:40:03 +08:00
< tr class = "separator:gadee39a3f17bf5144df5592e48dbfc9f7" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
2021-10-26 03:34:27 +08:00
< tr class = "memitem:gaf6259432561e794dea0e060f482d15e2" > < 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#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" >   < / td > < / tr >
2020-08-08 20:40:03 +08:00
< / table >
< a name = "details" id = "details" > < / a > < h2 class = "groupheader" > Detailed Description< / h2 >
2020-08-09 00:29:26 +08:00
< h2 class = "groupheader" > Function Documentation< / h2 >
2021-10-26 03:34:27 +08:00
< a id = "gadee39a3f17bf5144df5592e48dbfc9f7" name = "gadee39a3f17bf5144df5592e48dbfc9f7" > < / a >
2022-09-08 08:17:03 +08:00
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#gadee39a3f17bf5144df5592e48dbfc9f7" > ◆   < / a > < / span > calculateCentroid()< / h2 >
2020-08-09 00:29:26 +08:00
< 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" >
2023-01-20 12:47:59 +08:00
< p > Calculate centoid and assign it to the cluster variable< / p >
2020-08-09 00:29:26 +08:00
< 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 >
2022-01-15 04:11:27 +08:00
< 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-> < a class = "code hl_variable" href = "../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570" > x< / a > = 0;< / div >
< div class = "line" > < span class = "lineno" > 102< / span > centroid-> < a class = "code hl_variable" href = "../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479" > y< / a > = 0;< / div >
< div class = "line" > < span class = "lineno" > 103< / span > centroid-> < 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 < size; i++)< / div >
< div class = "line" > < span class = "lineno" > 105< / span > {< / div >
< div class = "line" > < span class = "lineno" > 106< / span > centroid-> < 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-> < 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-> < a class = "code hl_variable" href = "../../d1/d99/structcluster.html#a13278ef636c1d9bd9ce8fad736f4c570" > x< / a > /= centroid-> < a class = "code hl_variable" href = "../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b" > count< / a > ;< / div >
< div class = "line" > < span class = "lineno" > 111< / span > centroid-> < a class = "code hl_variable" href = "../../d1/d99/structcluster.html#a10fa7010c12d0f03a422d68321495479" > y< / a > /= centroid-> < a class = "code hl_variable" href = "../../d1/d99/structcluster.html#aaacf0562ee2d9e8866c66ddaa6527c2b" > count< / a > ;< / div >
< div class = "line" > < span class = "lineno" > 112< / span > }< / div >
2021-02-06 03:52:57 +08:00
< 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 >
2020-08-09 00:29:26 +08:00
< / div > <!-- fragment -->
< / div >
< / div >
2021-10-26 03:34:27 +08:00
< a id = "gad339c41d3ee9e6729aca9e9ab3f7d2d9" name = "gad339c41d3ee9e6729aca9e9ab3f7d2d9" > < / a >
2022-09-08 08:17:03 +08:00
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#gad339c41d3ee9e6729aca9e9ab3f7d2d9" > ◆   < / a > < / span > calculateNearst()< / h2 >
2020-08-09 00:29:26 +08:00
< 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" >
2023-01-20 12:47:59 +08:00
< p > Returns the index of centroid nearest to given observation< / p >
2020-08-09 00:29:26 +08:00
< 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 >
2022-01-15 04:11:27 +08:00
< 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 < 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-> < a class = "code hl_variable" href = "../../d1/d5e/structobservation.html#a04f3dcfd59dd91353395e35c9831fade" > x< / a > ) * (clusters[i].x - o-> < 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-> < a class = "code hl_variable" href = "../../d1/d5e/structobservation.html#ab6be1fa7024b2d5f3a30d6c6b70efdd7" > y< / a > ) * (clusters[i].y - o-> < 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 < 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 >
2020-08-09 00:29:26 +08:00
< / div > <!-- fragment -->
< / div >
< / div >
2021-10-26 03:34:27 +08:00
< a id = "gaf6259432561e794dea0e060f482d15e2" name = "gaf6259432561e794dea0e060f482d15e2" > < / a >
2022-09-08 08:17:03 +08:00
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#gaf6259432561e794dea0e060f482d15e2" > ◆   < / a > < / span > kMeans()< / h2 >
2020-08-09 00:29:26 +08:00
< div class = "memitem" >
< div class = "memproto" >
< table class = "memname" >
< tr >
2021-10-26 03:34:27 +08:00
< td class = "memname" > < a class = "el" href = "../../d1/d99/structcluster.html" > cluster< / a > * kMeans < / td >
2020-08-09 00:29:26 +08:00
< 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" >
2023-01-20 12:47:59 +08:00
< p > – K Means Algorithm– < / p > < ol type = "1" >
2020-08-09 00:29:26 +08:00
< 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 >
2022-01-15 04:11:27 +08:00
< 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 < = 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 < 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 < 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 < 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 < 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 < 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 < 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 > 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 < 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 >
2021-02-06 03:52:57 +08:00
< 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 >
2021-02-18 06:41:16 +08:00
< 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 >
2021-02-06 03:52:57 +08:00
< 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 >
2020-08-09 00:29:26 +08:00
< / div > <!-- fragment --> < div class = "dynheader" >
Here is the call graph for this function:< / div >
< div class = "dyncontent" >
2022-09-08 08:17:03 +08:00
< div class = "center" > < iframe scrolling = "no" frameborder = "0" src = "../../d8/d71/group__k__means_gaf6259432561e794dea0e060f482d15e2_cgraph.svg" width = "250" height = "86" > < p > < b > This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.< / b > < / p > < / iframe >
2020-08-09 00:29:26 +08:00
< / div >
< / div >
< / div >
< / div >
2020-08-08 20:40:03 +08:00
< / div > <!-- contents -->
< / div > <!-- doc - content -->
<!-- start footer part -->
< div id = "nav-path" class = "navpath" > <!-- id is needed for treeview function! -->
< ul >
2023-01-20 12:47:59 +08:00
< 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.5 < / li >
2020-08-08 20:40:03 +08:00
< / ul >
< / div >
< / body >
< / html >