Machine Learning Algorithms

## Files

file | adaline_learning.cpp
| Adaptive Linear Neuron (ADALINE) implementation
file | kohonen_som_topology.cpp
| Kohonen self organizing map (topological map)
file | kohonen_som_trace.cpp
| Kohonen self organizing map (data tracing)

## Functions

double | _random (double a, double b)
int | save_2d_data (const char *fname, const std::vector< std::valarray< double >> &X)
void | get_min_2d (const std::vector< std::valarray< double >> &X, double *val, int *x_idx, int *y_idx)
int | save_nd_data (const char *fname, const std::vector< std::valarray< double >> &X)

## Variables

constexpr int | MAX_ITER = 500

## Detailed Description

## Function Documentation

## _random()

double _random | ( | double | a,
|  | double | b
| ) |  |

Helper function to generate a random number in a given interval.
Steps:

1. `r1 = rand() % 100` gets a random number between 0 and 99
2. `r2 = r1 / 100` converts random number to be between 0 and 0.99
3. scale and offset the random number to given range of \([a,b]\)

Parameters

  [in] | a | lower limit
  [in] | b | upper limit

Returns
  random number in the range \([a,b]\)

{
return ((b - a) * (std::rand() % 100) / 100.f) + a;
}
std::rand
T rand(T... args)
Here is the call graph for this function:

## get_min_2d()

void get_min_2d | ( | const std::vector< std::valarray< double >> & | X,
|  | double * | val,
|  | int * | x_idx,
|  | int * | y_idx
| ) |  |

Get minimum value and index of the value in a matrix

Parameters

  [in] | X | matrix to search
  [in] | N | number of points in the vector
  [out] | val | minimum value found
  [out] | idx_x | x-index where minimum value was found
  [out] | idx_y | y-index where minimum value was found

{
val[0] = INFINITY; // initial min value
size_t N = X.size();
for (int i = 0; i < N; i++) { // traverse each x-index
auto result = std::min_element(std::begin(X[i]), std::end(X[i]));
double d_min = *result;
std::ptrdiff_t j = std::distance(std::begin(X[i]), result);
if (d_min < val[0]) { // if a lower value is found
// save the value and its index
x_idx[0] = i;
y_idx[0] = j;
val[0] = d_min;
}
}
}
std::begin
T begin(T... args)
std::distance
T distance(T... args)
std::end
T end(T... args)
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
std::min_element
T min_element(T... args)
std::ptrdiff_t
std::vector::size
T size(T... args)
Here is the call graph for this function:

## save_2d_data()

int save_2d_data | ( | const char * | fname,
|  | const std::vector< std::valarray< double >> & | X
| ) |  |

Save a given n-dimensional data martix to file.

Parameters

  [in] | fname | filename to save in (gets overwriten without confirmation)
  [in] | X | matrix to save

Returns
  0 if all ok
  -1 if file creation failed

{
size_t num_points = X.size(); // number of rows
size_t num_features = X[0].size(); // number of columns
std::ofstream fp;
fp.open(fname);
if (!fp.is_open()) {
// error with opening file to write
std::cerr << "Error opening file " << fname << ": "
<< std::strerror(errno) << "\n";
return -1;
}
// for each point in the array
for (int i = 0; i < num_points; i++) {
// for each feature in the array
for (int j = 0; j < num_features; j++) {
fp << X[i][j]; // print the feature value
if (j < num_features - 1) { // if not the last feature
fp << ","; // suffix comma
}
}
if (i < num_points - 1) { // if not the last row
fp << "\n"; // start a new line
}
}
fp.close();
return 0;
}
std::ofstream
std::cerr
std::ofstream::close
T close(T... args)
std::ofstream::is_open
T is_open(T... args)
std::ofstream::open
T open(T... args)
std::strerror
T strerror(T... args)
Here is the call graph for this function:

## save_nd_data()

int save_nd_data | ( | const char * | fname,
|  | const std::vector< std::valarray< double >> & | X
| ) |  |

Save a given n-dimensional data martix to file.

Parameters

  [in] | fname | filename to save in (gets overwriten without confirmation)
  [in] | X | matrix to save

Returns
  0 if all ok
  -1 if file creation failed

{
size_t num_points = X.size(); // number of rows
size_t num_features = X[0].size(); // number of columns
std::ofstream fp;
fp.open(fname);
if (!fp.is_open()) {
// error with opening file to write
std::cerr << "Error opening file " << fname << "\n";
return -1;
}
// for each point in the array
for (int i = 0; i < num_points; i++) {
// for each feature in the array
for (int j = 0; j < num_features; j++) {
fp << X[i][j]; // print the feature value
if (j < num_features - 1) { // if not the last feature
fp << ","; // suffix comma
}
}
if (i < num_points - 1) { // if not the last row
fp << "\n"; // start a new line
}
}
fp.close();
return 0;
}
Here is the call graph for this function:

## Variable Documentation

## MAX_ITER

constexpr int MAX_ITER = 500

constexpr

Maximum number of iterations to learn