Introduction
On this page, several simple MLPACK examples are contained, in increasing order of complexity.
Covariance Computation
A simple program to compute the covariance of a data matrix ("data.csv"), assuming that the data is already centered, and save it to file.
int main()
{
arma::mat data;
arma::mat cov = data * trans(data) / data.n_cols;
}
Nearest Neighbor
This simple program uses the mlpack::neighbor::NeighborSearch object to find the nearest neighbor of each point in a dataset using the L1 metric, and then print the index of the neighbor and the distance of it to stdout.
int main()
{
arma::mat data;
arma::Col<size_t> neighbors;
arma::vec distances;
nn.Search(1, neighbors, distances);
for (size_t i = 0; i < neighbors.n_elem; ++i)
{
Log::Info <<
"Nearest neighbor of point " << i <<
" is point "
<< neighbors[i] << " and the distance is " << distances[i] << ".\n";
}
}
Other examples
For more complex examples, it is useful to refer to the main executables:
- methods/neighbor_search/allknn_main.cpp
- methods/neighbor_search/allkfn_main.cpp
- methods/emst/emst_main.cpp
- methods/radical/radical_main.cpp
- methods/nca/nca_main.cpp
- methods/naive_bayes/nbc_main.cpp
- methods/pca/pca_main.cpp
- methods/lars/lars_main.cpp
- methods/linear_regression/linear_regression_main.cpp
- methods/gmm/gmm_main.cpp
- methods/kmeans/kmeans_main.cpp