Class NMF<T>
- Namespace
- AiDotNet.Preprocessing.DimensionalityReduction
- Assembly
- AiDotNet.dll
Non-negative Matrix Factorization for dimensionality reduction.
public class NMF<T> : TransformerBase<T, Matrix<T>, Matrix<T>>, IDataTransformer<T, Matrix<T>, Matrix<T>>
Type Parameters
TThe numeric type for calculations (e.g., float, double).
- Inheritance
-
NMF<T>
- Implements
- Inherited Members
Remarks
NMF factorizes a non-negative matrix V into two non-negative matrices W and H such that V ≈ W × H. This is useful for data that is naturally non-negative (e.g., images, text term frequencies, audio spectrograms).
Unlike PCA, NMF produces parts-based representations where each component represents an additive combination of non-negative features.
For Beginners: NMF learns "building blocks" from your data: - For images: NMF might learn eyes, noses, mouths as separate components - For text: NMF might learn topics as combinations of words - Unlike PCA, components are always positive (additive, never subtractive)
Think of it as finding the parts that, when added together, recreate your data.
Constructors
NMF(int, NMFInit, NMFSolver, double, double, double, int, double, int, int[]?)
Creates a new instance of NMF<T>.
public NMF(int nComponents = 2, NMFInit init = NMFInit.Random, NMFSolver solver = NMFSolver.MU, double beta = 2, double alpha = 0, double l1Ratio = 0, int maxIterations = 200, double tolerance = 0.0001, int randomState = 0, int[]? columnIndices = null)
Parameters
nComponentsintNumber of components. Defaults to 2.
initNMFInitInitialization method. Defaults to Random.
solverNMFSolverSolver algorithm. Defaults to MU (multiplicative update).
betadoubleBeta parameter for beta-divergence. Defaults to 2 (Frobenius norm).
alphadoubleRegularization parameter. Defaults to 0.
l1RatiodoubleL1 ratio in regularization. Defaults to 0.
maxIterationsintMaximum iterations. Defaults to 200.
tolerancedoubleConvergence tolerance. Defaults to 1e-4.
randomStateintRandom seed. Defaults to 0.
columnIndicesint[]The column indices to use, or null for all columns.
Properties
Components
Gets the learned components (H matrix).
public double[,]? Components { get; }
Property Value
- double[,]
NComponents
Gets the number of components.
public int NComponents { get; }
Property Value
NIterations
Gets the number of iterations performed.
public int NIterations { get; }
Property Value
ReconstructionError
Gets the final reconstruction error.
public double ReconstructionError { get; }
Property Value
Solver
Gets the solver type.
public NMFSolver Solver { get; }
Property Value
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
Methods
FitCore(Matrix<T>)
Fits NMF by learning the components matrix.
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>The training data matrix (must be non-negative).
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]
Returns
- string[]
InverseTransformCore(Matrix<T>)
Reconstructs data from coefficient matrix.
protected override Matrix<T> InverseTransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The coefficient matrix W.
Returns
- Matrix<T>
Reconstructed data in original space.
TransformCore(Matrix<T>)
Transforms data by finding the optimal W given learned H.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The coefficient matrix W.