This repository contains the implementation of Local Fisher Discriminant Analysis (LFDA), a supervised dimensionality reduction technique. LFDA is particularly useful in machine learning and pattern recognition for maintaining local class discriminability while maximizing class separability. This implementation is modular, consisting of the core LFDA algorithm, metrics calculations, and Python bindings for easy integration into data science workflows.
LFDA aims to find a transformation that maximizes the between-class scatter while minimizing the within-class scatter, taking into account the local structure of the data. This implementation provides a flexible, efficient approach to LFDA, including support for various distance metrics and embedding techniques.
lfda.h
&.cpp
: Core LFDA algorithm and class definition.metrics.h
&.cpp
: Distance metrics calculations.- Python bindings: Nanobind usage to expose LFDA functionality to Python.
- LFDA Algorithm: Implements the LFDA algorithm for supervised dimensionality reduction, including fitting the model to input data and transforming new data.
- Distance Metrics: Supports multiple distance metrics, including Euclidean, Manhattan, cosine, and more, for versatile pairwise distance computations.
- Python Integration: Provides Python bindings using Nanobind, making the LFDA implementation accessible in Python environments.
- One improvement over this implementation is Kernel LFDA, which willbe implemented soon.
- Eigen: A high-level C++ library for linear algebra operations.
- OpenMP: For parallel processing and improving computational efficiency.
- Nanobind: For creating Python bindings.
-
Clone the repository:
git clone --recursive-submodules https://github.com/athrva98/LocalFisherDiscriminantAnalysis.git
-
Compile the C++ Code:
-
Ensure Eigen and OpenMP are installed and configured in your environment.
-
Ensure that you have python installed in your environment. To check, open
cmd
and run -
python --version
This project assumes python 3.8, but you can build the package for python 3.8+ versions. To do this, make appropriate changes in the
CMakeLists.txt
file. -
Use your preferred C++ compiler to compile the source files, adjusting include paths as necessary.
-
For windows users, using visual studio, do the following
-
# in the root directory of the project mkdir build cd build cmake .. cmake build .
-
The above should generate a folder called
build
containing the Visual Studio Solution. Then open the solution file and build theALL_BUILD
target. Make sure to build for Release x64 configuration. -
You should see the folder
Release
created. Inside this folder, you will findlfda.pyd
-
Move the
lfda.pyd
file into thepython_lib_install
folder and follow the readme contained in the folder. -
After this, you should have the package
lfda
installed. -
You can test the package by following the readme in
python_test
folder.If you would like to use the package in a C++ project, follow the usage:
#include "lfda.h" // Initialize LFDA with desired parameters LFDA lfda(n_components, k, EMBEDDING_TYPE::WEIGHTED); // Fit the model lfda.Fit(X, y); // Transform new data Eigen::MatrixXd transformed = lfda.Transform(new_X);
To use the package as a python module, follow the pattern in
python_test\lfda_test.py
orfrom lfda import lfda # Initialize and fit LFDA model = lfda.LFDA(n_components=2, k=5, embedding=lfda.EMBEDDING_TYPE.WEIGHTED) model.fit(X, y) # Transform new data transformed = model.transform(new_X)
Contributions to improve or extend the functionality of this LFDA implementation are welcome. Please follow the standard pull request process:
- Fork the repository.
- Create your feature branch (
git checkout -b feature/AmazingFeature
). - Commit your changes (
git commit -m 'Add some AmazingFeature'
). - Push to the branch (
git push origin feature/AmazingFeature
). - Open a pull request.
- The Eigen library for linear algebra operations.
- OpenMP for parallel computation capabilities.
- Nanobind for facilitating Python integration.