TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
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.
for std::vector
Machine learning algorithms.
A* is an informed search algorithm, or a best-first search, meaning that it is formulated in terms of weighted graphs: starting from a specific starting node of a graph (initial state), it aims to find a path to the given goal node having the smallest cost (least distance travelled, shortest time, etc.). It evaluates by maintaining a tree of paths originating at the start node and extending those paths one edge at a time until it reaches the final state. The weighted edges (or cost) is evaluated on two factors, G score (cost required from starting node or initial state to current state) and H score (cost required from current state to final state). The F(state), then is evaluated as: F(state) = G(state) + H(state).
To solve the given search with shortest cost or path possible is to inspect values having minimum F(state).
Machine learning algorithms
for std::transform and std::sort for assert for std::pow and std::sqrt for std::cout for std::accumulate for std::unordered_map
Machine learning algorithms
std::vector< std::valarray< T > > machine_learning::apply_function | ( | const std::vector< std::valarray< T > > & | A, |
T(* | func )(const T &) ) |
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 |
Definition at line 329 of file vector_ops.hpp.
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 |
Definition at line 307 of file vector_ops.hpp.
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 |
Definition at line 136 of file vector_ops.hpp.
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 |
Definition at line 247 of file vector_ops.hpp.
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 |
Definition at line 494 of file vector_ops.hpp.
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 |
Definition at line 85 of file vector_ops.hpp.
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 |
Definition at line 269 of file kohonen_som_topology.cpp.
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 |
Definition at line 149 of file kohonen_som_trace.cpp.
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 |
Definition at line 269 of file vector_ops.hpp.
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 |
Definition at line 460 of file vector_ops.hpp.
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 |
Definition at line 347 of file vector_ops.hpp.
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 |
Definition at line 406 of file vector_ops.hpp.
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 |
Definition at line 433 of file vector_ops.hpp.
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 |
Definition at line 365 of file vector_ops.hpp.
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 |
Definition at line 52 of file vector_ops.hpp.
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 |
Definition at line 67 of file vector_ops.hpp.
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 |
Definition at line 32 of file vector_ops.hpp.
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 |
Definition at line 119 of file vector_ops.hpp.
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 |
Definition at line 102 of file vector_ops.hpp.
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 |
Definition at line 142 of file kohonen_som_topology.cpp.
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 |
Definition at line 232 of file vector_ops.hpp.
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 |
Definition at line 382 of file vector_ops.hpp.
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 |
Definition at line 166 of file vector_ops.hpp.
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 |
Definition at line 193 of file vector_ops.hpp.
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 |
Definition at line 103 of file kohonen_som_trace.cpp.
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 |
Definition at line 200 of file kohonen_som_topology.cpp.
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 |
Definition at line 213 of file vector_ops.hpp.
|
constexpr |
Minimum average distance of image nodes
Definition at line 129 of file kohonen_som_topology.cpp.