Class NCA<T>
- Namespace
- AiDotNet.Preprocessing.DimensionalityReduction
- Assembly
- AiDotNet.dll
Neighborhood Components Analysis (NCA) for supervised dimensionality reduction.
public class NCA<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
-
NCA<T>
- Implements
- Inherited Members
Remarks
NCA is a supervised dimensionality reduction algorithm that learns a linear transformation to maximize the expected leave-one-out classification accuracy in the transformed space. It uses stochastic neighbor assignment for soft nearest-neighbor classification.
The algorithm: 1. Define soft neighbor probabilities using softmax over distances 2. Compute expected leave-one-out classification accuracy 3. Optimize transformation matrix using gradient descent 4. Project data using learned transformation
For Beginners: NCA learns a space where k-NN works well: - Points with same label are pulled closer together - Points with different labels are pushed apart - The learned transformation is linear (a matrix) - Can be used for feature extraction before classification
Use cases:
- Preprocessing for k-NN classifiers
- Metric learning for distance-based methods
- Visualization with class structure preserved
- Feature extraction for classification tasks
Constructors
NCA(int, int, double, double, int?, int[]?)
Creates a new instance of NCA<T>.
public NCA(int nComponents = 2, int maxIter = 100, double learningRate = 0.01, double tol = 1E-05, int? randomState = null, int[]? columnIndices = null)
Parameters
nComponentsintTarget dimensionality. Defaults to 2.
maxIterintMaximum number of optimization iterations. Defaults to 100.
learningRatedoubleLearning rate for gradient descent. Defaults to 0.01.
toldoubleTolerance for convergence. Defaults to 1e-5.
randomStateint?Random seed for reproducibility.
columnIndicesint[]The column indices to use, or null for all columns.
Properties
NComponents
Gets the number of components (dimensions).
public int NComponents { get; }
Property Value
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
TransformationMatrix
Gets the learned transformation matrix.
public double[,]? TransformationMatrix { get; }
Property Value
- double[,]
Methods
FitCore(Matrix<T>)
Fits NCA (without labels - uses unsupervised initialization).
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>
Remarks
This method provides a fallback when no labels are available. For best results, use the Fit(data, labels) overload with class labels.
FitTransformSupervised(Matrix<T>, int[])
Fits NCA using the provided data and labels, then transforms the data.
public Matrix<T> FitTransformSupervised(Matrix<T> data, int[] labels)
Parameters
dataMatrix<T>The input data matrix.
labelsint[]The class labels for each sample.
Returns
- Matrix<T>
The transformed data projected onto the learned space.
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]
Returns
- string[]
InverseTransformCore(Matrix<T>)
Inverse transformation is not supported.
protected override Matrix<T> InverseTransformCore(Matrix<T> data)
Parameters
dataMatrix<T>
Returns
- Matrix<T>
TransformCore(Matrix<T>)
Transforms data using the learned transformation matrix.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>
Returns
- Matrix<T>