Algorithms_in_C++
1.0.0
Set of algorithms implemented in C++.
|
Machine learning algorithms. More...
Classes | |
class | adaline |
Functions | |
int | save_u_matrix (const char *fname, const std::vector< std::vector< std::valarray< double >>> &W) |
double | update_weights (const std::valarray< double > &X, std::vector< std::vector< std::valarray< double >>> *W, std::vector< std::valarray< double >> *D, double alpha, int R) |
void | kohonen_som (const std::vector< std::valarray< double >> &X, std::vector< std::vector< std::valarray< double >>> *W, double alpha_min) |
void | update_weights (const std::valarray< double > &x, std::vector< std::valarray< double >> *W, std::valarray< double > *D, double alpha, int R) |
void | kohonen_som_tracer (const std::vector< std::valarray< double >> &X, std::vector< std::valarray< double >> *W, double alpha_min) |
template<typename T > | |
std::ostream & | operator<< (std::ostream &out, std::vector< std::valarray< T >> const &A) |
template<typename T > | |
std::ostream & | operator<< (std::ostream &out, const std::pair< T, T > &A) |
template<typename T > | |
std::ostream & | operator<< (std::ostream &out, const std::valarray< T > &A) |
template<typename T > | |
std::valarray< T > | insert_element (const std::valarray< T > &A, const T &ele) |
template<typename T > | |
std::valarray< T > | pop_front (const std::valarray< T > &A) |
template<typename T > | |
std::valarray< T > | pop_back (const std::valarray< T > &A) |
template<typename T > | |
void | equal_shuffle (std::vector< std::vector< std::valarray< T >>> &A, std::vector< std::vector< std::valarray< T >>> &B) |
template<typename T > | |
void | uniform_random_initialization (std::vector< std::valarray< T >> &A, const std::pair< size_t, size_t > &shape, const T &low, const T &high) |
template<typename T > | |
void | unit_matrix_initialization (std::vector< std::valarray< T >> &A, const std::pair< size_t, size_t > &shape) |
template<typename T > | |
void | zeroes_initialization (std::vector< std::valarray< T >> &A, const std::pair< size_t, size_t > &shape) |
template<typename T > | |
T | sum (const std::vector< std::valarray< T >> &A) |
template<typename T > | |
std::pair< size_t, size_t > | get_shape (const std::vector< std::valarray< T >> &A) |
template<typename T > | |
std::vector< std::vector< std::valarray< T > > > | minmax_scaler (const std::vector< std::vector< std::valarray< T >>> &A, const T &low, const T &high) |
template<typename T > | |
size_t | argmax (const std::vector< std::valarray< T >> &A) |
template<typename T > | |
std::vector< std::valarray< T > > | apply_function (const std::vector< std::valarray< T >> &A, T(*func)(const T &)) |
template<typename T > | |
std::vector< std::valarray< T > > | operator* (const std::vector< std::valarray< T >> &A, const T &val) |
template<typename T > | |
std::vector< std::valarray< T > > | operator/ (const std::vector< std::valarray< T >> &A, const T &val) |
template<typename T > | |
std::vector< std::valarray< T > > | transpose (const std::vector< std::valarray< T >> &A) |
template<typename T > | |
std::vector< std::valarray< T > > | operator+ (const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B) |
template<typename T > | |
std::vector< std::valarray< T > > | operator- (const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B) |
template<typename T > | |
std::vector< std::valarray< T > > | multiply (const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B) |
template<typename T > | |
std::vector< std::valarray< T > > | hadamard_product (const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B) |
Variables | |
constexpr double | MIN_DISTANCE = 1e-4 |
Machine learning algorithms.
Machine Learning algorithms.
std::vector<std::valarray<T> > machine_learning::apply_function | ( | const std::vector< std::valarray< T >> & | A, |
T(*)(const T &) | func | ||
) |
Function which applys supplied function to every element of 2D vector
T | typename of the vector |
A | 2D vector on which function will be applied |
func | Function to be applied |
size_t machine_learning::argmax | ( | const std::vector< std::valarray< T >> & | A | ) |
Function to get index of maximum element in 2D vector
T | typename of the vector |
A | 2D vector for which maximum index is required |
void machine_learning::equal_shuffle | ( | std::vector< std::vector< std::valarray< T >>> & | A, |
std::vector< std::vector< std::valarray< T >>> & | B | ||
) |
Function to equally shuffle two 3D vectors (used for shuffling training data)
T | typename of the vector |
A | First 3D vector |
B | Second 3D vector |
std::pair<size_t, size_t> machine_learning::get_shape | ( | const std::vector< std::valarray< T >> & | A | ) |
Function to get shape of given 2D vector
T | typename of the vector |
A | 2D vector for which shape is required |
std::vector<std::valarray<T> > machine_learning::hadamard_product | ( | const std::vector< std::valarray< T >> & | A, |
const std::vector< std::valarray< T >> & | B | ||
) |
Function to get hadamard product of two 2D vectors
T | typename of the vector |
A | First 2D vector |
B | Second 2D vector |
std::valarray<T> machine_learning::insert_element | ( | const std::valarray< T > & | A, |
const T & | ele | ||
) |
Function to insert element into 1D vector
T | typename of the 1D vector and the element |
A | 1D vector in which element will to be inserted |
ele | element to be inserted |
void machine_learning::kohonen_som | ( | const std::vector< std::valarray< double >> & | X, |
std::vector< std::vector< std::valarray< double >>> * | W, | ||
double | alpha_min | ||
) |
Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset.
[in] | X | data set |
[in,out] | W | weights matrix |
[in] | alpha_min | terminal value of alpha |
void machine_learning::kohonen_som_tracer | ( | const std::vector< std::valarray< double >> & | X, |
std::vector< std::valarray< double >> * | W, | ||
double | alpha_min | ||
) |
Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset.
[in] | X | data set |
[in,out] | W | weights matrix |
[in] | alpha_min | terminal value of alpha |
std::vector<std::vector<std::valarray<T> > > machine_learning::minmax_scaler | ( | const std::vector< std::vector< std::valarray< T >>> & | A, |
const T & | low, | ||
const T & | high | ||
) |
Function to scale given 3D vector using min-max scaler
T | typename of the vector |
A | 3D vector which will be scaled |
low | new minimum value |
high | new maximum value |
std::vector<std::valarray<T> > machine_learning::multiply | ( | const std::vector< std::valarray< T >> & | A, |
const std::vector< std::valarray< T >> & | B | ||
) |
Function to multiply two 2D vectors
T | typename of the vector |
A | First 2D vector |
B | Second 2D vector |
std::vector<std::valarray<T> > machine_learning::operator* | ( | const std::vector< std::valarray< T >> & | A, |
const T & | val | ||
) |
Overloaded operator "*" to multiply given 2D vector with scaler
T | typename of both vector and the scaler |
A | 2D vector to which scaler will be multiplied |
val | Scaler value which will be multiplied |
std::vector<std::valarray<T> > machine_learning::operator+ | ( | const std::vector< std::valarray< T >> & | A, |
const std::vector< std::valarray< T >> & | B | ||
) |
Overloaded operator "+" to add two 2D vectors
T | typename of the vector |
A | First 2D vector |
B | Second 2D vector |
std::vector<std::valarray<T> > machine_learning::operator- | ( | const std::vector< std::valarray< T >> & | A, |
const std::vector< std::valarray< T >> & | B | ||
) |
Overloaded operator "-" to add subtract 2D vectors
T | typename of the vector |
A | First 2D vector |
B | Second 2D vector |
std::vector<std::valarray<T> > machine_learning::operator/ | ( | const std::vector< std::valarray< T >> & | A, |
const T & | val | ||
) |
Overloaded operator "/" to divide given 2D vector with scaler
T | typename of the vector and the scaler |
A | 2D vector to which scaler will be divided |
val | Scaler value which will be divided |
std::ostream& machine_learning::operator<< | ( | std::ostream & | out, |
const std::pair< T, T > & | A | ||
) |
Overloaded operator "<<" to print a pair
T | typename of the pair |
out | std::ostream to output |
A | Pair to be printed |
std::ostream& machine_learning::operator<< | ( | std::ostream & | out, |
const std::valarray< T > & | A | ||
) |
Overloaded operator "<<" to print a 1D vector
T | typename of the vector |
out | std::ostream to output |
A | 1D vector to be printed |
std::ostream& machine_learning::operator<< | ( | std::ostream & | out, |
std::vector< std::valarray< T >> const & | A | ||
) |
Overloaded operator "<<" to print 2D vector
T | typename of the vector |
out | std::ostream to output |
A | 2D vector to be printed |
std::valarray<T> machine_learning::pop_back | ( | const std::valarray< T > & | A | ) |
Function to remove last element from 1D vector
T | typename of the vector |
A | 1D vector from which last element will be removed |
std::valarray<T> machine_learning::pop_front | ( | const std::valarray< T > & | A | ) |
Function to remove first element from 1D vector
T | typename of the vector |
A | 1D vector from which first element will be removed |
int machine_learning::save_u_matrix | ( | const char * | fname, |
const std::vector< std::vector< std::valarray< double >>> & | W | ||
) |
Create the distance matrix or U-matrix from the trained 3D weiths matrix and save to disk.
[in] | fname | filename to save in (gets overwriten without confirmation) |
[in] | W | model matrix to save |
T machine_learning::sum | ( | const std::vector< std::valarray< T >> & | A | ) |
Function to get sum of all elements in 2D vector
T | typename of the vector |
A | 2D vector for which sum is required |
std::vector<std::valarray<T> > machine_learning::transpose | ( | const std::vector< std::valarray< T >> & | A | ) |
Function to get transpose of 2D vector
T | typename of the vector |
A | 2D vector which will be transposed |
void machine_learning::uniform_random_initialization | ( | std::vector< std::valarray< T >> & | A, |
const std::pair< size_t, size_t > & | shape, | ||
const T & | low, | ||
const T & | high | ||
) |
Function to initialize given 2D vector using uniform random initialization
T | typename of the vector |
A | 2D vector to be initialized |
shape | required shape |
low | lower limit on value |
high | upper limit on value |
void machine_learning::unit_matrix_initialization | ( | std::vector< std::valarray< T >> & | A, |
const std::pair< size_t, size_t > & | shape | ||
) |
Function to Intialize 2D vector as unit matrix
T | typename of the vector |
A | 2D vector to be initialized |
shape | required shape |
void machine_learning::update_weights | ( | const std::valarray< double > & | x, |
std::vector< std::valarray< double >> * | W, | ||
std::valarray< double > * | D, | ||
double | alpha, | ||
int | R | ||
) |
Update weights of the SOM using Kohonen algorithm
[in] | X | data point |
[in,out] | W | weights matrix |
[in,out] | D | temporary vector to store distances |
[in] | alpha | learning rate \(0<\alpha\le1\) |
[in] | R | neighborhood range |
double machine_learning::update_weights | ( | const std::valarray< double > & | X, |
std::vector< std::vector< std::valarray< double >>> * | W, | ||
std::vector< std::valarray< double >> * | D, | ||
double | alpha, | ||
int | R | ||
) |
Update weights of the SOM using Kohonen algorithm
[in] | X | data point - N features |
[in,out] | W | weights matrix - PxQxN |
[in,out] | D | temporary vector to store distances PxQ |
[in] | alpha | learning rate \(0<\alpha\le1\) |
[in] | R | neighborhood range |
void machine_learning::zeroes_initialization | ( | std::vector< std::valarray< T >> & | A, |
const std::pair< size_t, size_t > & | shape | ||
) |
Function to Intialize 2D vector as zeroes
T | typename of the vector |
A | 2D vector to be initialized |
shape | required shape |
|
constexpr |
Minimum average distance of image nodes