Package org.carrot2.math.matrix
Class NonnegativeMatrixFactorizationKL
- java.lang.Object
-
- org.carrot2.math.matrix.NonnegativeMatrixFactorizationKL
-
- All Implemented Interfaces:
IterativeMatrixFactorization
,MatrixFactorization
public class NonnegativeMatrixFactorizationKL extends Object
Performs matrix factorization using the Non-negative Matrix Factorization by minimization of Kullback-Leibler divergence between A and UV' and multiplicative updating.
-
-
Field Summary
Fields Modifier and Type Field Description protected org.carrot2.math.mahout.matrix.DoubleMatrix2D
A
Input matrixprotected double[]
aggregates
Sorting aggregatesprotected double
approximationError
Current approximation errorprotected double[]
approximationErrors
Approximation errors during subsequent iterationsprotected static int
DEFAULT_K
protected static int
DEFAULT_MAX_ITERATIONS
protected static boolean
DEFAULT_ORDERED
protected static SeedingStrategy
DEFAULT_SEEDING_STRATEGY
protected static double
DEFAULT_STOP_THRESHOLD
protected int
iterationsCompleted
Iteration counterprotected int
k
The desired number of base vectorsprotected int
maxIterations
The maximum number of iterations the algorithm is allowed to runprotected boolean
ordered
Order base vectors according to their 'activity'?protected SeedingStrategy
seedingStrategy
Seeding strategyprotected double
stopThreshold
If the percentage decrease in approximation error becomes smaller thanstopThreshold
, the algorithm will stop.protected org.carrot2.math.mahout.matrix.DoubleMatrix2D
U
Base vector result matrixprotected org.carrot2.math.mahout.matrix.DoubleMatrix2D
V
Coefficient result matrix
-
Constructor Summary
Constructors Constructor Description NonnegativeMatrixFactorizationKL(org.carrot2.math.mahout.matrix.DoubleMatrix2D A)
Creates the NonnegativeMatrixFactorizationKL object for matrix A.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
compute()
Computes the factorization.double[]
getAggregates()
Returns column aggregates for a sorted factorization, andnull
for an unsorted factorization.double
getApproximationError()
Returns approximation error achieved after the last iteration of the algorithm or -1 if the approximation error is not available.double[]
getApproximationErrors()
int
getIterationsCompleted()
Returns the number of iterations the algorithm has completed.int
getK()
Returns the number of base vectors k .int
getMaxIterations()
Returns the maximum number of iterations the algorithm is allowed to run.SeedingStrategy
getSeedingStrategy()
Returns currentSeedingStrategy
.double
getStopThreshold()
Returns the algorithmsstopThreshold
.org.carrot2.math.mahout.matrix.DoubleMatrix2D
getU()
Returns the U matrix (base vectors matrix).org.carrot2.math.mahout.matrix.DoubleMatrix2D
getV()
Returns the V matrix (coefficient matrix)boolean
isOrdered()
Returnstrue
when the factorization is set to generate an ordered basis.protected void
order()
Orders U and V matrices according to the 'activity' of base vectors.void
setK(int k)
Sets the number of base vectors k .void
setMaxIterations(int maxIterations)
Sets the maximum number of iterations the algorithm is allowed to run.void
setOrdered(boolean ordered)
Set totrue
to generate an ordered basis.void
setSeedingStrategy(SeedingStrategy seedingStrategy)
Sets newSeedingStrategy
.void
setStopThreshold(double stopThreshold)
Sets the algorithmsstopThreshold
.String
toString()
protected boolean
updateApproximationError()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.carrot2.math.matrix.MatrixFactorization
getU, getV
-
-
-
-
Field Detail
-
k
protected int k
The desired number of base vectors
-
DEFAULT_K
protected static int DEFAULT_K
-
maxIterations
protected int maxIterations
The maximum number of iterations the algorithm is allowed to run
-
DEFAULT_MAX_ITERATIONS
protected static final int DEFAULT_MAX_ITERATIONS
- See Also:
- Constant Field Values
-
stopThreshold
protected double stopThreshold
If the percentage decrease in approximation error becomes smaller thanstopThreshold
, the algorithm will stop. Note: calculation of approximation error is quite costly. Setting the threshold to -1 turns off approximation error calculation and hence makes the algorithm do the maximum number of iterations.
-
DEFAULT_STOP_THRESHOLD
protected static double DEFAULT_STOP_THRESHOLD
-
seedingStrategy
protected SeedingStrategy seedingStrategy
Seeding strategy
-
DEFAULT_SEEDING_STRATEGY
protected static final SeedingStrategy DEFAULT_SEEDING_STRATEGY
-
ordered
protected boolean ordered
Order base vectors according to their 'activity'?
-
DEFAULT_ORDERED
protected static final boolean DEFAULT_ORDERED
- See Also:
- Constant Field Values
-
approximationError
protected double approximationError
Current approximation error
-
approximationErrors
protected double[] approximationErrors
Approximation errors during subsequent iterations
-
iterationsCompleted
protected int iterationsCompleted
Iteration counter
-
aggregates
protected double[] aggregates
Sorting aggregates
-
A
protected org.carrot2.math.mahout.matrix.DoubleMatrix2D A
Input matrix
-
U
protected org.carrot2.math.mahout.matrix.DoubleMatrix2D U
Base vector result matrix
-
V
protected org.carrot2.math.mahout.matrix.DoubleMatrix2D V
Coefficient result matrix
-
-
Constructor Detail
-
NonnegativeMatrixFactorizationKL
public NonnegativeMatrixFactorizationKL(org.carrot2.math.mahout.matrix.DoubleMatrix2D A)
Creates the NonnegativeMatrixFactorizationKL object for matrix A. Before accessing results, perform computations by calling thecompute()
method.- Parameters:
A
- matrix to be factorized
-
-
Method Detail
-
compute
public void compute()
Computes the factorization.
-
setK
public void setK(int k)
Sets the number of base vectors k .- Parameters:
k
- the number of base vectors
-
getK
public int getK()
Returns the number of base vectors k .
-
updateApproximationError
protected boolean updateApproximationError()
- Returns:
- true if the decrease in the approximation error is smaller than the
stopThreshold
-
order
protected void order()
Orders U and V matrices according to the 'activity' of base vectors.
-
getSeedingStrategy
public SeedingStrategy getSeedingStrategy()
Returns currentSeedingStrategy
.
-
setSeedingStrategy
public void setSeedingStrategy(SeedingStrategy seedingStrategy)
Sets newSeedingStrategy
.
-
getMaxIterations
public int getMaxIterations()
Returns the maximum number of iterations the algorithm is allowed to run.
-
setMaxIterations
public void setMaxIterations(int maxIterations)
Sets the maximum number of iterations the algorithm is allowed to run.
-
getStopThreshold
public double getStopThreshold()
Returns the algorithmsstopThreshold
. If the percentage decrease in approximation error becomes smaller thanstopThreshold
, the algorithm will stop.
-
setStopThreshold
public void setStopThreshold(double stopThreshold)
Sets the algorithmsstopThreshold
. If the percentage decrease in approximation error becomes smaller thanstopThreshold
, the algorithm will stop.Note: calculation of approximation error is quite costly. Setting the threshold to -1 turns off calculation of the approximation error and hence makes the algorithm do the maximum allowed number of iterations.
-
getApproximationError
public double getApproximationError()
Description copied from interface:IterativeMatrixFactorization
Returns approximation error achieved after the last iteration of the algorithm or -1 if the approximation error is not available.- Specified by:
getApproximationError
in interfaceIterativeMatrixFactorization
- Returns:
- approximation error or -1
-
getApproximationErrors
public double[] getApproximationErrors()
-
getIterationsCompleted
public int getIterationsCompleted()
Description copied from interface:IterativeMatrixFactorization
Returns the number of iterations the algorithm has completed.- Specified by:
getIterationsCompleted
in interfaceIterativeMatrixFactorization
- Returns:
- the number of iterations the algorithm has completed
-
isOrdered
public boolean isOrdered()
Returnstrue
when the factorization is set to generate an ordered basis.
-
setOrdered
public void setOrdered(boolean ordered)
Set totrue
to generate an ordered basis.
-
getAggregates
public double[] getAggregates()
Returns column aggregates for a sorted factorization, andnull
for an unsorted factorization.
-
getU
public org.carrot2.math.mahout.matrix.DoubleMatrix2D getU()
Description copied from interface:MatrixFactorization
Returns the U matrix (base vectors matrix).- Specified by:
getU
in interfaceMatrixFactorization
- Returns:
- U matrix
-
getV
public org.carrot2.math.mahout.matrix.DoubleMatrix2D getV()
Description copied from interface:MatrixFactorization
Returns the V matrix (coefficient matrix)- Specified by:
getV
in interfaceMatrixFactorization
- Returns:
- V matrix
-
-