Include dependency graph for kohonen_som_topology.c:</div>
<divclass="dyncontent">
<divclass="center"><iframescrolling="no"frameborder="0"src="../../dc/dd4/kohonen__som__topology_8c__incl.svg"width="331"height="127"><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="../../d0/dc8/structarray__3d.html#details">More...</a><br/></td></tr>
<trclass="memitem:a7237d2cf3b3f9d034477268c1d6631da"><tdclass="memItemLeft"align="right"valign="top">double * </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da">data_3d</a> (const struct <aclass="el"href="../../d0/dc8/structarray__3d.html">array_3d</a> *arr, int x, int y, int z)</td></tr>
<trclass="memdesc:a7237d2cf3b3f9d034477268c1d6631da"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by: <ahref="../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da">More...</a><br/></td></tr>
<trclass="memitem:af5ce14f026d6d231bef29161bac2b485"><tdclass="memItemLeft"align="right"valign="top">double </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a> (double a, double b)</td></tr>
<trclass="memdesc:af5ce14f026d6d231bef29161bac2b485"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Helper function to generate a random number in a given interval. <ahref="../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485">More...</a><br/></td></tr>
<trclass="memitem:a6824dc6d973eb3339af7aef5fea78b0c"><tdclass="memItemLeft"align="right"valign="top">int </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a> (const char *fname, double **X, int num_points, int num_features)</td></tr>
<trclass="memdesc:a6824dc6d973eb3339af7aef5fea78b0c"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Save a given n-dimensional data martix to file. <ahref="../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c">More...</a><br/></td></tr>
<trclass="memdesc:adee1bc3871c881f6e04cb9e0b1d01158"><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="../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158">More...</a><br/></td></tr>
<trclass="memitem:adc22d512c00a9f5799ee067f4fb90b4b"><tdclass="memItemLeft"align="right"valign="top">void </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d2/df6/kohonen__som__topology_8c.html#adc22d512c00a9f5799ee067f4fb90b4b">get_min_2d</a> (double **X, int <aclass="el"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, double *val, int *x_idx, int *y_idx)</td></tr>
<trclass="memdesc:adc22d512c00a9f5799ee067f4fb90b4b"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Get minimum value and index of the value in a matrix. <ahref="../../d2/df6/kohonen__som__topology_8c.html#adc22d512c00a9f5799ee067f4fb90b4b">More...</a><br/></td></tr>
<trclass="memdesc:ad139de302fa8135a433d9c1bf971b13b"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Update weights of the SOM using Kohonen algorithm. <ahref="../../d2/df6/kohonen__som__topology_8c.html#ad139de302fa8135a433d9c1bf971b13b">More...</a><br/></td></tr>
<trclass="memitem:a1983dc0f35e734015772e9f8c0ff19f5"><tdclass="memItemLeft"align="right"valign="top">void </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d2/df6/kohonen__som__topology_8c.html#a1983dc0f35e734015772e9f8c0ff19f5">kohonen_som</a> (double **X, struct <aclass="el"href="../../d0/dc8/structarray__3d.html">array_3d</a> *W, int num_samples, int num_features, int num_out, double alpha_min)</td></tr>
<trclass="memdesc:a1983dc0f35e734015772e9f8c0ff19f5"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset. <ahref="../../d2/df6/kohonen__som__topology_8c.html#a1983dc0f35e734015772e9f8c0ff19f5">More...</a><br/></td></tr>
<trclass="memdesc:adb5ded007be1fd666fab9affe6764018"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Creates a random set of points distributed in four clusters in 3D space with centroids at the points. <ahref="../../d2/df6/kohonen__som__topology_8c.html#adb5ded007be1fd666fab9affe6764018">More...</a><br/></td></tr>
<trclass="memdesc:a1440a7779ac56f47a3f355ce4a8c7da0"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Test that creates a random set of points distributed in four clusters in 2D space and trains an SOM that finds the topological pattern. <ahref="../../d2/df6/kohonen__som__topology_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">More...</a><br/></td></tr>
<trclass="memdesc:ad9e25202bb8b481461f932668f249dbc"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Creates a random set of points distributed in four clusters in 3D space with centroids at the points. <ahref="../../d2/df6/kohonen__som__topology_8c.html#ad9e25202bb8b481461f932668f249dbc">More...</a><br/></td></tr>
<trclass="memdesc:a0283886819c7c140a023582b7269e2d0"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Test that creates a random set of points distributed in 4 clusters in 3D space and trains an SOM that finds the topological pattern. <ahref="../../d2/df6/kohonen__som__topology_8c.html#a0283886819c7c140a023582b7269e2d0">More...</a><br/></td></tr>
<trclass="memdesc:a5bb02a8322d717ead1b11182c5f02a3a"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Creates a random set of points distributed in four clusters in 3D space with centroids at the points. <ahref="../../d2/df6/kohonen__som__topology_8c.html#a5bb02a8322d717ead1b11182c5f02a3a">More...</a><br/></td></tr>
<trclass="memdesc:a6d0455dd5c30adda100e95f0423c786e"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Test that creates a random set of points distributed in eight clusters in 3D space and trains an SOM that finds the topological pattern. <ahref="../../d2/df6/kohonen__som__topology_8c.html#a6d0455dd5c30adda100e95f0423c786e">More...</a><br/></td></tr>
<trclass="memdesc:a2256c10b16edba377b64a44b6c656908"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Convert clock cycle difference to time in seconds. <ahref="../../d2/df6/kohonen__som__topology_8c.html#a2256c10b16edba377b64a44b6c656908">More...</a><br/></td></tr>
<dlclass="section author"><dt>Author</dt><dd><ahref="https://github.com/kvedala">Krishna Vedala</a> This example implements a powerful unsupervised learning algorithm called as a self organizing map. The algorithm creates a connected network of weights that closely follows the given data points. This thus creates a topological map of the given data i.e., it maintains the relationship between varipus data points in a much higher dimesional space by creating an equivalent in a 2-dimensional space. <imgsrc="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/machine_learning/kohonen/2D_Kohonen_SOM.svg"alt="Trained topological maps for the test cases in the program"style="pointer-events: none;"class="inline"/></dd></dl>
<dlclass="section warning"><dt>Warning</dt><dd>MSVC 2019 compiler generates code that does not execute as expected. However, MinGW, Clang for GCC and Clang for MSVC compilers on windows perform as expected. Any insights and suggestions should be directed to the author. </dd></dl>
<tr><tdclass="paramdir">[in]</td><tdclass="paramname">arr</td><td>pointer to <aclass="el"href="../../d0/dc8/structarray__3d.html"title="to store info regarding 3D arrays">array_3d</a> structure </td></tr>
<divclass="line"><aname="l00201"></a><spanclass="lineno"> 201</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i <<aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++) <spanclass="comment">// traverse each x-index</span></div>
<divclass="line"><aname="l00205"></a><spanclass="lineno"> 205</span> <spanclass="keywordflow">if</span> (X[i][j] < val[0]) <spanclass="comment">// if a lower value is found</span></div>
<divclass="line"><aname="l00206"></a><spanclass="lineno"> 206</span>  { <spanclass="comment">// save the value and its index</span></div>
<divclass="line"><aname="l00311"></a><spanclass="lineno"> 311</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i < num_out; i++)</div>
<divclass="line"><aname="l00314"></a><spanclass="lineno"> 314</span> <spanclass="keywordtype">double</span> dmin = 1.f; <spanclass="comment">// average minimum distance of all samples</span></div>
<divclass="line"><aname="l00321"></a><spanclass="lineno"> 321</span> <spanclass="comment">// Loop for each sample pattern in the data set</span></div>
<divclass="line"><aname="l00324"></a><spanclass="lineno"> 324</span> <spanclass="comment">// update weights for the current input pattern sample</span></div>
<divclass="line"><aname="l00329"></a><spanclass="lineno"> 329</span> <spanclass="comment">// every 20th iteration, reduce the neighborhood range</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d2/df6/kohonen__som__topology_8c_a1983dc0f35e734015772e9f8c0ff19f5_cgraph.svg"width="412"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="l00107"></a><spanclass="lineno"> 107</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="l00109"></a><spanclass="lineno"> 109</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="l00111"></a><spanclass="lineno"> 111</span>  fprintf(fp, <spanclass="stringliteral">"%.4g"</span>, X[i][j]); <spanclass="comment">// print the feature value</span></div>
<divclass="line"><aname="l00112"></a><spanclass="lineno"> 112</span> <spanclass="keywordflow">if</span> (j < num_features - 1) <spanclass="comment">// if not the last feature</span></div>
<divclass="line"><aname="l00115"></a><spanclass="lineno"> 115</span> <spanclass="keywordflow">if</span> (i < num_points - 1) <spanclass="comment">// if not the last row</span></div>
<divclass="line"><aname="l00116"></a><spanclass="lineno"> 116</span>  fputc(<spanclass="charliteral">'\n'</span>, fp); <spanclass="comment">// start a new line</span></div>
<divclass="line"><aname="l00145"></a><spanclass="lineno"> 145</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i < W-><aclass="code"href="../../d0/dc8/structarray__3d.html#ad5d3ee546eea3c05dc8f0dc90ec194f4">dim1</a>; i++) <spanclass="comment">// for each x</span></div>
<divclass="line"><aname="l00152"></a><spanclass="lineno"> 152</span> <spanclass="keywordtype">int</span> from_x = <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(0, i - R);</div>
<divclass="line"><aname="l00153"></a><spanclass="lineno"> 153</span> <spanclass="keywordtype">int</span> to_x = <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#ac6afabdc09a49a433ee19d8a9486056d">min</a>(W-><aclass="code"href="../../d0/dc8/structarray__3d.html#ad5d3ee546eea3c05dc8f0dc90ec194f4">dim1</a>, i + R + 1);</div>
<divclass="line"><aname="l00162"></a><spanclass="lineno"> 162</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="l00164"></a><spanclass="lineno"> 164</span> <spanclass="keywordtype">double</span> d = 0.f;</div>
<divclass="line"><aname="l00165"></a><spanclass="lineno"> 165</span> <spanclass="keywordflow">for</span> (k = 0; k < W-><aclass="code"href="../../d0/dc8/structarray__3d.html#ad823bd5424a0fc158da52a0497d8c10a">dim3</a>; k++) <spanclass="comment">// for each feature</span></div>
<divclass="line"><aname="l00177"></a><spanclass="lineno"> 177</span>  distance /= R * R; <spanclass="comment">// mean distance from neighbors</span></div>
<divclass="line"><aname="l00178"></a><spanclass="lineno"> 178</span>  fprintf(fp, <spanclass="stringliteral">"%.4g"</span>, distance); <spanclass="comment">// print the mean separation</span></div>
<divclass="line"><aname="l00179"></a><spanclass="lineno"> 179</span> <spanclass="keywordflow">if</span> (j < W->dim2 - 1) <spanclass="comment">// if not the last column</span></div>
<divclass="line"><aname="l00182"></a><spanclass="lineno"> 182</span> <spanclass="keywordflow">if</span> (i < W->dim1 - 1) <spanclass="comment">// if not the last row</span></div>
<divclass="line"><aname="l00183"></a><spanclass="lineno"> 183</span>  fputc(<spanclass="charliteral">'\n'</span>, fp); <spanclass="comment">// start a new line</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d2/df6/kohonen__som__topology_8c_adee1bc3871c881f6e04cb9e0b1d01158_cgraph.svg"width="234"height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00410"></a><spanclass="lineno"> 410</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i <<aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++) <spanclass="comment">// loop till max(N, num_out)</span></div>
<divclass="line"><aname="l00412"></a><spanclass="lineno"> 412</span> <spanclass="keywordflow">if</span> (i <<aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>) <spanclass="comment">// only add new arrays if i < N</span></div>
<divclass="line"><aname="l00414"></a><spanclass="lineno"> 414</span> <spanclass="keywordflow">if</span> (i < num_out) <spanclass="comment">// only add new arrays if i < num_out</span></div>
<divclass="line"><aname="l00416"></a><spanclass="lineno"> 416</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> k = 0; k < num_out; k++)</div>
<divclass="line"><aname="l00431"></a><spanclass="lineno"> 431</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#adb5ded007be1fd666fab9affe6764018">test_2d_classes</a>(X, <aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); <spanclass="comment">// create test data around circumference of a circle</span></div>
<divclass="line"><aname="l00432"></a><spanclass="lineno"> 432</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a>(<spanclass="stringliteral">"test1.csv"</span>, X, <aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features); <spanclass="comment">// save test data points</span></div>
<divclass="line"><aname="l00433"></a><spanclass="lineno"> 433</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158">save_u_matrix</a>(<spanclass="stringliteral">"w11.csv"</span>, &W); <spanclass="comment">// save initial random weights</span></div>
<divclass="line"><aname="l00435"></a><spanclass="lineno"> 435</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158">save_u_matrix</a>(<spanclass="stringliteral">"w12.csv"</span>, &W); <spanclass="comment">// save the resultant weights</span></div>
<divclass="line"><aname="l00437"></a><spanclass="lineno"> 437</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i <<aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++) free(X[i]);</div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d2/df6/kohonen__som__topology_8c_a1440a7779ac56f47a3f355ce4a8c7da0_cgraph.svg"width="526"height="270"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00499"></a><spanclass="lineno"> 499</span> <spanclass="comment">// 3D space, hence size = number of rows * 3</span></div>
<divclass="line"><aname="l00510"></a><spanclass="lineno"> 510</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i <<aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++) <spanclass="comment">// loop till max(N, num_out)</span></div>
<divclass="line"><aname="l00512"></a><spanclass="lineno"> 512</span> <spanclass="keywordflow">if</span> (i <<aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>) <spanclass="comment">// only add new arrays if i < N</span></div>
<divclass="line"><aname="l00514"></a><spanclass="lineno"> 514</span> <spanclass="keywordflow">if</span> (i < num_out) <spanclass="comment">// only add new arrays if i < num_out</span></div>
<divclass="line"><aname="l00516"></a><spanclass="lineno"> 516</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> k = 0; k < num_out; k++)</div>
<divclass="line"><aname="l00522"></a><spanclass="lineno"> 522</span>  { <spanclass="comment">// preallocate with random initial weights</span></div>
<divclass="line"><aname="l00523"></a><spanclass="lineno"> 523</span> <spanclass="keywordtype">double</span> *w = <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da">data_3d</a>(&W, i, k, j);</div>
<divclass="line"><aname="l00530"></a><spanclass="lineno"> 530</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#ad9e25202bb8b481461f932668f249dbc">test_3d_classes1</a>(X, <aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); <spanclass="comment">// create test data</span></div>
<divclass="line"><aname="l00531"></a><spanclass="lineno"> 531</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a>(<spanclass="stringliteral">"test2.csv"</span>, X, <aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features); <spanclass="comment">// save test data points</span></div>
<divclass="line"><aname="l00532"></a><spanclass="lineno"> 532</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158">save_u_matrix</a>(<spanclass="stringliteral">"w21.csv"</span>, &W); <spanclass="comment">// save initial random weights</span></div>
<divclass="line"><aname="l00534"></a><spanclass="lineno"> 534</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158">save_u_matrix</a>(<spanclass="stringliteral">"w22.csv"</span>, &W); <spanclass="comment">// save the resultant weights</span></div>
<divclass="line"><aname="l00536"></a><spanclass="lineno"> 536</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i <<aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++) free(X[i]);</div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d2/df6/kohonen__som__topology_8c_a0283886819c7c140a023582b7269e2d0_cgraph.svg"width="532"height="271"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00611"></a><spanclass="lineno"> 611</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i <<aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++) <spanclass="comment">// loop till max(N, num_out)</span></div>
<divclass="line"><aname="l00613"></a><spanclass="lineno"> 613</span> <spanclass="keywordflow">if</span> (i <<aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>) <spanclass="comment">// only add new arrays if i < N</span></div>
<divclass="line"><aname="l00615"></a><spanclass="lineno"> 615</span> <spanclass="keywordflow">if</span> (i < num_out) <spanclass="comment">// only add new arrays if i < num_out</span></div>
<divclass="line"><aname="l00617"></a><spanclass="lineno"> 617</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> k = 0; k < num_out; k++)</div>
<divclass="line"><aname="l00632"></a><spanclass="lineno"> 632</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#a5bb02a8322d717ead1b11182c5f02a3a">test_3d_classes2</a>(X, <aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); <spanclass="comment">// create test data around the lamniscate</span></div>
<divclass="line"><aname="l00633"></a><spanclass="lineno"> 633</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a>(<spanclass="stringliteral">"test3.csv"</span>, X, <aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features); <spanclass="comment">// save test data points</span></div>
<divclass="line"><aname="l00634"></a><spanclass="lineno"> 634</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158">save_u_matrix</a>(<spanclass="stringliteral">"w31.csv"</span>, &W); <spanclass="comment">// save initial random weights</span></div>
<divclass="line"><aname="l00636"></a><spanclass="lineno"> 636</span> <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158">save_u_matrix</a>(<spanclass="stringliteral">"w32.csv"</span>, &W); <spanclass="comment">// save the resultant weights</span></div>
<divclass="line"><aname="l00638"></a><spanclass="lineno"> 638</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i <<aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++) free(X[i]);</div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d2/df6/kohonen__som__topology_8c_a6d0455dd5c30adda100e95f0423c786e_cgraph.svg"width="532"height="271"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00355"></a><spanclass="lineno"> 355</span> <spanclass="keyword">const</span><spanclass="keywordtype">double</span> R = 0.3; <spanclass="comment">// radius of cluster</span></div>
<divclass="line"><aname="l00369"></a><spanclass="lineno"> 369</span> <spanclass="keywordflow">for</span> (i = 0; i <<aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
<divclass="line"><aname="l00372"></a><spanclass="lineno"> 372</span>  rand() % num_classes; <spanclass="comment">// select a random class for the point</span></div>
<divclass="line"><aname="l00374"></a><spanclass="lineno"> 374</span> <spanclass="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d2/df6/kohonen__som__topology_8c_adb5ded007be1fd666fab9affe6764018_cgraph.svg"width="248"height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00454"></a><spanclass="lineno"> 454</span> <spanclass="keyword">const</span><spanclass="keywordtype">double</span> R = 0.2; <spanclass="comment">// radius of cluster</span></div>
<divclass="line"><aname="l00468"></a><spanclass="lineno"> 468</span> <spanclass="keywordflow">for</span> (i = 0; i <<aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
<divclass="line"><aname="l00471"></a><spanclass="lineno"> 471</span>  rand() % num_classes; <spanclass="comment">// select a random class for the point</span></div>
<divclass="line"><aname="l00473"></a><spanclass="lineno"> 473</span> <spanclass="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d2/df6/kohonen__som__topology_8c_ad9e25202bb8b481461f932668f249dbc_cgraph.svg"width="255"height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00553"></a><spanclass="lineno"> 553</span> <spanclass="keyword">const</span><spanclass="keywordtype">double</span> R = 0.2; <spanclass="comment">// radius of cluster</span></div>
<divclass="line"><aname="l00571"></a><spanclass="lineno"> 571</span> <spanclass="keywordflow">for</span> (i = 0; i <<aclass="code"href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
<divclass="line"><aname="l00574"></a><spanclass="lineno"> 574</span>  rand() % num_classes; <spanclass="comment">// select a random class for the point</span></div>
<divclass="line"><aname="l00576"></a><spanclass="lineno"> 576</span> <spanclass="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d2/df6/kohonen__som__topology_8c_a5bb02a8322d717ead1b11182c5f02a3a_cgraph.svg"width="255"height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00242"></a><spanclass="lineno"> 242</span> <spanclass="comment">// compute Euclidian distance of each output</span></div>
<divclass="line"><aname="l00243"></a><spanclass="lineno"> 243</span> <spanclass="comment">// point from the current sample</span></div>
<divclass="line"><aname="l00244"></a><spanclass="lineno"> 244</span> <spanclass="keywordflow">for</span> (k = 0; k < num_features; k++)</div>
<divclass="line"><aname="l00264"></a><spanclass="lineno"> 264</span> <spanclass="comment">// step 3b: update the weights of nodes in the</span></div>
<divclass="line"><aname="l00273"></a><spanclass="lineno"> 273</span> <spanclass="comment">/* you can enable the following normalization if needed.</span></div>
<divclass="line"><aname="l00274"></a><spanclass="lineno"> 274</span> <spanclass="comment"> personally, I found it detrimental to convergence */</span></div>
<divclass="line"><aname="l00278"></a><spanclass="lineno"> 278</span> <spanclass="comment">/* apply scaling inversely proportional to distance from the</span></div>
<divclass="line"><aname="l00279"></a><spanclass="lineno"> 279</span> <spanclass="comment"> current node */</span></div>
<divclass="line"><aname="l00286"></a><spanclass="lineno"> 286</span> <spanclass="keywordtype">double</span> *w = <aclass="code"href="../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da">data_3d</a>(W, x, y, k);</div>
<divclass="line"><aname="l00287"></a><spanclass="lineno"> 287</span> <spanclass="comment">// update weights of nodes in the neighborhood</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d2/df6/kohonen__som__topology_8c_ad139de302fa8135a433d9c1bf971b13b_cgraph.svg"width="262"height="88"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="ttc"id="akohonen__som__topology_8c_html_ac6afabdc09a49a433ee19d8a9486056d"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#ac6afabdc09a49a433ee19d8a9486056d">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:36</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_ad139de302fa8135a433d9c1bf971b13b"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#ad139de302fa8135a433d9c1bf971b13b">update_weights</a></div><divclass="ttdeci">double update_weights(const double *X, struct 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:227</div></div>
<divclass="ttc"id="astructarray__3d_html_ac8967b2c72769f839c09fb4cebb4339f"><divclass="ttname"><ahref="../../d0/dc8/structarray__3d.html#ac8967b2c72769f839c09fb4cebb4339f">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:45</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_a7237d2cf3b3f9d034477268c1d6631da"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da">data_3d</a></div><divclass="ttdeci">double * data_3d(const struct 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:60</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_ad9e25202bb8b481461f932668f249dbc"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#ad9e25202bb8b481461f932668f249dbc">test_3d_classes1</a></div><divclass="ttdeci">void test_3d_classes1(double *const *data, int N)</div><divclass="ttdoc">Creates a random set of points distributed in four clusters in 3D space with centroids at the points.</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:452</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_a5bb02a8322d717ead1b11182c5f02a3a"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#a5bb02a8322d717ead1b11182c5f02a3a">test_3d_classes2</a></div><divclass="ttdeci">void test_3d_classes2(double *const *data, int N)</div><divclass="ttdoc">Creates a random set of points distributed in four clusters in 3D space with centroids at the points.</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:551</div></div>
<divclass="ttc"id="aproblem__13_2sol1_8c_html_a0240ac851181b84ac374872dc5434ee4"><divclass="ttname"><ahref="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a></div><divclass="ttdeci">#define N</div><divclass="ttdoc">number of digits of the large number</div><divclass="ttdef"><b>Definition:</b> sol1.c:109</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_adee1bc3871c881f6e04cb9e0b1d01158"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158">save_u_matrix</a></div><divclass="ttdeci">int save_u_matrix(const char *fname, struct array_3d *W)</div><divclass="ttdoc">Create the distance matrix or U-matrix from the trained weights and save to disk.</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:132</div></div>
<divclass="ttc"id="astructarray__3d_html_ad823bd5424a0fc158da52a0497d8c10a"><divclass="ttname"><ahref="../../d0/dc8/structarray__3d.html#ad823bd5424a0fc158da52a0497d8c10a">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:44</div></div>
<divclass="ttc"id="astructarray__3d_html_ad5d3ee546eea3c05dc8f0dc90ec194f4"><divclass="ttname"><ahref="../../d0/dc8/structarray__3d.html#ad5d3ee546eea3c05dc8f0dc90ec194f4">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:42</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_adc22d512c00a9f5799ee067f4fb90b4b"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#adc22d512c00a9f5799ee067f4fb90b4b">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:197</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_af5ce14f026d6d231bef29161bac2b485"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a></div><divclass="ttdeci">double _random(double a, double b)</div><divclass="ttdoc">Helper function to generate a random number in a given interval.</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:80</div></div>
<divclass="ttc"id="astructarray__3d_html"><divclass="ttname"><ahref="../../d0/dc8/structarray__3d.html">array_3d</a></div><divclass="ttdoc">to store info regarding 3D arrays</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:41</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_a6824dc6d973eb3339af7aef5fea78b0c"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a></div><divclass="ttdeci">int save_2d_data(const char *fname, double **X, int num_points, int num_features)</div><divclass="ttdoc">Save a given n-dimensional data martix to file.</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:95</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_affe776513b24d84b39af8ab0930fef7f"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f">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:32</div></div>
<divclass="ttc"id="astructarray__3d_html_aa431cc0a41fd379270dbd2b0e3ac1b45"><divclass="ttname"><ahref="../../d0/dc8/structarray__3d.html#aa431cc0a41fd379270dbd2b0e3ac1b45">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:43</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_a1983dc0f35e734015772e9f8c0ff19f5"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#a1983dc0f35e734015772e9f8c0ff19f5">kohonen_som</a></div><divclass="ttdeci">void kohonen_som(double **X, struct array_3d *W, int num_samples, int num_features, int num_out, double alpha_min)</div><divclass="ttdoc">Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given...</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:306</div></div>
<divclass="ttc"id="akohonen__som__topology_8c_html_adb5ded007be1fd666fab9affe6764018"><divclass="ttname"><ahref="../../d2/df6/kohonen__som__topology_8c.html#adb5ded007be1fd666fab9affe6764018">test_2d_classes</a></div><divclass="ttdeci">void test_2d_classes(double *const *data, int N)</div><divclass="ttdoc">Creates a random set of points distributed in four clusters in 3D space with centroids at the points.</div><divclass="ttdef"><b>Definition:</b> kohonen_som_topology.c:353</div></div>