shark::Centroids Class Reference

Clusters defined by centroids. More...

#include <shark/Models/Clustering/Centroids.h>

+ Inheritance diagram for shark::Centroids:

Public Member Functions

SHARK_EXPORT_SYMBOL Centroids ()
 Default constructor.
 
SHARK_EXPORT_SYMBOL Centroids (std::size_t centroids, std::size_t dimension)
 
SHARK_EXPORT_SYMBOL Centroids (Data< RealVector > const &centroids)
 
std::string name () const
 From INameable: return the class name.
 
SHARK_EXPORT_SYMBOL RealVector parameterVector () const
 from IParameterizable
 
SHARK_EXPORT_SYMBOL void setParameterVector (RealVector const &newParameters)
 from IParameterizable
 
SHARK_EXPORT_SYMBOL std::size_t numberOfParameters () const
 from IParameterizable
 
Shape inputShape () const
 return the dimension of the inputs
 
SHARK_EXPORT_SYMBOL std::size_t numberOfClusters () const
 return the number of centroids in the model
 
Data< RealVector > const & centroids () const
 read access to the centroid vectors
 
void setCentroids (Data< RealVector > const &newCentroids)
 overwrite the centroid vectors
 
SHARK_EXPORT_SYMBOL void read (InArchive &archive)
 from ISerializable
 
SHARK_EXPORT_SYMBOL void write (OutArchive &archive) const
 from ISerializable
 
SHARK_EXPORT_SYMBOL RealVector softMembership (RealVector const &pattern) const
 from AbstractClustering: Compute cluster memberships.
 
SHARK_EXPORT_SYMBOL RealMatrix softMembership (BatchInputType const &patterns) const
 From AbstractClustering: Compute cluster memberships for a batch of patterns.
 
SHARK_EXPORT_SYMBOL RealMatrix distances (BatchInputType const &patterns) const
 Computes the distances of each pattern to all cluster centers.
 
SHARK_EXPORT_SYMBOL void initFromData (ClassificationDataset const &data, std::size_t noClusters=0, std::size_t noClasses=0)
 
SHARK_EXPORT_SYMBOL void initFromData (Data< RealVector > const &dataset, std::size_t noClusters)
 
- Public Member Functions inherited from shark::AbstractClustering< RealVector >
const Featuresfeatures () const
 
virtual void updateFeatures ()
 
bool hasSoftMembershipFunction () const
 
virtual unsigned int hardMembership (InputType const &pattern) const
 Compute best matching cluster.
 
virtual BatchOutputType hardMembership (BatchInputType const &patterns) const
 Compute best matching cluster for a batch of inputs.
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
- Public Member Functions inherited from shark::IParameterizable< VectorType >
virtual ~IParameterizable ()
 
- Public Member Functions inherited from shark::ISerializable
virtual ~ISerializable ()
 Virtual d'tor.
 
void load (InArchive &archive, unsigned int version)
 Versioned loading of components, calls read(...).
 
void save (OutArchive &archive, unsigned int version) const
 Versioned storing of components, calls write(...).
 
 BOOST_SERIALIZATION_SPLIT_MEMBER ()
 

Protected Member Functions

virtual SHARK_EXPORT_SYMBOL double membershipKernel (double dist) const
 

Protected Attributes

Data< RealVector > m_centroids
 centroid vectors
 
- Protected Attributes inherited from shark::AbstractClustering< RealVector >
Features m_features
 

Additional Inherited Members

- Public Types inherited from shark::AbstractClustering< RealVector >
enum  Feature
 
typedef RealVector InputType
 
typedef unsigned int OutputType
 
typedef Batch< InputType >::type BatchInputType
 
typedef Batch< OutputType >::type BatchOutputType
 
typedef TypedFlags< FeatureFeatures
 
typedef TypedFeatureNotAvailableException< FeatureFeatureNotAvailableException
 
- Public Types inherited from shark::IParameterizable< VectorType >
typedef VectorType ParameterVectorType
 

Detailed Description

Clusters defined by centroids.

Centroids are an elementary way to define clusters by means of the one-nearest-neighbor rule. This rule defines a hard clustering decision.
The Centroids class uses inverse distances to compute soft clustering memberships. This is arbitrary and can be changed by overriding the membershipKernel function.

Definition at line 59 of file Centroids.h.

Constructor & Destructor Documentation

◆ Centroids() [1/3]

SHARK_EXPORT_SYMBOL shark::Centroids::Centroids ( )

