Table of Contents

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

T

The numeric type for calculations (e.g., float, double).

Inheritance
TransformerBase<T, Matrix<T>, Matrix<T>>
NMF<T>
Implements
IDataTransformer<T, Matrix<T>, Matrix<T>>
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

nComponents int

Number of components. Defaults to 2.

init NMFInit

Initialization method. Defaults to Random.

solver NMFSolver

Solver algorithm. Defaults to MU (multiplicative update).

beta double

Beta parameter for beta-divergence. Defaults to 2 (Frobenius norm).

alpha double

Regularization parameter. Defaults to 0.

l1Ratio double

L1 ratio in regularization. Defaults to 0.

maxIterations int

Maximum iterations. Defaults to 200.

tolerance double

Convergence tolerance. Defaults to 1e-4.

randomState int

Random seed. Defaults to 0.

columnIndices int[]

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

int

NIterations

Gets the number of iterations performed.

public int NIterations { get; }

Property Value

int

ReconstructionError

Gets the final reconstruction error.

public double ReconstructionError { get; }

Property Value

double

Solver

Gets the solver type.

public NMFSolver Solver { get; }

Property Value

NMFSolver

SupportsInverseTransform

Gets whether this transformer supports inverse transformation.

public override bool SupportsInverseTransform { get; }

Property Value

bool

Methods

FitCore(Matrix<T>)

Fits NMF by learning the components matrix.

protected override void FitCore(Matrix<T> data)

Parameters

data Matrix<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

inputFeatureNames string[]

Returns

string[]

InverseTransformCore(Matrix<T>)

Reconstructs data from coefficient matrix.

protected override Matrix<T> InverseTransformCore(Matrix<T> data)

Parameters

data Matrix<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

data Matrix<T>

The data to transform.

Returns

Matrix<T>

The coefficient matrix W.