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.DoubleMatrix2DAInput matrixprotected double[]aggregatesSorting aggregatesprotected doubleapproximationErrorCurrent approximation errorprotected double[]approximationErrorsApproximation errors during subsequent iterationsprotected static intDEFAULT_Kprotected static intDEFAULT_MAX_ITERATIONSprotected static booleanDEFAULT_ORDEREDprotected static SeedingStrategyDEFAULT_SEEDING_STRATEGYprotected static doubleDEFAULT_STOP_THRESHOLDprotected intiterationsCompletedIteration counterprotected intkThe desired number of base vectorsprotected intmaxIterationsThe maximum number of iterations the algorithm is allowed to runprotected booleanorderedOrder base vectors according to their 'activity'?protected SeedingStrategyseedingStrategySeeding strategyprotected doublestopThresholdIf the percentage decrease in approximation error becomes smaller thanstopThreshold, the algorithm will stop.protected org.carrot2.math.mahout.matrix.DoubleMatrix2DUBase vector result matrixprotected org.carrot2.math.mahout.matrix.DoubleMatrix2DVCoefficient result matrix -
Constructor Summary
Constructors Constructor Description NonnegativeMatrixFactorizationKL(org.carrot2.math.mahout.matrix.DoubleMatrix2D A)Creates the NonnegativeMatrixFactorizationKL object for matrix A. -
Method Summary
Modifier and Type Method Description voidcompute()Computes the factorization.double[]getAggregates()Returns column aggregates for a sorted factorization, andnullfor an unsorted factorization.doublegetApproximationError()Returns approximation error achieved after the last iteration of the algorithm or -1 if the approximation error is not available.double[]getApproximationErrors()intgetIterationsCompleted()Returns the number of iterations the algorithm has completed.intgetK()Returns the number of base vectors k .intgetMaxIterations()Returns the maximum number of iterations the algorithm is allowed to run.SeedingStrategygetSeedingStrategy()Returns currentSeedingStrategy.doublegetStopThreshold()Returns the algorithmsstopThreshold.org.carrot2.math.mahout.matrix.DoubleMatrix2DgetU()Returns the U matrix (base vectors matrix).org.carrot2.math.mahout.matrix.DoubleMatrix2DgetV()Returns the V matrix (coefficient matrix)booleanisOrdered()Returnstruewhen the factorization is set to generate an ordered basis.protected voidorder()Orders U and V matrices according to the 'activity' of base vectors.voidsetK(int k)Sets the number of base vectors k .voidsetMaxIterations(int maxIterations)Sets the maximum number of iterations the algorithm is allowed to run.voidsetOrdered(boolean ordered)Set totrueto generate an ordered basis.voidsetSeedingStrategy(SeedingStrategy seedingStrategy)Sets newSeedingStrategy.voidsetStopThreshold(double stopThreshold)Sets the algorithmsstopThreshold.StringtoString()protected booleanupdateApproximationError()
-
Field Details
-
k
protected int kThe desired number of base vectors -
DEFAULT_K
protected static int DEFAULT_K -
maxIterations
protected int maxIterationsThe 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 stopThresholdIf 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
Seeding strategy -
DEFAULT_SEEDING_STRATEGY
-
ordered
protected boolean orderedOrder base vectors according to their 'activity'? -
DEFAULT_ORDERED
protected static final boolean DEFAULT_ORDERED- See Also:
- Constant Field Values
-
approximationError
protected double approximationErrorCurrent approximation error -
approximationErrors
protected double[] approximationErrorsApproximation errors during subsequent iterations -
iterationsCompleted
protected int iterationsCompletedIteration counter -
aggregates
protected double[] aggregatesSorting aggregates -
A
protected org.carrot2.math.mahout.matrix.DoubleMatrix2D AInput matrix -
U
protected org.carrot2.math.mahout.matrix.DoubleMatrix2D UBase vector result matrix -
V
protected org.carrot2.math.mahout.matrix.DoubleMatrix2D VCoefficient result matrix
-
-
Constructor Details
-
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 Details
-
compute
public void compute()Computes the factorization. -
toString
-
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
Returns currentSeedingStrategy. -
setSeedingStrategy
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:IterativeMatrixFactorizationReturns approximation error achieved after the last iteration of the algorithm or -1 if the approximation error is not available.- Specified by:
getApproximationErrorin interfaceIterativeMatrixFactorization- Returns:
- approximation error or -1
-
getApproximationErrors
public double[] getApproximationErrors() -
getIterationsCompleted
public int getIterationsCompleted()Description copied from interface:IterativeMatrixFactorizationReturns the number of iterations the algorithm has completed.- Specified by:
getIterationsCompletedin interfaceIterativeMatrixFactorization- Returns:
- the number of iterations the algorithm has completed
-
isOrdered
public boolean isOrdered()Returnstruewhen the factorization is set to generate an ordered basis. -
setOrdered
public void setOrdered(boolean ordered)Set totrueto generate an ordered basis. -
getAggregates
public double[] getAggregates()Returns column aggregates for a sorted factorization, andnullfor an unsorted factorization. -
getU
public org.carrot2.math.mahout.matrix.DoubleMatrix2D getU()Description copied from interface:MatrixFactorizationReturns the U matrix (base vectors matrix).- Specified by:
getUin interfaceMatrixFactorization- Returns:
- U matrix
-
getV
public org.carrot2.math.mahout.matrix.DoubleMatrix2D getV()Description copied from interface:MatrixFactorizationReturns the V matrix (coefficient matrix)- Specified by:
getVin interfaceMatrixFactorization- Returns:
- V matrix
-