37#ifndef VIGRA_UNSUPERVISED_DECOMPOSITION_HXX
38#define VIGRA_UNSUPERVISED_DECOMPOSITION_HXX
41#include "mathutil.hxx"
43#include "singular_value_decomposition.hxx"
118template <
class T,
class C1,
class C2,
class C3>
126 int numFeatures =
rowCount(features);
129 vigra_precondition(numSamples >= numFeatures,
130 "principalComponents(): The number of samples has to be larger than the number of features.");
131 vigra_precondition(numFeatures >= numComponents && numComponents >= 1,
132 "principalComponents(): The number of features has to be larger or equal to the number of components in which the feature matrix is decomposed.");
133 vigra_precondition(
rowCount(fz) == numFeatures,
134 "principalComponents(): The output matrix fz has to be of dimension numFeatures*numComponents.");
136 "principalComponents(): The output matrix zv has to be of dimension numComponents*numSamples.");
138 Matrix<T> U(numSamples, numFeatures), S(numFeatures, 1), V(numFeatures, numFeatures);
141 for(
int k=0; k<numComponents; ++k)
164 : min_rel_gain(1e-4),
166 normalized_component_weights(true)
175 vigra_precondition(n >= 1,
176 "PLSAOptions::maximumNumberOfIterations(): number must be a positive integer.");
187 vigra_precondition(g >= 0.0,
188 "PLSAOptions::minimumRelativeGain(): number must be positive or zero.");
202 normalized_component_weights = v;
208 bool normalized_component_weights;
288doxygen_overloaded_function(template <...>
void pLSA)
291template <
class U,
class C1,
class C2,
class C3,
class Random>
296 Random
const& random,
301 int numFeatures =
rowCount(features);
304 vigra_precondition(numFeatures >= numComponents && numComponents >= 1,
305 "pLSA(): The number of features has to be larger or equal to the number of components in which the feature matrix is decomposed.");
306 vigra_precondition(
rowCount(fz) == numFeatures,
307 "pLSA(): The output matrix fz has to be of dimension numFeatures*numComponents.");
309 "pLSA(): The output matrix zv has to be of dimension numComponents*numSamples.");
319 double eps = 1.0/NumericTraits<U>::max();
320 double lastChange = NumericTraits<U>::max();
327 features.
sum(columnSums);
328 Matrix<U> expandedSums = ones<U>(numFeatures, 1) * columnSums;
330 while(iteration < options.max_iterations && (lastChange > options.min_rel_gain))
340 Matrix<U> factor = features / pointWise(fzv + (U)eps);
347 Matrix<U> model = expandedSums * pointWise(fzv);
351 lastChange =
abs((err-err_old) / (U)(err + eps));
359 if(!options.normalized_component_weights)
362 for(
int k=0; k<numSamples; ++k)
367template <
class U,
class C1,
class C2,
class C3>
369pLSA(MultiArrayView<2, U, C1>
const & features,
370 MultiArrayView<2, U, C2> & fz,
371 MultiArrayView<2, U, C3> & zv,
372 PLSAOptions
const & options = PLSAOptions())
374 RandomNumberGenerator<> generator(RandomSeed);
375 pLSA(features, fz, zv, generator, options);
Base class for, and view to, MultiArray.
Definition multi_array.hxx:705
U sum() const
Definition multi_array.hxx:1805
MultiArrayView< N, T, StridedArrayTag > transpose() const
Definition multi_array.hxx:1569
Option object for the pLSA algorithm.
Definition unsupervised_decomposition.hxx:159
PLSAOptions & minimumRelativeGain(double g)
Definition unsupervised_decomposition.hxx:185
PLSAOptions & normalizedComponentWeights(bool v=true)
Definition unsupervised_decomposition.hxx:200
PLSAOptions()
Definition unsupervised_decomposition.hxx:163
PLSAOptions & maximumNumberOfIterations(unsigned int n)
Definition unsupervised_decomposition.hxx:173
Definition matrix.hxx:125
MultiArrayView< 2, vluae_type, StridedArrayTag > transpose() const
Linear algebra functions.
Definition eigensystem.hxx:50
void principalComponents(MultiArrayView< 2, T, C1 > const &features, MultiArrayView< 2, T, C2 > fz, MultiArrayView< 2, T, C3 > zv)
Decompose a matrix according to the PCA algorithm.
Definition unsupervised_decomposition.hxx:120
MultiArrayIndex columnCount(const MultiArrayView< 2, T, C > &x)
Definition matrix.hxx:684
void initMultiArray(...)
Write a value to every element in a multi-dimensional array.
void pLSA(...)
Decompose a matrix according to the pLSA algorithm.
unsigned int singularValueDecomposition(MultiArrayView< 2, T, C1 > const &A, MultiArrayView< 2, T, C2 > &U, MultiArrayView< 2, T, C3 > &S, MultiArrayView< 2, T, C4 > &V)
Definition singular_value_decomposition.hxx:75
MultiArrayView< 2, T, C > rowVector(MultiArrayView< 2, T, C > const &m, MultiArrayIndex d)
Definition matrix.hxx:697
MultiArrayIndex rowCount(const MultiArrayView< 2, T, C > &x)
Definition matrix.hxx:671
FFTWComplex< R >::NormType abs(const FFTWComplex< R > &a)
absolute value (= magnitude)
Definition fftw3.hxx:1002
FFTWComplex< R >::SquaredNormType squaredNorm(const FFTWComplex< R > &a)
squared norm (= squared magnitude)
Definition fftw3.hxx:1044
void prepareColumns(...)
Standardize the columns of a matrix according to given DataPreparationGoals.
MultiArrayView< 2, T, C > columnVector(MultiArrayView< 2, T, C > const &m, MultiArrayIndex d)
Definition matrix.hxx:727