Default constructor.

◆ Centroids() [2/3]

SHARK_EXPORT_SYMBOL shark::Centroids::Centroids ( std::size_t  centroids,
std::size_t  dimension 
)

Constructor

Parameters
centroidsnumber of centroids in the model (initially zero)
dimensiondimension of the input space, and thus of the centroids

◆ Centroids() [3/3]

SHARK_EXPORT_SYMBOL shark::Centroids::Centroids ( Data< RealVector > const &  centroids)

Constructor

Parameters
centroidscentroid vectors

Member Function Documentation

◆ centroids()

Data< RealVector > const & shark::Centroids::centroids ( ) const
inline

read access to the centroid vectors

Definition at line 100 of file Centroids.h.

References m_centroids.

Referenced by main().

◆ distances()

SHARK_EXPORT_SYMBOL RealMatrix shark::Centroids::distances ( BatchInputType const &  patterns) const

Computes the distances of each pattern to all cluster centers.

◆ initFromData() [1/2]

SHARK_EXPORT_SYMBOL void shark::Centroids::initFromData ( ClassificationDataset const &  data,
std::size_t  noClusters = 0,
std::size_t  noClasses = 0 
)

initialize centroids from labeled data: take the first data points with different labels; if there are more centroids than classes, the remaining centroids are filled with the first elements in the data set

Parameters
datadataset from which to take the centroids
noClustersnumber of centroids in the model, default 0 is mapped to the number of classes in the data set
noClassesnumber of clases in the dataset, default 0 means that the number is computed

◆ initFromData() [2/2]

SHARK_EXPORT_SYMBOL void shark::Centroids::initFromData ( Data< RealVector > const &  dataset,
std::size_t  noClusters 
)

initialize centroids from unlabeled data: take a random subset of data points

Parameters
datasetdataset from which to take the centroids
noClustersnumber of centroids in the model

◆ inputShape()

Shape shark::Centroids::inputShape ( ) const
inlinevirtual

return the dimension of the inputs

Implements shark::AbstractClustering< RealVector >.

Definition at line 92 of file Centroids.h.

References shark::dataDimension(), and m_centroids.

◆ membershipKernel()

virtual SHARK_EXPORT_SYMBOL double shark::Centroids::membershipKernel ( double  dist) const
protectedvirtual

Compute unnormalized membership from distance. The default implementation is to return exp(-distance)

◆ name()

std::string shark::Centroids::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 79 of file Centroids.h.

◆ numberOfClusters()

SHARK_EXPORT_SYMBOL std::size_t shark::Centroids::numberOfClusters ( ) const
virtual

return the number of centroids in the model

Implements shark::AbstractClustering< RealVector >.

◆ numberOfParameters()

SHARK_EXPORT_SYMBOL std::size_t shark::Centroids::numberOfParameters ( ) const
virtual

◆ parameterVector()

SHARK_EXPORT_SYMBOL RealVector shark::Centroids::parameterVector ( ) const
virtual

◆ read()

SHARK_EXPORT_SYMBOL void shark::Centroids::read ( InArchive archive)
virtual

◆ setCentroids()

void shark::Centroids::setCentroids ( Data< RealVector > const &  newCentroids)
inline

overwrite the centroid vectors

Definition at line 105 of file Centroids.h.

References m_centroids.

◆ setParameterVector()

SHARK_EXPORT_SYMBOL void shark::Centroids::setParameterVector ( RealVector const &  newParameters)
virtual

◆ softMembership() [1/2]

SHARK_EXPORT_SYMBOL RealMatrix shark::Centroids::softMembership ( BatchInputType const &  patterns) const
virtual

From AbstractClustering: Compute cluster memberships for a batch of patterns.

Reimplemented from shark::AbstractClustering< RealVector >.

◆ softMembership() [2/2]

SHARK_EXPORT_SYMBOL RealVector shark::Centroids::softMembership ( RealVector const &  pattern) const
virtual

from AbstractClustering: Compute cluster memberships.

Reimplemented from shark::AbstractClustering< RealVector >.

◆ write()

SHARK_EXPORT_SYMBOL void shark::Centroids::write ( OutArchive archive) const
virtual

Member Data Documentation

◆ m_centroids

Data<RealVector> shark::Centroids::m_centroids
protected

centroid vectors

Definition at line 147 of file Centroids.h.

Referenced by centroids(), inputShape(), and setCentroids().


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