Ocean
ClusteringSpectral.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MATH_SPECTRAL_CLUSTERING_H
9 #define META_OCEAN_MATH_SPECTRAL_CLUSTERING_H
10 
11 #include "ocean/math/Math.h"
12 #include "ocean/math/Matrix.h"
13 
14 namespace Ocean
15 {
16 
17 /**
18  * This class implements spectral clustering.
19  * @ingroup math
20  */
22 {
23  public:
24 
25  /**
26  * Determines the symmetric Laplacian matrix of input given and computes the eigen system of the Laplacian matrix.
27  * @param affinityMatrix a square weight matrix derived from similarity matrix (must be non-negative)
28  * @param numberCluster number of cluster to be returned (0, infinity)
29  */
30  ClusteringSpectral(const Matrix& affinityMatrix, const unsigned int numberCluster);
31 
32  /**
33  * Performances the actual clustering.
34  * The algorithm is implemented according to "Huang et al. Spectral rotation versus K-means in spectral clustering. AAAI'13".
35  * @param iterations Upper limit of iterations to be performed [1, infinity)
36  * @param convergenceThreshold Differential threshold used as convergence criteria (0, infinity)
37  * @return Indices of cluster elements (number of cluster was given in constructor)
38  */
39  std::vector<Indices32> clusterRotation(const unsigned int iterations = 100u, const Scalar convergenceThreshold = Scalar(0.0001));
40 
41  protected:
42 
43  /**
44  * Determines unnormalized graph Laplacian matrix.
45  * L = D - W. W is the weight matrix and D is a diagonal matrix containing the row sum of W
46  * @param affinityMatrix a square weight matrix derived from similarity matrix (must be non-negative)
47  * @return Laplacian matrix
48  */
49  static Matrix determineLaplacianMatrix(const Matrix& affinityMatrix);
50 
51  /**
52  * Determines normalized graph Laplacian matrix closely connected to a random walk.
53  * L = I - D^{-1} * W. W is the weight matrix, I is the identity matrix and D is a diagonal matrix containing the row sum of W
54  * @tparam tSimplify indicating whether a simplified version is used, then substraction with identity matrix is obmitted. Mathematically wrong but commonly used
55  * @param affinityMatrix a square weight matrix derived from similarity matrix (must be non-negative)
56  * @return Laplacian matrix
57  */
58  template<bool tSimplify>
59  static Matrix determineRandomWalkLaplacianMatrix(const Matrix& affinityMatrix);
60 
61  /**
62  * Determines symmetric (normalized) graph Laplacian matrix.
63  * L = I - D^{-1/2} * W * D^{-1/2}. W is the weight matrix, I is the identity matrix and D is a diagonal matrix containing the row sum of W
64  * @tparam tSimplify indicating whether a simplified version is used, then substraction with identity matrix is obmitted. Mathematically wrong but commonly used
65  * @param affinityMatrix a square weight matrix derived from similarity matrix (must be non-negative)
66  * @return Laplacian matrix
67  */
68  template<bool tSimplify>
69  static Matrix determineSymmetricLaplacianMatrix(const Matrix& affinityMatrix);
70 
71  /**
72  * Sorts a list of eigenvectors with corresponding eigenvalues in descending order
73  * @param firstElem First pair of eigenvalue and eigenvector
74  * @param secondElem Second pair of eigenvalue and eigenvector
75  * @return first eigenvalue is greater than eigenvalue of second element
76  */
77  template <typename T>
78  static inline bool pairSortDescending(const std::pair<T, MatrixT<T>>& firstElem, const std::pair<T, MatrixT<T>>& secondElem);
79 
80  protected:
81 
82  /// Column matrix of eigen vectors ordered by eigen values and reduced to [numberCluster] vectors
84 };
85 
86 template <typename T>
87 inline bool ClusteringSpectral::pairSortDescending(const std::pair<T, MatrixT<T>>& firstElem, const std::pair<T, MatrixT<T>>& secondElem)
88 {
89  return firstElem.first > secondElem.first;
90 }
91 
92 }
93 
94 #endif //OCEAN_MATH_SPECTRAL_CLUSTERING_H
This class implements spectral clustering.
Definition: ClusteringSpectral.h:22
ClusteringSpectral(const Matrix &affinityMatrix, const unsigned int numberCluster)
Determines the symmetric Laplacian matrix of input given and computes the eigen system of the Laplaci...
Matrix reducedEigenvectors
Column matrix of eigen vectors ordered by eigen values and reduced to [numberCluster] vectors.
Definition: ClusteringSpectral.h:83
static Matrix determineRandomWalkLaplacianMatrix(const Matrix &affinityMatrix)
Determines normalized graph Laplacian matrix closely connected to a random walk.
static Matrix determineLaplacianMatrix(const Matrix &affinityMatrix)
Determines unnormalized graph Laplacian matrix.
static bool pairSortDescending(const std::pair< T, MatrixT< T >> &firstElem, const std::pair< T, MatrixT< T >> &secondElem)
Sorts a list of eigenvectors with corresponding eigenvalues in descending order.
Definition: ClusteringSpectral.h:87
static Matrix determineSymmetricLaplacianMatrix(const Matrix &affinityMatrix)
Determines symmetric (normalized) graph Laplacian matrix.
std::vector< Indices32 > clusterRotation(const unsigned int iterations=100u, const Scalar convergenceThreshold=Scalar(0.0001))
Performances the actual clustering.
float Scalar
Definition of a scalar type.
Definition: Math.h:128
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15