mlpack  master
Public Member Functions | Private Member Functions | Private Attributes | List of all members
mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType > Class Template Reference

This class implements mean shift clustering. More...

Public Member Functions

 MeanShift (const double radius=0, const size_t maxIterations=1000, const KernelType kernel=KernelType())
 Create a mean shift object and set the parameters which mean shift will be run with. More...
 
void Cluster (const MatType &data, arma::Col< size_t > &assignments, arma::mat &centroids, bool useSeeds=true)
 Perform mean shift clustering on the data, returning a list of cluster assignments and centroids. More...
 
double EstimateRadius (const MatType &data, const double ratio=0.2)
 Give an estimation of radius based on given dataset. More...
 
const KernelType & Kernel () const
 Get the kernel. More...
 
KernelType & Kernel ()
 Modify the kernel. More...
 
size_t MaxIterations () const
 Get the maximum number of iterations. More...
 
size_t & MaxIterations ()
 Set the maximum number of iterations. More...
 
double Radius () const
 Get the radius. More...
 
void Radius (double radius)
 Set the radius. More...
 

Private Member Functions

template<bool ApplyKernel = UseKernel>
std::enable_if< ApplyKernel, bool >::type CalculateCentroid (const MatType &data, const std::vector< size_t > &neighbors, const std::vector< double > &distances, arma::colvec &centroid)
 Use kernel to calculate new centroid given dataset and valid neighbors. More...
 
template<bool ApplyKernel = UseKernel>
std::enable_if<!ApplyKernel, bool >::type CalculateCentroid (const MatType &data, const std::vector< size_t > &neighbors, const std::vector< double > &, arma::colvec &centroid)
 Use mean to calculate new centroid given dataset and valid neighbors. More...
 
void GenSeeds (const MatType &data, const double binSize, const int minFreq, MatType &seeds)
 To speed up, we can generate some seeds from data set and use them as initial centroids rather than all the points in the data set. More...
 

Private Attributes

KernelType kernel
 Instantiated kernel. More...
 
size_t maxIterations
 Maximum number of iterations before giving up. More...
 
double radius
 If distance of two centroids is less than radius, one will be removed. More...
 

Detailed Description

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
class mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >

This class implements mean shift clustering.

For each point in dataset, apply mean shift algorithm until maximum iterations or convergence. Then remove duplicate centroids.

A simple example of how to run mean shift clustering is shown below.

extern arma::mat data; // Dataset we want to run mean shift on.
arma::Col<size_t> assignments; // Cluster assignments.
arma::mat centroids; // Cluster centroids.
MeanShift<> meanShift();
meanShift.Cluster(dataset, assignments, centroids);
Template Parameters
UseKernelUse kernel or mean to calculate new centroid. If false, KernelType will be ignored.
KernelTypeThe kernel to use.
MatTypeThe type of matrix the data is stored in.

Definition at line 49 of file mean_shift.hpp.

Constructor & Destructor Documentation

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::MeanShift ( const double  radius = 0,
const size_t  maxIterations = 1000,
const KernelType  kernel = KernelType() 
)

Create a mean shift object and set the parameters which mean shift will be run with.

Parameters
radiusIf distance of two centroids is less than it, one will be removed. If this value isn't positive, an estimation will be given when clustering.
maxIterationsMaximum number of iterations allowed before giving up iterations will terminate.
kernelOptional KernelType object.

Member Function Documentation

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
template<bool ApplyKernel = UseKernel>
std::enable_if<ApplyKernel, bool>::type mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::CalculateCentroid ( const MatType &  data,
const std::vector< size_t > &  neighbors,
const std::vector< double > &  distances,
arma::colvec &  centroid 
)
private

Use kernel to calculate new centroid given dataset and valid neighbors.

Parameters
dataThe whole dataset
neighborsValid neighbors
distancesDistances to neighbors

centroid Store calculated centroid

Referenced by mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::Kernel().

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
template<bool ApplyKernel = UseKernel>
std::enable_if<!ApplyKernel, bool>::type mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::CalculateCentroid ( const MatType &  data,
const std::vector< size_t > &  neighbors,
const std::vector< double > &  ,
arma::colvec &  centroid 
)
private

Use mean to calculate new centroid given dataset and valid neighbors.

Parameters
dataThe whole dataset
neighborsValid neighbors
distancesDistances to neighbors

centroid Store calculated centroid

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
void mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::Cluster ( const MatType &  data,
arma::Col< size_t > &  assignments,
arma::mat &  centroids,
bool  useSeeds = true 
)

Perform mean shift clustering on the data, returning a list of cluster assignments and centroids.

Template Parameters
MatTypeType of matrix.
Parameters
dataDataset to cluster.
assignmentsVector to store cluster assignments in.
centroidsMatrix in which centroids are stored.
template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
double mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::EstimateRadius ( const MatType &  data,
const double  ratio = 0.2 
)

Give an estimation of radius based on given dataset.

Parameters
dataDataset for estimation.
ratioPercentage of dataset to use for nearest neighbor search.
template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
void mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::GenSeeds ( const MatType &  data,
const double  binSize,
const int  minFreq,
MatType &  seeds 
)
private

To speed up, we can generate some seeds from data set and use them as initial centroids rather than all the points in the data set.

The basic idea here is that we will place our points into hypercube bins of side length binSize, and any bins that contain fewer than minFreq points will be removed as possible seeds. Usually, 1 is a sufficient parameter for minFreq, and the bin size can be set equal to the estimated radius.

Parameters
dataThe reference data set.
binSizeWidth of hypercube bins.
minFreqMinimum number of points in bin.
seedMatrix to store generated seeds in.

Referenced by mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::Kernel().

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
const KernelType& mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::Kernel ( ) const
inline

Get the kernel.

Definition at line 100 of file mean_shift.hpp.

References mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::kernel.

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
KernelType& mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::Kernel ( )
inline
template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
size_t mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::MaxIterations ( ) const
inline

Get the maximum number of iterations.

Definition at line 90 of file mean_shift.hpp.

References mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::maxIterations.

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
size_t& mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::MaxIterations ( )
inline

Set the maximum number of iterations.

Definition at line 92 of file mean_shift.hpp.

References mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::maxIterations.

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
double mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::Radius ( ) const
inline

Get the radius.

Definition at line 95 of file mean_shift.hpp.

References mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::radius.

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
void mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::Radius ( double  radius)

Set the radius.

Member Data Documentation

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
KernelType mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::kernel
private

Instantiated kernel.

Definition at line 164 of file mean_shift.hpp.

Referenced by mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::Kernel().

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
size_t mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::maxIterations
private

Maximum number of iterations before giving up.

Definition at line 161 of file mean_shift.hpp.

Referenced by mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::MaxIterations().

template<bool UseKernel = false, typename KernelType = kernel::GaussianKernel, typename MatType = arma::mat>
double mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::radius
private

If distance of two centroids is less than radius, one will be removed.

Points with distance to current centroid less than radius will be used to calculate new centroid.

Definition at line 158 of file mean_shift.hpp.

Referenced by mlpack::meanshift::MeanShift< UseKernel, KernelType, MatType >::Radius().


The documentation for this class was generated from the following file: