Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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

 machine_learning
 Machine learning algorithms.
 

Macros

#define MAX_ITER   500
 
#define _USE_MATH_DEFINES
 
#define _USE_MATH_DEFINES
 

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)
 

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]\)
50  {
51  return ((b - a) * (std::rand() % 100) / 100.f) + a;
52 }

◆ 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
100  {
101  val[0] = INFINITY; // initial min value
102  int N = X.size();
103 
104  for (int i = 0; i < N; i++) { // traverse each x-index
105  auto result = std::min_element(std::begin(X[i]), std::end(X[i]));
106  double d_min = *result;
107  int j = std::distance(std::begin(X[i]), result);
108 
109  if (d_min < val[0]) { // if a lower value is found
110  // save the value and its index
111  x_idx[0] = i;
112  y_idx[0] = j;
113  val[0] = d_min;
114  }
115  }
116 }
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
63  {
64  size_t num_points = X.size(); // number of rows
65  size_t num_features = X[0].size(); // number of columns
66 
67  std::ofstream fp;
68  fp.open(fname);
69  if (!fp.is_open()) {
70  // error with opening file to write
71  std::cerr << "Error opening file " << fname << "\n";
72  return -1;
73  }
74 
75  // for each point in the array
76  for (int i = 0; i < num_points; i++) {
77  // for each feature in the array
78  for (int j = 0; j < num_features; j++) {
79  fp << X[i][j]; // print the feature value
80  if (j < num_features - 1) // if not the last feature
81  fp << ","; // suffix comma
82  }
83  if (i < num_points - 1) // if not the last row
84  fp << "\n"; // start a new line
85  }
86 
87  fp.close();
88  return 0;
89 }
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
58  {
59  size_t num_points = X.size(); // number of rows
60  size_t num_features = X[0].size(); // number of columns
61 
62  std::ofstream fp;
63  fp.open(fname);
64  if (!fp.is_open()) {
65  // error with opening file to write
66  std::cerr << "Error opening file " << fname << "\n";
67  return -1;
68  }
69 
70  // for each point in the array
71  for (int i = 0; i < num_points; i++) {
72  // for each feature in the array
73  for (int j = 0; j < num_features; j++) {
74  fp << X[i][j]; // print the feature value
75  if (j < num_features - 1) // if not the last feature
76  fp << ","; // suffix comma
77  }
78  if (i < num_points - 1) // if not the last row
79  fp << "\n"; // start a new line
80  }
81 
82  fp.close();
83  return 0;
84 }
Here is the call graph for this function:
f
uint64_t f[MAX]
Definition: fibonacci_fast.cpp:27
std::vector::size
T size(T... args)
std::distance
T distance(T... args)
std::cerr
std::ofstream
STL class.
std::min_element
T min_element(T... args)
std::ofstream::close
T close(T... args)
std::ofstream::open
T open(T... args)
a
vector< ll > a
Definition: matrix_exponentiation.cpp:53
std::rand
T rand(T... args)
std::begin
T begin(T... args)
std::end
T end(T... args)
std::ofstream::is_open
T is_open(T... args)