Skip to content

Example

Matthew Henderson edited this page Oct 3, 2016 · 3 revisions

k means example

By using RcppMLPACK, a k-means method which can be called by R can be implemented like below. The interface between R and C++ is handled by Rcpp and RcppArmadillo.

#include "RcppMLPACK.h"

using namespace mlpack::kmeans;
using namespace Rcpp;

// [[Rcpp::export]]
List kmeans(const arma::mat& data, const int& clusters) {
    
    arma::Col<size_t> assignments;

    // Initialize with the default arguments.
    // More specification available
    // Like KMeans<mlpack::metric::MahalanobisDistance> k(1000, 1.0, distance);
    KMeans<> k;

    k.Cluster(data, clusters, assignments); 

    return List::create(_["clusters"] = clusters,
                        _["result"]   = assignments);
}

k-means example using inline

inline package provides a complete wrapper around the compilation, linking, and loading steps. So all the steps can be done in an R session. There is no reason that RcppMLPACK doesn't support the inline compilation.

library(inline)
library(RcppMLPACK)
code <- '
  arma::mat data = as<arma::mat>(test);
  int clusters = as<int>(n);
  arma::Col<size_t> assignments;
  mlpack::kmeans::KMeans<> k;
  k.Cluster(data, clusters, assignments); 
  return List::create(_["clusters"] = clusters,
                      _["result"]   = assignments);
'
mlKmeans <- cxxfunction(signature(test="numeric", n ="integer"), code, 
                        plugin="RcppMLPACK")
data(trees, package="datasets")
mlKmeans(t(trees), 3)

As said above, MLPACK uses a column-major format of matrix, so when we pass data from R to MLPACK, a transpose may be needed.

Clone this wiki locally