A lightweight, header-only C++ machine learning library that provides fast implementations of common ML algorithms with minimal dependencies. Designed for high performance and ease of integration.
✅ K-Nearest Neighbors (KNN) - Classification algorithm
✅ K-Means Clustering - Unsupervised clustering algorithm
🚧 More algorithms coming soon!
c++ ML kit/
├── include/
│ └── mlkit/
│ ├── core.hpp # Includes all algorithms
│ ├── knn.hpp # KNN classifier implementation
│ ├── kmeans.hpp # K-means clustering implementation
│ └── README.md # Detailed API documentation
├── examples/
│ ├── example_knn.cpp # KNN classification example
│ └── example_kmeans.cpp # K-means clustering example
└── README.md
#include "mlkit/core.hpp" // All algorithms
// OR include individually:
// #include "mlkit/knn.hpp"
// #include "mlkit/kmeans.hpp"
mlkit::KNNClassifier<double, int> knn(5);
knn.fit(X_train, y_train);
auto predictions = knn.predict(X_test);
mlkit::ClusteringAlgorithm<double> kmeans(3);
kmeans.fit(X_train);
auto labels = kmeans.get_labels();
auto centroids = kmeans.get_centroids();
Compile with C++17 or later:
# Compile KNN example
g++ -std=c++17 examples/example_knn.cpp -Iinclude -o knn_example
# Compile K-means example
g++ -std=c++17 examples/example_kmeans.cpp -Iinclude -o kmeans_example
# Run examples
./knn_example
./kmeans_example
- Header-only: No separate compilation required
- Template-based: Compile-time optimizations
- STL-optimized: Efficient use of std::vector, std::algorithms
- Low memory footprint: Minimal overhead
- Exception-safe: Proper error handling with std::invalid_argument, std::runtime_error
- Input validation: Comprehensive parameter checking
- Convergence detection: Automatic stopping for iterative algorithms
- Generic types: Support for float, double, and custom numeric types
- Multi-dimensional: Works with data of any dimensionality
- Configurable: Adjustable parameters for different use cases
- Type: Supervised Classification
- Features: Multi-class support, batch prediction, configurable k
- Use cases: Pattern recognition, recommendation systems, anomaly detection
- Type: Unsupervised Clustering
- Features: Configurable clusters, convergence detection, quality metrics
- Use cases: Customer segmentation, image compression, data exploration
🚧 Planned Algorithms:
- Decision Trees
- Linear Regression
- Principal Component Analysis (PCA)
- Support Vector Machines (SVM)
- Neural Networks (ANN/CNN)
For detailed API documentation, usage examples, and implementation details, see:
include/mlkit/README.md
- Complete API referenceexamples/
- Working code examples
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please read CONTRIBUTING.md for guidelines on how to contribute to this project.
- Inspired by scikit-learn's API design
- Built with modern C++ best practices
- Community-driven development
⭐ Star this repository if you find it useful!
Contributions, ideas, and issues are welcome! You can