Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
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)
 

Namespaces

namespace  machine_learning
 A* search algorithm
 

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]alower limit
[in]bupper limit
Returns
random number in the range \([a,b]\)
53 {
54 return ((b - a) * (std::rand() % 100) / 100.f) + a;
55}
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]Xmatrix to search
[in]Nnumber of points in the vector
[out]valminimum value found
[out]idx_xx-index where minimum value was found
[out]idx_yy-index where minimum value was found
106 {
107 val[0] = INFINITY; // initial min value
108 size_t N = X.size();
109
110 for (int i = 0; i < N; i++) { // traverse each x-index
111 auto result = std::min_element(std::begin(X[i]), std::end(X[i]));
112 double d_min = *result;
113 std::ptrdiff_t j = std::distance(std::begin(X[i]), result);
114
115 if (d_min < val[0]) { // if a lower value is found
116 // save the value and its index
117 x_idx[0] = i;
118 y_idx[0] = j;
119 val[0] = d_min;
120 }
121 }
122}
T begin(T... args)
T distance(T... args)
T end(T... args)
T min_element(T... args)
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]fnamefilename to save in (gets overwriten without confirmation)
[in]Xmatrix to save
Returns
0 if all ok
-1 if file creation failed
66 {
67 size_t num_points = X.size(); // number of rows
68 size_t num_features = X[0].size(); // number of columns
69
71 fp.open(fname);
72 if (!fp.is_open()) {
73 // error with opening file to write
74 std::cerr << "Error opening file " << fname << ": "
75 << std::strerror(errno) << "\n";
76 return -1;
77 }
78
79 // for each point in the array
80 for (int i = 0; i < num_points; i++) {
81 // for each feature in the array
82 for (int j = 0; j < num_features; j++) {
83 fp << X[i][j]; // print the feature value
84 if (j < num_features - 1) { // if not the last feature
85 fp << ","; // suffix comma
86 }
87 }
88 if (i < num_points - 1) { // if not the last row
89 fp << "\n"; // start a new line
90 }
91 }
92
93 fp.close();
94 return 0;
95}
T close(T... args)
T is_open(T... args)
T open(T... args)
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]fnamefilename to save in (gets overwriten without confirmation)
[in]Xmatrix to save
Returns
0 if all ok
-1 if file creation failed
59 {
60 size_t num_points = X.size(); // number of rows
61 size_t num_features = X[0].size(); // number of columns
62
64 fp.open(fname);
65 if (!fp.is_open()) {
66 // error with opening file to write
67 std::cerr << "Error opening file " << fname << "\n";
68 return -1;
69 }
70
71 // for each point in the array
72 for (int i = 0; i < num_points; i++) {
73 // for each feature in the array
74 for (int j = 0; j < num_features; j++) {
75 fp << X[i][j]; // print the feature value
76 if (j < num_features - 1) { // if not the last feature
77 fp << ","; // suffix comma
78 }
79 }
80 if (i < num_points - 1) { // if not the last row
81 fp << "\n"; // start a new line
82 }
83 }
84
85 fp.close();
86 return 0;
87}
Here is the call graph for this function:

Variable Documentation

◆ MAX_ITER

constexpr int MAX_ITER = 500
constexpr

Maximum number of iterations to learn