---
name: "sample"
section: "3"
platform: "linux"
package: "mlpack-doc"
version: "1.0.10-1"
release: "Debian 8 (Jessie)"
platforms: ["linux"]
url: "https://new.carta.tech/sample"
---

# sample(3)

Simple Sample MLPACK Programs

## NAME

sample - Simple Sample MLPACK Programs

## 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.

```
// Includes all relevant components of MLPACK.
#include <mlpack/core.hpp>
// Convenience.
using namespace mlpack;
int main()
{
  // First, load the data.
  arma::mat data;
  // Use data::Load() which transposes the matrix.
  data::Load("data.csv", data, true);
  // Now compute the covariance.  We assume that the data is already centered.
  // Remember, because the matrix is column-major, the covariance operation is
  // transposed.
  arma::mat cov = data * trans(data) / data.n_cols;
  // Save the output.
  data::Save("cov.csv", cov, true);
}
```

## 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.

```
#include <mlpack/core.hpp>
#include <mlpack/methods/neighbor_search/neighbor_search.hpp>
using namespace mlpack;
using namespace mlpack::neighbor; // NeighborSearch and NearestNeighborSort
using namespace mlpack::metric; // ManhattanDistance
int main()
{
  // Load the data from data.csv (hard-coded).  Use CLI for simple command-line
  // parameter handling.
  arma::mat data;
  data::Load("data.csv", data, true);
  // Use templates to specify that we want a NeighborSearch object which uses
  // the Manhattan distance.
  NeighborSearch<NearestNeighborSort, ManhattanDistance> nn(data);
  // Create the object we will store the nearest neighbors in.
  arma::Col<size_t> neighbors;
  arma::vec distances; // We need to store the distance too.
  // Compute the neighbors.
  nn.Search(1, neighbors, distances);
  // Write each neighbor and distance using Log.
  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] << ".0;
  }
}
```

## 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
