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. More...
 
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. More...
 
SHARK_EXPORT_SYMBOL RealVector parameterVector () const
 from IParameterizable More...
 
SHARK_EXPORT_SYMBOL void setParameterVector (RealVector const &newParameters)
 from IParameterizable More...
 
SHARK_EXPORT_SYMBOL std::size_t numberOfParameters () const
 from IParameterizable More...
 
Shape inputShape () const
 return the dimension of the inputs More...
 
SHARK_EXPORT_SYMBOL std::size_t numberOfClusters () const
 return the number of centroids in the model More...
 
Data< RealVector > const & centroids () const
 read access to the centroid vectors More...
 
void setCentroids (Data< RealVector > const &newCentroids)
 overwrite the centroid vectors More...
 
SHARK_EXPORT_SYMBOL void read (InArchive &archive)
 from ISerializable More...
 
SHARK_EXPORT_SYMBOL void write (OutArchive &archive) const
 from ISerializable More...
 
SHARK_EXPORT_SYMBOL RealVector softMembership (RealVector const &pattern) const
 from AbstractClustering: Compute cluster memberships. More...
 
SHARK_EXPORT_SYMBOL RealMatrix softMembership (BatchInputType const &patterns) const
 From AbstractClustering: Compute cluster memberships for a batch of patterns. More...
 
SHARK_EXPORT_SYMBOL RealMatrix distances (BatchInputType const &patterns) const
 Computes the distances of each pattern to all cluster centers. More...
 
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. More...
 
virtual BatchOutputType hardMembership (BatchInputType const &patterns) const
 Compute best matching cluster for a batch of inputs. More...
 
void read (InArchive &archive)
 empty default implementation of ISerializable::read More...
 
void write (OutArchive &archive) const
 empty default implementation of ISerializable::write More...
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
- Public Member Functions inherited from shark::IParameterizable<>
virtual ~IParameterizable ()
 
- Public Member Functions inherited from shark::ISerializable
virtual ~ISerializable ()
 Virtual d'tor. More...
 
void load (InArchive &archive, unsigned int version)
 Versioned loading of components, calls read(...). More...
 
void save (OutArchive &archive, unsigned int version) const
 Versioned storing of components, calls write(...). More...
 
 BOOST_SERIALIZATION_SPLIT_MEMBER ()
 

Protected Member Functions

virtual SHARK_EXPORT_SYMBOL double membershipKernel (double dist) const
 

Protected Attributes

Data< RealVector > m_centroids
 centroid vectors More...
 
- 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<>
typedef RealVector 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 58 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 99 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.

Referenced by setCentroids().

◆ 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

Referenced by setCentroids().

◆ 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 91 of file Centroids.h.

References shark::dataDimension(), m_centroids, numberOfClusters(), and SHARK_EXPORT_SYMBOL.

◆ 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)

Referenced by setCentroids().

◆ name()

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

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 78 of file Centroids.h.

References numberOfParameters(), parameterVector(), setParameterVector(), and SHARK_EXPORT_SYMBOL.

◆ numberOfClusters()

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

return the number of centroids in the model

Implements shark::AbstractClustering< RealVector >.

Referenced by inputShape().

◆ numberOfParameters()

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

from IParameterizable

Reimplemented from shark::IParameterizable<>.

Referenced by name().

◆ parameterVector()

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

from IParameterizable

Reimplemented from shark::IParameterizable<>.

Referenced by name().

◆ read()

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

from ISerializable

Reimplemented from shark::ISerializable.

Referenced by setCentroids().

◆ setCentroids()

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

overwrite the centroid vectors

Definition at line 104 of file Centroids.h.

References distances(), initFromData(), m_centroids, membershipKernel(), read(), SHARK_EXPORT_SYMBOL, softMembership(), and write().

◆ setParameterVector()

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

from IParameterizable

Reimplemented from shark::IParameterizable<>.

Referenced by name().

◆ softMembership() [1/2]

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

from AbstractClustering: Compute cluster memberships.

Reimplemented from shark::AbstractClustering< RealVector >.

Referenced by setCentroids().

◆ softMembership() [2/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 >.

◆ write()

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

from ISerializable

Reimplemented from shark::ISerializable.

Referenced by setCentroids().

Member Data Documentation

◆ m_centroids

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

centroid vectors

Definition at line 146 of file Centroids.h.

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


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