<divclass="title">Kohonen SOM topology algorithm<divclass="ingroups"><aclass="el"href="../../d9/d66/group__machine__learning.html">Machine learning algorithms</a></div></div></div>
</div><!--header-->
<divclass="contents">
<divclass="dynheader">
Collaboration diagram for Kohonen SOM topology algorithm:</div>
<divclass="dyncontent">
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d1/d6b/group__kohonen__2d.svg"width="403"height="51"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<trclass="memdesc:"><tdclass="mdescLeft"> </td><tdclass="mdescRight">to store info regarding 3D arrays <ahref="../../d8/db8/structkohonen__array__3d.html#details">More...</a><br/></td></tr>
<trclass="memdesc:gaffe776513b24d84b39af8ab0930fef7f"><tdclass="mdescLeft"> </td><tdclass="mdescRight">shorthand for maximum value <br/></td></tr>
<trclass="memdesc:gac6afabdc09a49a433ee19d8a9486056d"><tdclass="mdescLeft"> </td><tdclass="mdescRight">shorthand for minimum value <br/></td></tr>
<trclass="memitem:ga8df35f04c1762a01dcf108fa13b897d6"><tdclass="memItemLeft"align="right"valign="top">double * </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">kohonen_data_3d</a> (const struct <aclass="el"href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *arr, int x, int y, int z)</td></tr>
<trclass="memdesc:ga8df35f04c1762a01dcf108fa13b897d6"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by: <ahref="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">More...</a><br/></td></tr>
<trclass="memitem:gaf5ce14f026d6d231bef29161bac2b485"><tdclass="memItemLeft"align="right"valign="top">double </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a> (double a, double b)</td></tr>
<trclass="memdesc:gaf5ce14f026d6d231bef29161bac2b485"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Helper function to generate a random number in a given interval. <ahref="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">More...</a><br/></td></tr>
<trclass="memitem:ga6824dc6d973eb3339af7aef5fea78b0c"><tdclass="memItemLeft"align="right"valign="top">int </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d1/d6b/group__kohonen__2d.html#ga6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a> (const char *fname, double **X, int num_points, int num_features)</td></tr>
<trclass="memdesc:ga6824dc6d973eb3339af7aef5fea78b0c"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Save a given n-dimensional data martix to file. <ahref="../../d1/d6b/group__kohonen__2d.html#ga6824dc6d973eb3339af7aef5fea78b0c">More...</a><br/></td></tr>
<trclass="memdesc:ga49d35f68f5d11d8ef6f8cce0d0e7bcba"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Create the distance matrix or <ahref="https://en.wikipedia.org/wiki/U-matrix">U-matrix</a> from the trained weights and save to disk. <ahref="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">More...</a><br/></td></tr>
<trclass="memitem:gadc22d512c00a9f5799ee067f4fb90b4b"><tdclass="memItemLeft"align="right"valign="top">void </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">get_min_2d</a> (double **X, int N, double *val, int *x_idx, int *y_idx)</td></tr>
<trclass="memdesc:gadc22d512c00a9f5799ee067f4fb90b4b"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Get minimum value and index of the value in a matrix. <ahref="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">More...</a><br/></td></tr>
<trclass="memitem:ga83abb572c60d202e100595a989dfe123"><tdclass="memItemLeft"align="right"valign="top">double </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">kohonen_update_weights</a> (const double *X, struct <aclass="el"href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *W, double **D, int num_out, int num_features, double alpha, int R)</td></tr>
<trclass="memdesc:ga83abb572c60d202e100595a989dfe123"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Update weights of the SOM using Kohonen algorithm. <ahref="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">More...</a><br/></td></tr>
<trclass="memitem:gacb42eda8af6ebd6a141a34ab00a0b710"><tdclass="memItemLeft"align="right"valign="top">void </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d1/d6b/group__kohonen__2d.html#gacb42eda8af6ebd6a141a34ab00a0b710">kohonen_som</a> (double **X, struct <aclass="el"href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *W, int num_samples, int num_features, int num_out, double alpha_min)</td></tr>
<trclass="memdesc:gacb42eda8af6ebd6a141a34ab00a0b710"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset. <ahref="../../d1/d6b/group__kohonen__2d.html#gacb42eda8af6ebd6a141a34ab00a0b710">More...</a><br/></td></tr>
<divclass="line"><aname="l00208"></a><spanclass="lineno"> 208</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i < N; i++) <spanclass="comment">// traverse each x-index</span></div>
<divclass="line"><aname="l00212"></a><spanclass="lineno"> 212</span> <spanclass="keywordflow">if</span> (X[i][j] < val[0]) <spanclass="comment">// if a lower value is found</span></div>
<divclass="line"><aname="l00213"></a><spanclass="lineno"> 213</span>  { <spanclass="comment">// save the value and its index</span></div>
<p>Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by: </p>
<pclass="formulaDsp">
\[ X_{i,j,k} = i\times M\times N + j\times N + k \]
</p>
<p> where \(L\), \(M\) and \(N\) are the 3D matrix dimensions. </p><dlclass="params"><dt>Parameters</dt><dd>
<tableclass="params">
<tr><tdclass="paramdir">[in]</td><tdclass="paramname">arr</td><td>pointer to <aclass="el"href="../../d8/db8/structkohonen__array__3d.html"title="to store info regarding 3D arrays">kohonen_array_3d</a> structure </td></tr>
<tr><tdclass="paramdir">[in]</td><tdclass="paramname">x</td><td>first index </td></tr>
<tr><tdclass="paramdir">[in]</td><tdclass="paramname">y</td><td>second index </td></tr>
<tr><tdclass="paramdir">[in]</td><tdclass="paramname">z</td><td>third index </td></tr>
</table>
</dd>
</dl>
<dlclass="section return"><dt>Returns</dt><dd>pointer to (x,y,z)^th location of data </dd></dl>
<divclass="line"><aname="l00319"></a><spanclass="lineno"> 319</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i < num_out; i++)</div>
<divclass="line"><aname="l00322"></a><spanclass="lineno"> 322</span> <spanclass="keywordtype">double</span> dmin = 1.f; <spanclass="comment">// average minimum distance of all samples</span></div>
<divclass="line"><aname="l00329"></a><spanclass="lineno"> 329</span> <spanclass="comment">// Loop for each sample pattern in the data set</span></div>
<divclass="line"><aname="l00332"></a><spanclass="lineno"> 332</span> <spanclass="comment">// update weights for the current input pattern sample</span></div>
<divclass="line"><aname="l00337"></a><spanclass="lineno"> 337</span> <spanclass="comment">// every 20th iteration, reduce the neighborhood range</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d1/d6b/group__kohonen__2d_gacb42eda8af6ebd6a141a34ab00a0b710_cgraph.svg"width="503"height="88"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00250"></a><spanclass="lineno"> 250</span> <spanclass="comment">// compute Euclidian distance of each output</span></div>
<divclass="line"><aname="l00251"></a><spanclass="lineno"> 251</span> <spanclass="comment">// point from the current sample</span></div>
<divclass="line"><aname="l00252"></a><spanclass="lineno"> 252</span> <spanclass="keywordflow">for</span> (k = 0; k < num_features; k++)</div>
<divclass="line"><aname="l00272"></a><spanclass="lineno"> 272</span> <spanclass="comment">// step 3b: update the weights of nodes in the</span></div>
<divclass="line"><aname="l00281"></a><spanclass="lineno"> 281</span> <spanclass="comment">/* you can enable the following normalization if needed.</span></div>
<divclass="line"><aname="l00282"></a><spanclass="lineno"> 282</span> <spanclass="comment"> personally, I found it detrimental to convergence */</span></div>
<divclass="line"><aname="l00286"></a><spanclass="lineno"> 286</span> <spanclass="comment">/* apply scaling inversely proportional to distance from the</span></div>
<divclass="line"><aname="l00287"></a><spanclass="lineno"> 287</span> <spanclass="comment"> current node */</span></div>
<divclass="line"><aname="l00294"></a><spanclass="lineno"> 294</span> <spanclass="keywordtype">double</span> *w = <aclass="code"href="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">kohonen_data_3d</a>(W, x, y, k);</div>
<divclass="line"><aname="l00295"></a><spanclass="lineno"> 295</span> <spanclass="comment">// update weights of nodes in the neighborhood</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d1/d6b/group__kohonen__2d_ga83abb572c60d202e100595a989dfe123_cgraph.svg"width="352"height="88"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00114"></a><spanclass="lineno"> 114</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i < num_points; i++) <spanclass="comment">// for each point in the array</span></div>
<divclass="line"><aname="l00116"></a><spanclass="lineno"> 116</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> j = 0; j < num_features; j++) <spanclass="comment">// for each feature in the array</span></div>
<divclass="line"><aname="l00118"></a><spanclass="lineno"> 118</span>  fprintf(fp, <spanclass="stringliteral">"%.4g"</span>, X[i][j]); <spanclass="comment">// print the feature value</span></div>
<divclass="line"><aname="l00119"></a><spanclass="lineno"> 119</span> <spanclass="keywordflow">if</span> (j < num_features - 1) <spanclass="comment">// if not the last feature</span></div>
<divclass="line"><aname="l00122"></a><spanclass="lineno"> 122</span> <spanclass="keywordflow">if</span> (i < num_points - 1) <spanclass="comment">// if not the last row</span></div>
<divclass="line"><aname="l00123"></a><spanclass="lineno"> 123</span>  fputc(<spanclass="charliteral">'\n'</span>, fp); <spanclass="comment">// start a new line</span></div>
<divclass="line"><aname="l00152"></a><spanclass="lineno"> 152</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i < W-><aclass="code"href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a>; i++) <spanclass="comment">// for each x</span></div>
<divclass="line"><aname="l00159"></a><spanclass="lineno"> 159</span> <spanclass="keywordtype">int</span> from_x = <aclass="code"href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(0, i - R);</div>
<divclass="line"><aname="l00160"></a><spanclass="lineno"> 160</span> <spanclass="keywordtype">int</span> to_x = <aclass="code"href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(W-><aclass="code"href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a>, i + R + 1);</div>
<divclass="line"><aname="l00169"></a><spanclass="lineno"> 169</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> m = from_y; m < to_y; m++) <spanclass="comment">// scan neighborhood in y</span></div>
<divclass="line"><aname="l00171"></a><spanclass="lineno"> 171</span> <spanclass="keywordtype">double</span> d = 0.f;</div>
<divclass="line"><aname="l00172"></a><spanclass="lineno"> 172</span> <spanclass="keywordflow">for</span> (k = 0; k < W-><aclass="code"href="../../d8/db8/structkohonen__array__3d.html#a160f14830bdfbbf9f422f382ee754dbf">dim3</a>; k++) <spanclass="comment">// for each feature</span></div>
<divclass="line"><aname="l00184"></a><spanclass="lineno"> 184</span>  distance /= R * R; <spanclass="comment">// mean distance from neighbors</span></div>
<divclass="line"><aname="l00185"></a><spanclass="lineno"> 185</span>  fprintf(fp, <spanclass="stringliteral">"%.4g"</span>, distance); <spanclass="comment">// print the mean separation</span></div>
<divclass="line"><aname="l00186"></a><spanclass="lineno"> 186</span> <spanclass="keywordflow">if</span> (j < W->dim2 - 1) <spanclass="comment">// if not the last column</span></div>
<divclass="line"><aname="l00189"></a><spanclass="lineno"> 189</span> <spanclass="keywordflow">if</span> (i < W->dim1 - 1) <spanclass="comment">// if not the last row</span></div>
<divclass="line"><aname="l00190"></a><spanclass="lineno"> 190</span>  fputc(<spanclass="charliteral">'\n'</span>, fp); <spanclass="comment">// start a new line</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d1/d6b/group__kohonen__2d_ga49d35f68f5d11d8ef6f8cce0d0e7bcba_cgraph.svg"width="290"height="38"><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 -->
<divclass="ttc"id="astructkohonen__array__3d_html_ad546baa2e81c6196d5f1dc0fe2e5bd59"><divclass="ttname"><ahref="../../d8/db8/structkohonen__array__3d.html#ad546baa2e81c6196d5f1dc0fe2e5bd59">kohonen_array_3d::data</a></div><divclass="ttdeci">double * data</div><divclass="ttdoc">pointer to data</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:52</div></div>
<divclass="ttc"id="astructkohonen__array__3d_html_a888d7e007b38c91c7933e12a9566af1d"><divclass="ttname"><ahref="../../d8/db8/structkohonen__array__3d.html#a888d7e007b38c91c7933e12a9566af1d">kohonen_array_3d::dim2</a></div><divclass="ttdeci">int dim2</div><divclass="ttdoc">lengths of second dimension</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:50</div></div>
<divclass="ttc"id="astructkohonen__array__3d_html_a160f14830bdfbbf9f422f382ee754dbf"><divclass="ttname"><ahref="../../d8/db8/structkohonen__array__3d.html#a160f14830bdfbbf9f422f382ee754dbf">kohonen_array_3d::dim3</a></div><divclass="ttdeci">int dim3</div><divclass="ttdoc">lengths of thirddimension</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:51</div></div>
<divclass="ttc"id="agroup__kohonen__2d_html_gadc22d512c00a9f5799ee067f4fb90b4b"><divclass="ttname"><ahref="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">get_min_2d</a></div><divclass="ttdeci">void get_min_2d(double **X, int N, double *val, int *x_idx, int *y_idx)</div><divclass="ttdoc">Get minimum value and index of the value in a matrix.</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:204</div></div>
<divclass="ttc"id="agroup__kohonen__2d_html_gac6afabdc09a49a433ee19d8a9486056d"><divclass="ttname"><ahref="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a></div><divclass="ttdeci">#define min(a, b)</div><divclass="ttdoc">shorthand for minimum value</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:43</div></div>
<divclass="ttc"id="agroup__kohonen__2d_html_gaffe776513b24d84b39af8ab0930fef7f"><divclass="ttname"><ahref="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a></div><divclass="ttdeci">#define max(a, b)</div><divclass="ttdoc">shorthand for maximum value</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:39</div></div>
<divclass="ttc"id="astructkohonen__array__3d_html_a16720581653fa9a34d1029e7229a7377"><divclass="ttname"><ahref="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">kohonen_array_3d::dim1</a></div><divclass="ttdeci">int dim1</div><divclass="ttdoc">lengths of first dimension</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:49</div></div>
<divclass="ttc"id="agroup__kohonen__2d_html_ga83abb572c60d202e100595a989dfe123"><divclass="ttname"><ahref="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">kohonen_update_weights</a></div><divclass="ttdeci">double kohonen_update_weights(const double *X, struct kohonen_array_3d *W, double **D, int num_out, int num_features, double alpha, int R)</div><divclass="ttdoc">Update weights of the SOM using Kohonen algorithm.</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:234</div></div>
<divclass="ttc"id="agroup__kohonen__2d_html_ga8df35f04c1762a01dcf108fa13b897d6"><divclass="ttname"><ahref="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">kohonen_data_3d</a></div><divclass="ttdeci">double * kohonen_data_3d(const struct kohonen_array_3d *arr, int x, int y, int z)</div><divclass="ttdoc">Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by:</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:67</div></div>
<!-- start footer part -->
<divid="nav-path"class="navpath"><!-- id is needed for treeview function! -->