Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
Kohonen self organizing map (data tracing) More...
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Functions | |
double | _random (double a, double b) |
Helper function to generate a random number in a given interval. More... | |
int | save_nd_data (const char *fname, double **X, int num_points, int num_features) |
Save a given n-dimensional data martix to file. More... | |
void | kohonen_get_min_1d (double const *X, int N, double *val, int *idx) |
Get minimum value and index of the value in a vector. More... | |
void | kohonen_update_weights (double const *x, double *const *W, double *D, int num_out, int num_features, double alpha, int R) |
Update weights of the SOM using Kohonen algorithm. More... | |
void | kohonen_som_tracer (double **X, double *const *W, int num_samples, int num_features, int num_out, double alpha_min) |
Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset. More... | |
void | test_circle (double *const *data, int N) |
Creates a random set of points distributed near the circumference of a circle and trains an SOM that finds that circular pattern. More... | |
void | test1 () |
Test that creates a random set of points distributed near the circumference of a circle and trains an SOM that finds that circular pattern. More... | |
void | test_lamniscate (double *const *data, int N) |
Creates a random set of points distributed near the locus of the Lamniscate of Gerono. More... | |
void | test2 () |
Test that creates a random set of points distributed near the locus of the Lamniscate of Gerono and trains an SOM that finds that circular pattern. More... | |
void | test_3d_classes (double *const *data, int N) |
Creates a random set of points distributed in four clusters in 3D space with centroids at the points. More... | |
void | test3 () |
Test that creates a random set of points distributed in six clusters in 3D space. More... | |
double | get_clock_diff (clock_t start_t, clock_t end_t) |
Convert clock cycle difference to time in seconds. More... | |
int | main (int argc, char **argv) |
Main function. More... | |
Kohonen self organizing map (data tracing)
This example implements a powerful self organizing map algorithm. The algorithm creates a connected network of weights that closely follows the given data points. This creates a chain of nodes that resembles the given input shape.
double get_clock_diff | ( | clock_t | start_t, |
clock_t | end_t | ||
) |
Convert clock cycle difference to time in seconds.
[in] | start_t | start clock |
[in] | end_t | end clock |
int main | ( | int | argc, |
char ** | argv | ||
) |
Main function.
void test1 | ( | ) |
Test that creates a random set of points distributed near the circumference of a circle and trains an SOM that finds that circular pattern.
The following CSV files are created to validate the execution:
test1.csv
: random test samples points with a circular patternw11.csv
: initial random mapw12.csv
: trained SOM mapThe outputs can be readily plotted in gnuplot using the following snippet
void test2 | ( | ) |
Test that creates a random set of points distributed near the locus of the Lamniscate of Gerono and trains an SOM that finds that circular pattern.
The following CSV files are created to validate the execution:
test2.csv
: random test samples points with a lamniscate patternw21.csv
: initial random mapw22.csv
: trained SOM mapThe outputs can be readily plotted in gnuplot using the following snippet
void test3 | ( | ) |
Test that creates a random set of points distributed in six clusters in 3D space.
The following CSV files are created to validate the execution:
test3.csv
: random test samples points with a circular patternw31.csv
: initial random mapw32.csv
: trained SOM mapThe outputs can be readily plotted in gnuplot using the following snippet
void test_3d_classes | ( | double *const * | data, |
int | N | ||
) |
Creates a random set of points distributed in four clusters in 3D space with centroids at the points.
[out] | data | matrix to store data in |
[in] | N | number of points required |
void test_circle | ( | double *const * | data, |
int | N | ||
) |
Creates a random set of points distributed near the circumference of a circle and trains an SOM that finds that circular pattern.
The generating function is
\begin{eqnarray*} r &\in& [1-\delta r, 1+\delta r)\\ \theta &\in& [0, 2\pi)\\ x &=& r\cos\theta\\ y &=& r\sin\theta \end{eqnarray*}
[out] | data | matrix to store data in |
[in] | N | number of points required |
void test_lamniscate | ( | double *const * | data, |
int | N | ||
) |
Creates a random set of points distributed near the locus of the Lamniscate of Gerono.
\begin{eqnarray*} \delta r &=& 0.2\\ \delta x &\in& [-\delta r, \delta r)\\ \delta y &\in& [-\delta r, \delta r)\\ \theta &\in& [0, \pi)\\ x &=& \delta x + \cos\theta\\ y &=& \delta y + \frac{\sin(2\theta)}{2} \end{eqnarray*}
[out] | data | matrix to store data in |
[in] | N | number of points required |